001: /*
002: * Title: Oyster Project
003: * Description: S/MIME email sending capabilities
004: * @Author Vladan Obradovic, Vladimir Radisic
005: * @Version 2.1.6
006: */
007:
008: package org.enhydra.oyster.crypto;
009:
010: import java.security.Key;
011: import javax.crypto.Cipher;
012: import org.enhydra.oyster.exception.SMIMEException;
013: import org.enhydra.oyster.exception.ErrorStorage;
014:
015: /**
016: * AsymmetricEncryption class is used for asymmetric encryption of the small
017: * parts of data (for exsample to encrypt symmetric key genereted in the class
018: * SymmetricEncryption). Performed algorithm is RSA/ECB/PKCS1Padding.<BR>
019: *<BR>
020: * In order to create a Cipher object, the application calls the Cipher's
021: * getInstance method, and passes the name of the requested transformation to it.
022: * The name of a provider may be specified.
023: */
024: public class AsymmetricEncryption {
025:
026: /**
027: * Container for encrypted value.
028: */
029: private byte[] encryptContent = null;
030:
031: /**
032: * Container for decrypted value.
033: */
034: private byte[] decryptContent = null;
035:
036: /**
037: * Default constructor
038: */
039: public AsymmetricEncryption() {
040: }
041:
042: /**
043: * Perform RSA encryption of the input byte array with the private or public key.
044: * @param key0 private or public key
045: * @param forEncrypt0 content for encryption
046: * @exception SMIMEException caused by non SMIMEException which can be one of
047: * the following: NoSuchPaddingException, NoSuchProviderException,
048: * NoSuchAlgorithmException, InvalidKeyException, BadPaddingException,
049: * IllegalBlockSizeException.
050: */
051: public void encrypt(Key key0, byte[] forEncrypt0)
052: throws SMIMEException {
053: try {
054: Cipher c = Cipher.getInstance("RSA/ECB/PKCS1Padding", "BC");
055: c.init(Cipher.ENCRYPT_MODE, key0);
056: encryptContent = c.doFinal(forEncrypt0);
057: } catch (Exception e) {
058: throw new SMIMEException(e);
059: }
060: }
061:
062: /**
063: * Returns encrypted value. To work properly encryption must be first performed.
064: * @return Encrypted value represented as byte array.
065: */
066: public byte[] getEncryptedValue() {
067: return encryptContent;
068: }
069:
070: /**
071: * Perform RSA decryption of the input byte array with the public or private key.
072: * @param key0 private or public key
073: * @param forDecrypt0 content for decryption
074: * @exception SMIMEException caused by non SMIMEException which can be one of the following:
075: * NoSuchPaddingException, NoSuchProviderException, NoSuchAlgorithmException,
076: * InvalidKeyException, BadPaddingException, IllegalBlockSizeException.
077: */
078: public void decrypt(Key key0, byte[] forDecrypt0)
079: throws SMIMEException {
080: try {
081: Cipher c = Cipher.getInstance("RSA/ECB/PKCS1Padding", "BC");
082: c.init(Cipher.DECRYPT_MODE, key0);
083: decryptContent = c.doFinal(forDecrypt0);
084: } catch (Exception e) {
085: throw new SMIMEException(e);
086: }
087: }
088:
089: /**
090: * Returns the decrypted value. To work properly, decryption must be first
091: * performed.
092: * @return Decrypted value represented as byte array.
093: */
094: public byte[] getDecryptedValue() {
095: return decryptContent;
096: }
097:
098: /**
099: * Resets (sets to null) all private attributes in the generated instance
100: * of the class.
101: */
102: void reset() {
103: encryptContent = null;
104: decryptContent = null;
105: }
106: }
|