KickJava   Java API By Example, From Geeks To Geeks.

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


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.Key JavaDoc;
12 import javax.crypto.Cipher;
13 import org.enhydra.oyster.exception.SMIMEException;
14 import org.enhydra.oyster.exception.ErrorStorage;
15
16
17 /**
18  * AsymmetricEncryption class is used for asymmetric encryption of the small
19  * parts of data (for exsample to encrypt symmetric key genereted in the class
20  * SymmetricEncryption). Performed algorithm is RSA/ECB/PKCS1Padding.<BR>
21  *<BR>
22  * In order to create a Cipher object, the application calls the Cipher's
23  * getInstance method, and passes the name of the requested transformation to it.
24  * The name of a provider may be specified.
25  */

26 public class AsymmetricEncryption {
27
28 /**
29  * Container for encrypted value.
30  */

31   private byte[] encryptContent = null;
32
33 /**
34  * Container for decrypted value.
35  */

36   private byte[] decryptContent = null;
37
38
39 /**
40  * Default constructor
41  */

42   public AsymmetricEncryption () {
43   }
44
45 /**
46  * Perform RSA encryption of the input byte array with the private or public key.
47  * @param key0 private or public key
48  * @param forEncrypt0 content for encryption
49  * @exception SMIMEException caused by non SMIMEException which can be one of
50  * the following: NoSuchPaddingException, NoSuchProviderException,
51  * NoSuchAlgorithmException, InvalidKeyException, BadPaddingException,
52  * IllegalBlockSizeException.
53  */

54   public void encrypt (Key JavaDoc key0, byte[] forEncrypt0) throws SMIMEException{
55     try {
56       Cipher c = Cipher.getInstance("RSA/ECB/PKCS1Padding", "BC");
57       c.init(Cipher.ENCRYPT_MODE, key0);
58       encryptContent = c.doFinal(forEncrypt0);
59     }
60     catch(Exception JavaDoc e) {
61       throw SMIMEException.getInstance(this, e, "encryption" );
62     }
63   }
64
65 /**
66  * Returns encrypted value. To work properly encryption must be first performed.
67  * @return Encrypted value represented as byte array.
68  */

69   public byte[] getEncryptedValue () {
70     return encryptContent;
71   }
72
73 /**
74  * Perform RSA decryption of the input byte array with the public or private key.
75  * @param key0 private or public key
76  * @param forDecrypt0 content for decryption
77  * @exception SMIMEException caused by non SMIMEException which can be one of the following:
78  * NoSuchPaddingException, NoSuchProviderException, NoSuchAlgorithmException,
79  * InvalidKeyException, BadPaddingException, IllegalBlockSizeException.
80  */

81   public void decrypt (Key JavaDoc key0, byte[] forDecrypt0) throws SMIMEException{
82     try {
83       Cipher c = Cipher.getInstance("RSA/ECB/PKCS1Padding", "BC");
84       c.init(Cipher.DECRYPT_MODE, key0);
85       decryptContent = c.doFinal(forDecrypt0);
86     }
87     catch(Exception JavaDoc e) {
88       throw SMIMEException.getInstance(this, e, "decryption" );
89     }
90   }
91
92 /**
93  * Returns the decrypted value. To work properly, decryption must be first
94  * performed.
95  * @return Decrypted value represented as byte array.
96  */

97   public byte[] getDecryptedValue () {
98     return decryptContent;
99   }
100
101 /**
102  * Resets (sets to null) all private attributes in the generated instance
103  * of the class.
104  */

105   void reset () {
106     encryptContent = null;
107     decryptContent = null;
108   }
109 }
110
Popular Tags