View Javadoc
1 /* 2 * Title: S/MIME Project 3 * Description: S/MIME email sending capabilities 4 * @Author Vladan Obradovic 5 * @Version 2.0.1 6 */ 7 8 9 package org.webdocwf.util.smime.crypto; 10 11 12 import java.security.Key; 13 import javax.crypto.Cipher; 14 import org.webdocwf.util.smime.exception.SMIMEException; 15 import org.webdocwf.util.smime.exception.ErrorStorage; 16 17 18 /*** 19 * AsymmetricEncryption class is used for asymmetric encryption of the small 20 * parts of data (for exsample to encrypt symmetric key genereted in the class 21 * SymmetricEncryption). Performed algorithm is RSA/ECB/PKCS1Padding.<BR> 22 *<BR> 23 * In order to create a Cipher object, the application calls the Cipher's 24 * getInstance method, and passes the name of the requested transformation to it. 25 * The name of a provider may be specified. 26 */ 27 public class AsymmetricEncryption { 28 29 /*** 30 * Container for encrypted value. 31 */ 32 private byte[] encryptContent = null; 33 34 /*** 35 * Container for decrypted value. 36 */ 37 private byte[] decryptContent = null; 38 39 /*** 40 * Default constructor 41 */ 42 public AsymmetricEncryption() {} 43 44 /*** 45 * Perform RSA encryption of the input byte array with the private or public key. 46 * @param key0 private or public key 47 * @param forEncrypt0 content for encryption 48 * @exception SMIMEException caused by non SMIMEException which can be one of 49 * the following: NoSuchPaddingException, NoSuchProviderException, 50 * NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, 51 * IllegalBlockSizeException. 52 */ 53 public void encryption(Key key0, byte[] forEncrypt0) throws SMIMEException { 54 try { 55 Cipher c = Cipher.getInstance("RSA/ECB/PKCS1Padding", "BC"); 56 57 c.init(Cipher.ENCRYPT_MODE, key0); 58 encryptContent = c.doFinal(forEncrypt0); 59 } catch (Exception e) { 60 throw SMIMEException.getInstance(this, e, "encryption"); 61 } 62 } 63 64 /*** 65 * Returns encrypted value. To work properly encryption must be first performed. 66 * @return Encrypted value represented as byte array. 67 */ 68 public byte[] getEncryptValue() { 69 return encryptContent; 70 } 71 72 /*** 73 * Perform RSA decryption of the input byte array with the public or private key. 74 * @param key0 private or public key 75 * @param forDecrypt0 content for decryption 76 * @exception SMIMEException caused by non SMIMEException which can be one of the following: 77 * NoSuchPaddingException, NoSuchProviderException, NoSuchAlgorithmException, 78 * InvalidKeyException, BadPaddingException, IllegalBlockSizeException. 79 */ 80 public void decryption(Key key0, byte[] forDecrypt0) throws SMIMEException { 81 try { 82 Cipher c = Cipher.getInstance("RSA/ECB/PKCS1Padding", "BC"); 83 84 c.init(Cipher.DECRYPT_MODE, key0); 85 decryptContent = c.doFinal(forDecrypt0); 86 } catch (Exception e) { 87 throw SMIMEException.getInstance(this, e, "decryption"); 88 } 89 } 90 91 /*** 92 * Returns the decrypted value. To work properly, decryption must be first 93 * performed. 94 * @return Decrypted value represented as byte array. 95 */ 96 public byte[] getDecryptValue() { 97 return decryptContent; 98 } 99 100 /*** 101 * Resets (sets to null) all private attributes in the generated instance 102 * of the class. 103 */ 104 void reset() { 105 encryptContent = null; 106 decryptContent = null; 107 } 108 }

This page was automatically generated by Maven