KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > oyster > crypto > SymmetricEncryption


1 /*
2  * Title: Oyster Project
3  * Description: S/MIME email sending capabilities
4  * @Author Vladan Obradovic
5  * @Version 2.1.5
6  */

7
8
9 package org.enhydra.oyster.crypto;
10
11 import java.security.SecureRandom JavaDoc;
12 import java.security.Key JavaDoc;
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 /**
23  * SymmetricEncryption class is used for symmetric encryption large amounts
24  * of data. Algorithm names and corresponding key lengths are:<BR>
25  * DES - 56<BR>
26  * DES_EDE3_CBC - 128, 192<BR>
27  * RC2_CBC - 40, 64, 128<BR>
28  */

29 public class SymmetricEncryption {
30
31 /**
32  * Storage for initialization vector (iv).
33  */

34   private byte[] iv = null;
35
36 /**
37  * Storage for symmetric key.
38  */

39   private byte[] symmetricKey = null;
40
41 /**
42  * Storage for symetric key length.
43  */

44   private int symmetricKeyLength = 0;
45
46 /**
47  * Storage for symetric algorithm name.
48  */

49   private String JavaDoc algorithmName = null;
50
51 /**
52  * Container for encrypted message.
53  */

54   private byte[] encryptedValue = null;
55
56 /**
57  * Container for decrypted message.
58  */

59   private byte[] decryptedValue = null;
60
61 /**
62  * Construction with the given algorithm name and key size in bits.
63  * @param algorithmName0 is name of algorithm: "DES", "DES_EDE3_CBC" or "RC2_CBC".
64  * @param keyLength0 is key size in bits.
65  * @exception SMIMEException thrown in case of invalid algorithm names, or in
66  * case of wrong key sizes in bits.
67  */

68   public SymmetricEncryption (String JavaDoc algorithmName0, int keyLength0) throws SMIMEException
69   {
70     AlgorithmChecker alg = new AlgorithmChecker(algorithmName0, keyLength0);
71     symmetricKeyLength = alg.getKeySize();
72     algorithmName = alg.getAlgorithmName();
73   }
74
75 /**
76  * Encryption of the byte array with random generated key
77  * @param forEncrypt0 is byte array for encryption
78  * @exception SMIMEException caused by non SMIMEException which can be one of
79  * the following: NoSuchPaddingException, NoSuchProviderException,
80  * NoSuchAlgorithmException, InvalidKeyException, BadPaddingException,
81  * IllegalBlockSizeException.
82  */

83   public void encrypt (byte[] forEncrypt0) throws SMIMEException {
84     try {
85       SecureRandom JavaDoc rand = SecureRandom.getInstance("SHA1PRNG", "SUN");
86       KeyGenerator keyGen = KeyGenerator.getInstance(algorithmName, "BC"); // Construct key and cipher for Symmetric Cipher and choosed algorithm
87
keyGen.init(symmetricKeyLength);
88       Key JavaDoc 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 JavaDoc e) {
96       throw SMIMEException.getInstance(this, e, "encryption" );
97     }
98   }
99
100 /**
101  * Encryption of the byte array with the given symmetric key
102  * @param forEncrypt0 is byte array for encryption
103  * @param simKey0 symmetric key
104  * @exception SMIMEException if number of bytes in imported symmetric Key is
105  * incorect. Also, can be caused by non SMIMEException which can be one of the following:
106  * NoSuchPaddingException, NoSuchProviderException, NoSuchAlgorithmException,
107  * InvalidKeyException, BadPaddingException, IllegalBlockSizeException.
108  */

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 JavaDoc 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 JavaDoc e) {
122       throw SMIMEException.getInstance(this, e, "encryption" );
123     }
124   }
125
126 /**
127  * Returns the algorithm name used for symmetric encryption.
128  * @return Name of the chosen algorithm for symmetric encryption.
129  */

130   public String JavaDoc getAlgorithmName () {
131     return algorithmName;
132   }
133
134 /**
135  * Returns the key size in bits used for chosen algorithm name.
136  * @return Key size in bits for the chosen algorithm for symmetric
137  * encryption.
138  */

139   public int getKeyLength () {
140     return symmetricKeyLength;
141   }
142
143 /**
144  * Returns the initialization vector (IV) used for Symmetric Cipher and
145  * the chosen algorithm.
146  * @return IV as a byte array.
147  */

148   public byte[] getIV () {
149     return iv;
150   }
151
152 /**
153  * Returns the generated symmetric key in bytes.
154  * @return Symmetric key in byte array.
155  */

156   public byte[] getSymmetricKey () {
157     return symmetricKey;
158   }
159
160 /**
161  * Returns the encrypted content.
162  * @return Encrypted content as byte array.
163  */

164   public byte[] getEncryptedValue () {
165     return encryptedValue;
166   }
167
168 /**
169  * Decription of the byte array with the symmetric key and IV previously
170  * generated by the process of encryption in the same session with the same
171  * object. This method is mainly used in testing purposes.
172  * @param forDecrypt previosly encrypted byte array
173  * @exception SMIMEException caused by non SMIMEException which can be one of the following:
174  * NoSuchPaddingException, NoSuchProviderException, NoSuchAlgorithmException,
175  * InvalidKeyException, BadPaddingException, IllegalBlockSizeException.
176  */

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 JavaDoc e) {
185       throw SMIMEException.getInstance(this, e, "decryption" );
186     }
187   }
188
189 /**
190  * Decription of the byte array with the given symmetric key and
191  * initialization vector (IV)
192  * @param forDecrypt byte array for encryption
193  * @param simKey0 imported symetric key
194  * @param iv0 imported initialization vector
195  * @exception SMIMEException if number of bytes in imported symmetric key is
196  * incorect, or if number of bytes in imported IV isn't 8. Also, can be caused
197  * by non SMIMEException which can be one of the following:
198  * NoSuchPaddingException, NoSuchProviderException, NoSuchAlgorithmException,
199  * InvalidKeyException, BadPaddingException, IllegalBlockSizeException.
200  */

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 JavaDoc e) {
213       throw SMIMEException.getInstance(this, e, "decryption" );
214     }
215   }
216
217 /**
218  * Returns the decrypted content
219  * @return Decrypted content as a byte array.
220  */

221   public byte[] getDecryptedValue () {
222     return decryptedValue;
223   }
224
225 /**
226  * Resets (sets to null) all private attributes in generated instance of
227  * the class.
228  */

229   public void reset () {
230     iv = null;
231     symmetricKey = null;
232     symmetricKeyLength = 0;
233     String JavaDoc algorithmName = null;
234     encryptedValue = null;
235     decryptedValue = null;
236   }
237 }
238
239
240
241
Popular Tags