1 20 21 package net.sourceforge.lightcrypto; 22 23 import org.bouncycastle.util.encoders.Base64; 24 25 import java.io.ByteArrayInputStream ; 26 import java.io.IOException ; 27 28 38 39 public class Hsqldb { 40 private static int BUFFERSIZE_TEXT = 64; 41 42 50 public static String digest( 51 String text 52 , String algorithm) throws CryptoException { 53 return Digesters.digest(new ByteArrayInputStream (text.getBytes()), algorithm, BUFFERSIZE_TEXT).toString(); 54 } 55 56 66 public static StringBuffer loadkey( 67 String file 68 , StringBuffer passphrase) throws Exception , CryptoException, KeyException { 69 70 SafeObject so = new SafeObject(); 71 so = Key.loadkey(file, passphrase); 72 73 StringBuffer key = new StringBuffer (so.getBase64()); 74 so.clearText(); 75 76 return key; 77 } 78 79 90 public static String encrypt( 91 String text 92 , String key 93 ) throws Exception , CryptoException, IOException { 94 95 SafeObject k = new SafeObject(); 96 k.setText(Base64.decode(key)); 97 98 String cipher = new String (Crypt.encrypt(new StringBuffer (text), k)); 99 k.clearText(); 100 101 return cipher; 102 } 103 104 114 public static String decrypt( 115 String text 116 , String key 117 ) throws Exception , CryptoException, IOException { 118 119 SafeObject k = new SafeObject(); 120 k.setText(Base64.decode(key)); 121 122 String decipher = new String (Crypt.decrypt(new StringBuffer (text), k)); 123 k.clearText(); 124 125 return decipher; 126 } 127 128 139 public static String PBEEncrypt( 140 String text 141 , String passphrase 142 ) throws Exception , CryptoException, IOException { 143 144 String cipher = new String (PBECrypt.encrypt(new StringBuffer (text), new StringBuffer (passphrase))); 145 146 return cipher; 147 } 148 149 159 public static String PBEDecrypt( 160 String text 161 , String passphrase 162 ) throws Exception , CryptoException, IOException { 163 164 String decipher = new String (PBECrypt.decrypt(new StringBuffer (text), new StringBuffer (passphrase))); 165 166 return decipher; 167 } 168 169 176 public static String hmac( 177 String text 178 ) throws CryptoException { 179 180 String hmac = new String (HMacs.mac(new StringBuffer (text))); 181 return hmac; 182 } 183 184 192 public static String mac( 193 String text 194 ,String key 195 ) throws Exception , CryptoException { 196 197 SafeObject k = new SafeObject(); 198 k.setText(Base64.decode(key)); 199 200 String mac = new String (Macs.mac(new StringBuffer (text), k)); 201 k.clearText(); 202 203 return mac; 204 } 205 } 206 | Popular Tags |