1 20 21 package net.sourceforge.lightcrypto; 22 23 import org.bouncycastle.crypto.*; 24 import org.bouncycastle.crypto.digests.SHA1Digest; 25 import org.bouncycastle.crypto.engines.TwofishEngine; 26 import org.bouncycastle.crypto.generators.PKCS12ParametersGenerator; 27 import org.bouncycastle.crypto.modes.CBCBlockCipher; 28 import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher; 29 30 import java.io.ByteArrayOutputStream ; 31 import java.io.FileInputStream ; 32 import java.io.FileNotFoundException ; 33 import java.io.FileOutputStream ; 34 import java.security.SecureRandom ; 35 36 45 46 public class Key { 47 private static int SecKeystoreCount = 100; 48 private static int SecKeystoreKeylength = 256; 49 private static int SecKeylength = 128; 50 51 54 public Key() { 55 } 56 57 64 public static void generatekey( 65 String file 66 , StringBuffer passphrase) throws CryptoException { 67 generatekey(file, passphrase, null); 68 } 69 70 78 public static void generatekey( 79 String file 80 , StringBuffer passphrase 81 , StringBuffer seed 82 ) throws CryptoException { 83 84 FileOutputStream fos = null; 85 byte[] key = null; 86 byte[] wrappedkey = null; 87 byte[] newkey = null; 88 89 try { 90 SecureRandom sr = new SecureRandom (); 91 92 if (seed != null && !seed.equals("")) { 94 sr.setSeed(seed.toString().getBytes()); 95 Clean.blank(seed); 97 } 98 99 KeyGenerationParameters keygen = new KeyGenerationParameters(sr, SecKeylength); 100 CipherKeyGenerator cipherkey = new CipherKeyGenerator(); 101 cipherkey.init(keygen); 102 103 key = cipherkey.generateKey(); 105 106 byte[] randomsalt = new byte[8]; 108 sr.nextBytes(randomsalt); 109 110 PBEParametersGenerator generator = new PKCS12ParametersGenerator(new SHA1Digest()); 112 generator.init(PBEParametersGenerator.PKCS12PasswordToBytes(passphrase.toString().toCharArray()), randomsalt, SecKeystoreCount); 113 114 Clean.blank(passphrase); 116 117 TwofishEngine blockCipher = new TwofishEngine(); 118 CBCBlockCipher cbcCipher = new CBCBlockCipher(blockCipher); 119 BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(cbcCipher); 120 121 CipherParameters param = generator.generateDerivedParameters(SecKeystoreKeylength,128); 122 123 cipher.init(true, param); 125 126 int outputLen = 0; 127 wrappedkey = new byte[cipher.getOutputSize(key.length)]; 128 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 129 130 outputLen = cipher.processBytes(key, 0, key.length, wrappedkey, 0); 132 133 if (outputLen > 0) { 134 baos.write(wrappedkey, 0, outputLen); 135 } 136 137 outputLen = cipher.doFinal(wrappedkey, 0); 139 140 if (outputLen > 0) { 141 baos.write(wrappedkey, 0, outputLen); 142 } 143 144 fos = new FileOutputStream (file); 146 fos.write(randomsalt); 147 fos.write(baos.toByteArray()); 148 fos.close(); 149 150 baos.close(); 151 } catch (Exception ex) { 152 ex.printStackTrace(); 153 throw new CryptoException(ex.getMessage()); 154 } finally { 155 if (fos != null) { 157 try { 158 fos.close(); 159 } catch (Exception e) { 160 ; 161 } 162 fos = null; 163 } 164 165 Clean.blank(passphrase); 167 if (seed != null) { 168 Clean.blank(seed); 169 seed = null; 170 } 171 if (key != null) { 172 Clean.blank(key); 173 key = null; 174 } 175 if (wrappedkey != null) { 176 Clean.blank(wrappedkey); 177 wrappedkey = null; 178 } 179 if (newkey != null) { 180 Clean.blank(wrappedkey); 181 wrappedkey = null; 182 } 183 } 184 } 185 186 195 public static SafeObject loadkey( 196 String file 197 , StringBuffer passphrase) throws CryptoException, KeyException { 198 199 FileInputStream fInput = null; 200 byte[] keybytes = null; 201 202 try { 203 fInput = new FileInputStream (file); 204 205 byte[] randomsalt = new byte[8]; 207 fInput.read(randomsalt); 208 209 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 211 int i = 0; 212 while ((i = fInput.read()) != -1) { 213 baos.write(i); 214 } 215 fInput.close(); 216 byte[] wrappedkey = baos.toByteArray(); 217 baos.close(); 218 219 PBEParametersGenerator generator = new PKCS12ParametersGenerator(new SHA1Digest()); 221 generator.init(PBEParametersGenerator.PKCS12PasswordToBytes(passphrase.toString().toCharArray()), randomsalt, SecKeystoreCount); 222 223 Clean.blank(passphrase); 225 226 TwofishEngine blockCipher = new TwofishEngine(); 227 CBCBlockCipher cbcCipher = new CBCBlockCipher(blockCipher); 228 BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(cbcCipher); 229 230 CipherParameters param = generator.generateDerivedParameters(SecKeystoreKeylength,128); 231 232 cipher.init(false, param); 234 235 int outputLen = 0; 236 keybytes = new byte[cipher.getOutputSize(wrappedkey.length)]; 237 238 ByteArrayOutputStream baos2 = new ByteArrayOutputStream (); 239 240 outputLen = cipher.processBytes(wrappedkey, 0, wrappedkey.length, keybytes, 0); 242 243 if (outputLen > 0) { 244 baos2.write(keybytes, 0, outputLen); 245 } 246 247 outputLen = cipher.doFinal(keybytes, 0); 249 250 if (outputLen > 0) { 251 baos2.write(keybytes, 0, outputLen); 252 } 253 254 SafeObject sf = new SafeObject(); 255 sf.setText(baos2.toByteArray()); 256 257 baos2.close(); 258 259 return sf; 260 } catch (FileNotFoundException fnfe) { 261 throw new KeyException("Unable to load key from keystore \"" + file + "\" - keystore could not be found"); 262 } catch (Exception ex) { 263 ex.printStackTrace(); 264 throw new CryptoException(ex.getMessage()); 265 } finally { 266 if (fInput != null) { 268 try { 269 fInput.close(); 270 } catch (Exception e) { 271 ; 272 } 273 fInput = null; 274 } 275 276 Clean.blank(passphrase); 278 } 279 } 280 281 286 public void setSecKeystoreCount(int secKeystoreCount) { 287 SecKeystoreCount = secKeystoreCount; 288 } 289 290 295 public static void setSecKeystoreKeylength(int secKeystoreKeylength) { 296 SecKeystoreKeylength = secKeystoreKeylength; 297 } 298 299 304 public void setSecKeylength(int secKeylength) { 305 SecKeylength = secKeylength; 306 } 307 } | Popular Tags |