1 20 21 package net.sourceforge.lightcrypto; 22 23 import org.bouncycastle.crypto.PBEParametersGenerator; 24 import org.bouncycastle.crypto.BufferedBlockCipher; 25 import org.bouncycastle.crypto.CipherParameters; 26 import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher; 27 import org.bouncycastle.crypto.modes.CBCBlockCipher; 28 import org.bouncycastle.crypto.engines.TwofishEngine; 29 import org.bouncycastle.crypto.digests.SHA1Digest; 30 import org.bouncycastle.crypto.generators.PKCS12ParametersGenerator; 31 import org.bouncycastle.util.encoders.Base64; 32 33 import java.io.*; 34 import java.security.SecureRandom ; 35 36 44 45 public class PBECrypt { 46 private static int BUFFERSIZE_TEXT = 64; 47 private static int BUFFERSIZE_FILE = 8192; 48 49 private static int PBECount = 20; 50 private static int PBEKeyLength = 256; 51 52 62 public static StringBuffer encrypt( 63 StringBuffer text 64 , StringBuffer passphrase 65 ) throws CryptoException, IOException { 66 67 return encrypt(text, passphrase, null); 68 } 69 70 81 public static StringBuffer encrypt( 82 StringBuffer text 83 , StringBuffer passphrase 84 , StringBuffer seed 85 ) throws CryptoException, IOException { 86 87 ByteArrayOutputStream bao = new ByteArrayOutputStream(); 88 DataOutputStream dao = new DataOutputStream(bao); 89 90 encrypt(new ByteArrayInputStream(text.toString().getBytes()), dao, passphrase, seed, BUFFERSIZE_TEXT); 92 93 StringBuffer result = new StringBuffer (new String (Base64.encode(bao.toByteArray()))); 94 95 dao.flush(); 97 dao.close(); 98 99 return result; 100 } 101 102 113 public static void encrypt( 114 InputStream is 115 , DataOutputStream daos 116 , StringBuffer passphrase 117 , StringBuffer seed 118 , int bufferlength 119 ) throws CryptoException { 120 121 try { 122 SecureRandom sr = new SecureRandom (); 123 124 if (seed != null && !seed.equals("")) { 126 sr.setSeed(seed.toString().getBytes()); 127 } 128 129 byte[] randomsalt = new byte[8]; 131 sr.nextBytes(randomsalt); 132 133 daos.write(randomsalt); 135 136 PBEParametersGenerator generator = new PKCS12ParametersGenerator(new SHA1Digest()); 138 generator.init(PBEParametersGenerator.PKCS12PasswordToBytes(passphrase.toString().toCharArray()), randomsalt, PBECount); 139 140 TwofishEngine blockCipher = new TwofishEngine(); 141 CBCBlockCipher cbcCipher = new CBCBlockCipher(blockCipher); 142 BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(cbcCipher); 143 144 CipherParameters param = generator.generateDerivedParameters(PBEKeyLength,128); 145 146 cipher.init(true, param); 148 149 byte[] buffer = new byte[bufferlength]; 150 int length = cipher.getOutputSize(bufferlength); 151 byte[] result = new byte[length]; 152 int outputLen = 0; 153 154 while ((length = is.read(buffer)) != -1) { 156 outputLen = cipher.processBytes(buffer, 0, length, result, 0); 157 158 if (outputLen > 0) { 159 daos.write(result, 0, outputLen); 160 } 161 } 162 163 outputLen = cipher.doFinal(result, 0); 165 if (outputLen > 0) { 166 daos.write(result, 0, outputLen); 167 } 168 169 } catch (Exception ex) { 170 ex.printStackTrace(); 171 throw new CryptoException(ex.getMessage()); 172 } 173 } 174 175 185 public static void encryptFile( 186 String file 187 , String newfile 188 , StringBuffer passphrase 189 ) throws CryptoException, IOException { 190 encryptFile(file, newfile, passphrase, null); 191 } 192 193 204 public static void encryptFile( 205 String file 206 , String newfile 207 , StringBuffer passphrase 208 , StringBuffer seed 209 ) throws CryptoException, IOException { 210 211 FileInputStream fis = new FileInputStream(file); 212 FileOutputStream fos = new FileOutputStream(newfile); 213 214 DataOutputStream dao = new DataOutputStream(fos); 215 216 encrypt(fis, dao, passphrase, seed, BUFFERSIZE_FILE); 218 219 dao.flush(); 221 dao.close(); 222 223 fis.close(); 225 fos.close(); 226 } 227 228 237 public static StringBuffer decrypt( 238 StringBuffer text 239 , StringBuffer passphrase 240 ) throws CryptoException, IOException { 241 242 ByteArrayOutputStream bao = new ByteArrayOutputStream(); 243 DataOutputStream dao = new DataOutputStream(bao); 244 245 decrypt(new ByteArrayInputStream(Base64.decode(text.toString())), dao, passphrase, BUFFERSIZE_TEXT); 247 248 dao.flush(); 250 dao.close(); 251 252 return new StringBuffer (new String (bao.toByteArray())); 253 } 254 255 264 public static void decryptFile( 265 String file 266 , String newfile 267 , StringBuffer passphrase 268 ) throws CryptoException, IOException { 269 270 FileInputStream fis = new FileInputStream(file); 271 FileOutputStream fos = new FileOutputStream(newfile); 272 DataOutputStream dao = new DataOutputStream(fos); 273 274 decrypt(fis, dao, passphrase, BUFFERSIZE_FILE); 276 277 dao.flush(); 279 dao.close(); 280 281 fis.close(); 283 fos.close(); 284 } 285 286 295 public static void decrypt( 296 InputStream is 297 , DataOutputStream daos 298 , StringBuffer passphrase 299 , int bufferlength 300 ) throws CryptoException { 301 302 try { 303 byte[] randomsalt = new byte[8]; 305 is.read(randomsalt); 306 307 PBEParametersGenerator generator = new PKCS12ParametersGenerator(new SHA1Digest()); 309 generator.init(PBEParametersGenerator.PKCS12PasswordToBytes(passphrase.toString().toCharArray()), randomsalt, PBECount); 310 311 TwofishEngine blockCipher = new TwofishEngine(); 312 CBCBlockCipher cbcCipher = new CBCBlockCipher(blockCipher); 313 BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(cbcCipher); 314 315 CipherParameters param = generator.generateDerivedParameters(PBEKeyLength,128); 316 317 cipher.init(false, param); 319 320 byte[] buffer = new byte[bufferlength]; 321 int length = cipher.getOutputSize(bufferlength); 322 byte[] result = new byte[length]; 323 int outputLen = 0; 324 325 while ((length = is.read(buffer)) != -1) { 327 outputLen = cipher.processBytes(buffer, 0, length, result, 0); 328 329 if (outputLen > 0) { 330 daos.write(result, 0, outputLen); 331 } 332 } 333 334 outputLen = cipher.doFinal(result, 0); 336 if (outputLen > 0) { 337 daos.write(result, 0, outputLen); 338 } 339 340 } catch (Exception ex) { 341 ex.printStackTrace(); 342 throw new CryptoException(ex.getMessage()); 343 } 344 } 345 } 346 | Popular Tags |