1 21 22 package net.sourceforge.jcetaglib.lib; 23 24 import net.sourceforge.jcetaglib.exceptions.KeystoreException; 25 import org.bouncycastle.jce.provider.BouncyCastleProvider; 26 27 import javax.crypto.Cipher; 28 import javax.crypto.KeyGenerator; 29 import javax.crypto.SecretKey; 30 import javax.crypto.SecretKeyFactory; 31 import javax.crypto.spec.PBEKeySpec; 32 import javax.crypto.spec.PBEParameterSpec; 33 import java.io.ByteArrayOutputStream ; 34 import java.io.FileInputStream ; 35 import java.io.FileOutputStream ; 36 import java.io.IOException ; 37 import java.security.Key ; 38 import java.security.SecureRandom ; 39 import java.security.Security ; 40 41 49 public class Keystore { 50 51 static final String SECRET_KEYSTORE_ALGORITHM = "PBEWithSHAAndTwofish-CBC"; 53 static final int SECRET_KEYSTORE_COUNT = 100; 54 55 65 public static void generateKey(String algorithm 66 , int strength 67 , byte[] seed 68 , String file 69 , StringBuffer passphrase) 70 throws KeystoreException { 71 72 KeyGenerator kg = null; 73 Key key; 74 PBEKeySpec pbeKeySpec; 75 PBEParameterSpec pbeParamSpec; 76 SecretKeyFactory keyFac; 77 SecretKey pbeKey; 78 Cipher pbeCipher; 79 FileOutputStream fos = null; 80 81 try { 82 83 Security.addProvider(new BouncyCastleProvider()); 84 SecureRandom sr = Seed.getSecureRandom(seed); 85 86 kg = KeyGenerator.getInstance(algorithm, "BC"); 88 kg.init(strength, sr); 89 90 key = kg.generateKey(); 92 93 95 byte[] randomsalt = new byte[8]; 97 sr.nextBytes(randomsalt); 98 99 pbeParamSpec = new PBEParameterSpec(randomsalt, SECRET_KEYSTORE_COUNT); 101 102 pbeKeySpec = new PBEKeySpec(passphrase.toString().toCharArray()); 103 keyFac = SecretKeyFactory.getInstance(SECRET_KEYSTORE_ALGORITHM); 104 pbeKey = keyFac.generateSecret(pbeKeySpec); 105 106 pbeCipher = Cipher.getInstance(SECRET_KEYSTORE_ALGORITHM); 108 109 pbeCipher.init(Cipher.WRAP_MODE, pbeKey, pbeParamSpec); 111 byte[] wrappedKey = pbeCipher.wrap(key); 112 113 fos = new FileOutputStream (file); 115 fos.write(randomsalt); 116 fos.write(wrappedKey); 117 } catch (Exception ex) { 118 ex.printStackTrace(); 119 throw new KeystoreException(ex.getMessage()); 120 } finally { 121 if (fos != null) { 123 try { 124 fos.close(); 125 } catch (IOException ioe) { 126 ; 127 } 128 } 129 130 key = null; 132 pbeKey = null; 133 Clean.blank(passphrase); 134 passphrase = null; 135 } 136 } 137 138 147 public static Key loadKey(String algorithm 148 , String file 149 , StringBuffer passphrase) 150 throws KeystoreException { 151 152 FileInputStream fInput = null; 153 ByteArrayOutputStream baos = null; 154 PBEKeySpec pbeKeySpec; 155 PBEParameterSpec pbeParamSpec; 156 SecretKeyFactory keyFac; 157 SecretKey pbeKey; 158 Cipher pbeCipher; 159 Key newkey; 160 161 try { 162 Security.addProvider(new BouncyCastleProvider()); 164 165 fInput = new FileInputStream (file); 166 167 byte[] randomsalt = new byte[8]; 169 fInput.read(randomsalt); 170 171 baos = new ByteArrayOutputStream (); 173 int i = 0; 174 while ((i = fInput.read()) != -1) { 175 baos.write(i); 176 } 177 178 byte[] wrappedKey = baos.toByteArray(); 179 180 pbeParamSpec = new PBEParameterSpec(randomsalt, SECRET_KEYSTORE_COUNT); 182 183 pbeKeySpec = new PBEKeySpec(passphrase.toString().toCharArray()); 184 keyFac = SecretKeyFactory.getInstance(SECRET_KEYSTORE_ALGORITHM); 185 pbeKey = keyFac.generateSecret(pbeKeySpec); 186 187 pbeCipher = Cipher.getInstance(SECRET_KEYSTORE_ALGORITHM); 189 190 pbeCipher.init(Cipher.UNWRAP_MODE, pbeKey, pbeParamSpec); 192 newkey = pbeCipher.unwrap(wrappedKey, algorithm, Cipher.SECRET_KEY); 193 194 return newkey; 195 } catch (Exception ex) { 196 ex.printStackTrace(); 197 throw new KeystoreException(ex.getMessage()); 198 } finally { 199 if (fInput != null) { 201 try { 202 fInput.close(); 203 } catch (IOException ioe) { 204 ; 205 } 206 } 207 if (baos != null) { 209 try { 210 baos.close(); 211 } catch (IOException ioe) { 212 ; 213 } 214 } 215 pbeKey = null; 217 Clean.blank(passphrase); 218 passphrase = null; 219 } 220 } 221 } 222 | Popular Tags |