29 June 2010

Implementation of RSA encryption/decryption algorithm in java

Introduction:-RSA is basically an asymmetric encryption /decryption algorithm. It was proposed by Rivest, Shamir, and Adleman.
It is asymmetric in the sense, that here public key distributed to all through which one can encrypt the message and private key which is used for decryption is kept secret and is not shared to everyone .
Here in order to accomplish the task we require following
2:-public key
3:-Private key

a ) For Encryption
First consider two random prime no, say p and q
Now find modulus which is equal to N=p * q
We have a message say M.we can generate cipher text through following equation
C=MKP Modulo N
Here C is encrypted text generated
b) For decryption
We will perform following calculation
M=Cks Modulo N
Here Kp and Ks are public and private key respectively
Limitation of RSA algorithm:-
1:-It is suitable for short messages only
The above description is just the basic introduction of RSA .I have developed this program by using some predefined classes provided by java.
So here ,is the implementation of RSA in java

/* Implementation of RSA algorithm in java
Author name:-Manish Goyal
System Name:-HCL
Operating system used:-Window Vista
Language used:-java

This Program will take any string as input then encrypt it and finally decrypt the data
import java.math.BigInteger;
import java.security.*;
import java.security.spec.*;
import java.io.*;
import javax.crypto.Cipher;

public class RSA

public static void main(String args[])
String srci="";
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Please enter any string you want to encrypt");
catch(IOException ioe)
KeyPairGenerator kpg=KeyPairGenerator.getInstance("RSA");

kpg.initialize(512);//initialize key pairs to 512 bits ,you can also take 1024 or 2048 bits

KeyPair kp=kpg.genKeyPair();

PublicKey publi=kp.getPublic();

Cipher cipher = Cipher.getInstance("RSA");

cipher.init(Cipher.ENCRYPT_MODE, publi);

byte[]src=srci.getBytes();//converting source data into byte array

byte[] cipherData = cipher.doFinal(src);//use this method to finally encrypt data

String srco=new String(cipherData);//converting byte array into string
System.out.println("Encrypted data is:-"+srco);

PrivateKey privatei=kp.getPrivate();//Generating private key

Cipher cipheri=Cipher.getInstance("RSA");//Intializing 2nd instance of Cipher class

cipheri.init(Cipher.DECRYPT_MODE, privatei);//Setting to decrypt_mode

byte[] cipherDat = cipheri.doFinal(cipherData);//Finally decrypting data

String decryptdata=new String(cipherDat);
System.out.println("Decrypted data:-"+decryptdata);
catch(Exception  e)
If you have any query related to this then fire it here

1 comment:

  1. Write more, thats all I have to say. Literally, it seems as though you relied on the video to make your point.
    You clearly know what youre talking about, why throw away your intelligence
    on just posting videos to your weblog when you could be
    giving us something enlightening to read?
    Also visit my webpage ... best paid online Jobs