RSA is algorithm used by modern computers to encrypt and decrypt messages. It is an asymmetric cryptographic algorithm. Asymmetric means that there are two different keys. This is also called public key cryptography, because one of them can be given to everyone. The other key must be kept private. It is based on the fact that finding the factors of an integer is hard (the factoring problem). RSA stands for Ron Rivest, Adi Shamir and Leonard Adleman, who first publicly described it in 1978. A user of RSA creates and then publishes the product of two large prime numbers, along with an auxiliary value, as their public key. The prime factors must be kept secret. Anyone can use the public key to encrypt a message, but with currently published methods, if the public key is large enough, only someone with knowledge of the prime factors can feasibly decode the message.

Program is according to Pune University BE IT Syllabus : Write a program in C++, C# or Java to implement RSA algorithm using Libraries (API).
Point to be remember before executing program.
  1. Create a text file of name ptext.txt and writing something in it eg: Professional Cipher
  2. After executing program it will create two file dtext.txt and ctext.txt where it will have de-cipher text and cipher text respectively.
  3. Create a Java Class with the name of : RSA_USING_API
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
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.Cipher;
import javax.crypto.CipherOutputStream;
import javax.crypto.NoSuchPaddingException;
/*
 * @ author Professional Cipher [www.professionalcipher.com]  
 */
public class RSA_USING_API
{
    public static KeyPair keyPair;
    public static PublicKey publickey;
    public static PrivateKey privateKey;

    public RSA_USING_API() throws NoSuchAlgorithmException
    {
        KeyPairGenerator keyPairGenerator=KeyPairGenerator.getInstance("RSA");
        keyPairGenerator.initialize(2048);
        keyPair=keyPairGenerator.genKeyPair();
        publickey=keyPair.getPublic();
        privateKey=keyPair.getPrivate();
    }
    public void encrypt(String filePath) throws NoSuchAlgorithmException,NoSuchPaddingException, InvalidKeyException,FileNotFoundException,IOException
    {
        Cipher cipher=Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE,publickey);
        FileInputStream fis=new FileInputStream(filePath);
        FileOutputStream fos=new FileOutputStream("H:\\workspace\\ICS\\src\\ctext.txt");
        CipherOutputStream cos=new CipherOutputStream(fos,cipher);
        byte[] bytes=new byte[30];
        int i=0;
        while((i=fis.read(bytes))!=-1)
        {
            cos.write(bytes,0,i);

        }
        cos.close();
    }
    public void decrypyt(String filePath) throws NoSuchAlgorithmException,NoSuchPaddingException,InvalidKeyException,FileNotFoundException,IOException
    {
        Cipher cipher=Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE,privateKey);
        FileInputStream fis=new FileInputStream(filePath);
        FileOutputStream fos=new FileOutputStream("H:\\workspace\\ICS\\src\\dtext.txt");
        CipherOutputStream cos=new CipherOutputStream(fos,cipher);
        byte[] bytes=new byte[30];
        int i=0;
        while((i=fis.read(bytes))!=-1)
        {
            cos.write(bytes,0,i);

        }
        cos.close();

    }


    public static void main(String[] args) throws  NoSuchAlgorithmException,NoSuchPaddingException,InvalidKeyException,IOException
    {
        RSA_USING_API m1=new RSA_USING_API();
        m1.encrypt("H:\\workspace\\ICS\\src\\ptext.txt");
        m1.decrypyt("H:\\workspace\\ICS\\src\\ctext.txt");


    }
}