1 7 8 9 package org.enhydra.oyster.crypto; 10 11 import java.security.SecureRandom ; 12 import java.security.Key ; 13 import javax.crypto.SecretKey; 14 import javax.crypto.KeyGenerator; 15 import javax.crypto.Cipher; 16 import javax.crypto.spec.IvParameterSpec; 17 import javax.crypto.spec.SecretKeySpec; 18 import org.enhydra.oyster.exception.SMIMEException; 19 import org.enhydra.oyster.exception.ErrorStorage; 20 21 22 29 public class SymmetricEncryption { 30 31 34 private byte[] iv = null; 35 36 39 private byte[] symmetricKey = null; 40 41 44 private int symmetricKeyLength = 0; 45 46 49 private String algorithmName = null; 50 51 54 private byte[] encryptedValue = null; 55 56 59 private byte[] decryptedValue = null; 60 61 68 public SymmetricEncryption (String algorithmName0, int keyLength0) throws SMIMEException 69 { 70 AlgorithmChecker alg = new AlgorithmChecker(algorithmName0, keyLength0); 71 symmetricKeyLength = alg.getKeySize(); 72 algorithmName = alg.getAlgorithmName(); 73 } 74 75 83 public void encrypt (byte[] forEncrypt0) throws SMIMEException { 84 try { 85 SecureRandom rand = SecureRandom.getInstance("SHA1PRNG", "SUN"); 86 KeyGenerator keyGen = KeyGenerator.getInstance(algorithmName, "BC"); keyGen.init(symmetricKeyLength); 88 Key key = keyGen.generateKey(); 89 Cipher cipher = Cipher.getInstance(algorithmName + "/CBC/PKCS5Padding", "BC"); 90 cipher.init(Cipher.ENCRYPT_MODE, key, rand); 91 iv = cipher.getIV(); 92 symmetricKey = key.getEncoded(); 93 encryptedValue = cipher.doFinal(forEncrypt0); 94 } 95 catch(Exception e) { 96 throw SMIMEException.getInstance(this, e, "encryption" ); 97 } 98 } 99 100 109 public void encrypt (byte[] forEncrypt0, byte[] simKey0) throws SMIMEException{ 110 try { 111 if (simKey0.length != symmetricKeyLength) 112 throw new SMIMEException(this, 1011); 113 SecureRandom rand = SecureRandom.getInstance("SHA1PRNG", "SUN"); 114 SecretKeySpec secSpec = new SecretKeySpec(simKey0, algorithmName); 115 Cipher cipher = Cipher.getInstance(algorithmName + "/CBC/PKCS5Padding", "BC"); 116 cipher.init(Cipher.ENCRYPT_MODE, (SecretKey)secSpec, rand); 117 iv = cipher.getIV(); 118 symmetricKey = simKey0; 119 encryptedValue = cipher.doFinal(forEncrypt0); 120 } 121 catch(Exception e) { 122 throw SMIMEException.getInstance(this, e, "encryption" ); 123 } 124 } 125 126 130 public String getAlgorithmName () { 131 return algorithmName; 132 } 133 134 139 public int getKeyLength () { 140 return symmetricKeyLength; 141 } 142 143 148 public byte[] getIV () { 149 return iv; 150 } 151 152 156 public byte[] getSymmetricKey () { 157 return symmetricKey; 158 } 159 160 164 public byte[] getEncryptedValue () { 165 return encryptedValue; 166 } 167 168 177 public void decrypt (byte[] forDecrypt) throws SMIMEException { 178 try { 179 SecretKeySpec secSpec = new SecretKeySpec(symmetricKey, algorithmName); 180 Cipher symCipher = Cipher.getInstance(algorithmName + "/CBC/PKCS5Padding", "BC"); 181 symCipher.init(Cipher.DECRYPT_MODE, (SecretKey)secSpec, new IvParameterSpec(iv)); 182 decryptedValue = symCipher.doFinal(forDecrypt); 183 } 184 catch(Exception e) { 185 throw SMIMEException.getInstance(this, e, "decryption" ); 186 } 187 } 188 189 201 public void decrypt (byte[] forDecrypt, byte[] simKey0, byte[] iv0) throws SMIMEException { 202 try { 203 if (simKey0.length*8 != symmetricKeyLength) 204 throw new SMIMEException(this, 1011); 205 if (iv0.length != 8) 206 throw new SMIMEException(this, 1012); 207 SecretKeySpec secSpec = new SecretKeySpec(simKey0, algorithmName); 208 Cipher symCipher = Cipher.getInstance(algorithmName + "/CBC/PKCS5Padding", "BC"); 209 symCipher.init(Cipher.DECRYPT_MODE, (SecretKey)secSpec, new IvParameterSpec(iv0)); 210 decryptedValue = symCipher.doFinal(forDecrypt); 211 } 212 catch(Exception e) { 213 throw SMIMEException.getInstance(this, e, "decryption" ); 214 } 215 } 216 217 221 public byte[] getDecryptedValue () { 222 return decryptedValue; 223 } 224 225 229 public void reset () { 230 iv = null; 231 symmetricKey = null; 232 symmetricKeyLength = 0; 233 String algorithmName = null; 234 encryptedValue = null; 235 decryptedValue = null; 236 } 237 } 238 239 240 241 | Popular Tags |