Java - RSA Algorithm

/*RSAAlgorithm.java*/ 
/*RSA stands for Rivest, Shamir and Adleman who first publicly described it*/

import java.security.InvalidKeyException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;

/**
 *
 * @author dhanoopbhaskar
 */
public class RSAAlgorithm {
   
    private KeyPairGenerator keyPairGenerator = null;
    private PrivateKey privateKey = null;
    private PublicKey publicKey = null;
    Cipher cipher = null;
   
    public RSAAlgorithm() {
        try {
            /**
             * Create RSA key pairs
             * - private key
             * - public key
             */
            keyPairGenerator = KeyPairGenerator.getInstance("RSA");
            keyPairGenerator.initialize(512); //512 is the key size
            KeyPair keyPair = keyPairGenerator.generateKeyPair();
            privateKey = keyPair.getPrivate();
            publicKey = keyPair.getPublic();
            /**
             * Create an instance of cipher
             */
            cipher = Cipher.getInstance("RSA");
        } catch (NoSuchPaddingException ex) {
            System.out.println(ex);
        } catch (NoSuchAlgorithmException ex) {
            System.out.println(ex);
        }
    }
   
    /**
     *
     * @param plainText
     * @return cipherText
     */
    public byte[] encryptText(String plainText) {
        byte[] cipherText = null;
        try {
            /**
             * Initialize the cipher for encryption
             */
            cipher.init(Cipher.ENCRYPT_MODE, publicKey);
            /**
             * Convert the text string to byte format
             */
            byte[] plainBytes = plainText.getBytes();
            /**
             * Perform encryption with method doFinal()
             */
            cipherText = cipher.doFinal(plainBytes);           
        } catch (IllegalBlockSizeException ex) {
            System.out.println(ex);
        } catch (BadPaddingException ex) {
            System.out.println(ex);
        } catch (InvalidKeyException ex) {
            System.out.println(ex);
        }
       
        return cipherText;
    }
   
    /**
     *
     * @param cipherText
     * @return plainText
     */
    public String decryptText(byte[] cipherText) {
        String plainText = null;
        try {
            /**
             * Initialize the cipher for decryption
             */
            cipher.init(Cipher.DECRYPT_MODE, privateKey);           
            /**
             * Perform decryption with method doFinal()
             */
            byte[] plainBytes = cipher.doFinal(cipherText);
            /**
             * Convert encrypted text to string format
             */
            plainText = new String(plainBytes);
        } catch (IllegalBlockSizeException ex) {
            System.out.println(ex);
        } catch (BadPaddingException ex) {
            System.out.println(ex);
        } catch (InvalidKeyException ex) {
            System.out.println(ex);
        }
       
        return plainText;
    }
   
    public static void main(String[] args) {
        RSAAlgorithm rsaAlgorithm = new RSAAlgorithm();
        String textToEncrypt = "RSA Algorithm";
        System.out.println("Text before Encryption: " + textToEncrypt);
        byte[] cipherText = rsaAlgorithm.encryptText(textToEncrypt);
        System.out.println("Cipher Text [bytes]: " + cipherText);       
        String textDecrypted = rsaAlgorithm.decryptText(cipherText);
        System.out.println("Text after Decryption: " + textDecrypted);
    }
}
 

Post a Comment

0 Comments