Introduction

RSA is a cryptosystem, which is known as one of the first practicable public-key cryptosystems and is widely used for secure data transmission. In such a cryptosystem, the encryption key is public and differs from the decryption key which is kept secret. In RSA, this asymmetry is based on the practical difficulty of factoring the product of two large prime numbers, the factoring problem. RSA stands for Ron Rivest, Adi Shamir and Leonard Adleman, who first publicly described the algorithm in 1977.

For more information please refer to wiki
Here I am going to give an example for encryption and decryption mechanism in java using RSA algorithm.

First create the RSAConstants java class which will be used for holding the various constants used in this coding.

package in.sblog.rsa.utils;

public final class RSAConstants {

    private RSAConstants() throws CloneNotSupportedException {
        throw new CloneNotSupportedException("Instantiation not supported!");
    }

    public static final String ALGORITHM = "RSA";
    public static final int ALGORITHM_BITS = 2048;
}

 

Now create the RSA KeyPair generator class which will be further used for encryption and decryption.

package in.sblog.rsa.keys;

import in.sblog.rsa.utils.RSAConstants;

import java.security.KeyPair;
import java.security.KeyPairGenerator;

public class RSAKeyPair {

    public static KeyPair keyPairRSA() {
        KeyPairGenerator generator = null;
        try {
            generator = KeyPairGenerator.getInstance(RSAConstants.ALGORITHM);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (generator != null) {
            generator.initialize(RSAConstants.ALGORITHM_BITS);
            KeyPair keyPair = generator.genKeyPair();
            return keyPair;
        }
        return null;
    }

}

 

Now create the logic for encryption and decryption

package in.sblog.rsa.encdec;

import in.sblog.rsa.utils.RSAConstants;

import java.security.Key;

import javax.crypto.Cipher;

public class RSAEncDec {
    public static byte[] encrypt(String original, Key privateKey) {
        if (original != null && privateKey != null) {
            byte[] bs = original.getBytes();
            byte[] encData = convert(bs, privateKey, Cipher.ENCRYPT_MODE);
            return encData;
        }
        return null;
    }

    public static byte[] decrypt(byte[] encrypted, Key publicKey) {
        if (encrypted != null && publicKey != null) {
            byte[] decData = convert(encrypted, publicKey, Cipher.DECRYPT_MODE);
            return decData;
        }
        return null;
    }

    private static byte[] convert(byte[] data, Key key, int mode) {
        try {
            Cipher cipher = Cipher.getInstance(RSAConstants.ALGORITHM);
            cipher.init(mode, key);
            byte[] newData = cipher.doFinal(data);
            return newData;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

 

Now for testing purpose create a main class and test the above logic.

package in.sblog.rsa.test;

import in.sblog.rsa.encdec.RSAEncDec;
import in.sblog.rsa.keys.RSAKeyPair;

import java.security.Key;
import java.security.KeyPair;
import java.util.Arrays;

public class TestRSA {

    public static void main(String[] args) {
        String password = "password";
        KeyPair keyPair = RSAKeyPair.keyPairRSA();
        Key publicKey = keyPair.getPublic();
        Key privateKey = keyPair.getPrivate();

        System.out.println("Encrypt Start");
        System.out.println("Original: " + password);
        byte[] encrypted = RSAEncDec.encrypt(password, privateKey);
        System.out.println("Encrypted: " + new String(encrypted));
        System.out.println("Encrypt End");

        System.out.println();

        System.out.println("Decrypt Start");
        byte[] decrypted = RSAEncDec.decrypt(encrypted, publicKey);
        System.out.println("Decrypted: " + new String(decrypted));
        System.out.println("Decrypted matches Original: "
                + Arrays.equals(decrypted, password.getBytes()));
        System.out.println("Decrypt End");
    }

}

 

Thanks for your reading. Please do not forget to leave a comment.

I am a professional Web developer, Enterprise Application developer, Software Engineer and Blogger. Connect me on Roy Tutorials Twitter Facebook  Google Plus Linkedin Or Email Me

Leave a Reply

Your email address will not be published. Required fields are marked *