import java.math.*;
import java.io.*;
import java.security.*;
import javax.crypto.*;

public class Test {
public static void main (String[] args) throws NoSuchAlgorithmException, 

InvalidKeyException, IllegalBlockSizeException, NoSuchProviderException, 

BadPaddingException, NoSuchPaddingException {

/* Generate a RSA key pair */

KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
SecureRandom random = SecureRandom.getInstance("SHA1PRNG", "SUN");

keyGen.initialize(512, random);

KeyPair pair = keyGen.generateKeyPair();
PrivateKey priv = pair.getPrivate();
PublicKey pub = pair.getPublic();
//System.out.println("Public is" + pub);
//System.out.println("Private is" + priv); 

/* Create the cipher */
Cipher rsaCipher = Cipher.getInstance("RSA");
rsaCipher.init(Cipher.ENCRYPT_MODE, pub);

// Cleartext
byte[] cleartext = "This is Bilal".getBytes();
System.out.println("the original cleartext is: " + new String(cleartext));
//System.out.println("the original cleartext is: " + cleartext);

// Encrypt the cleartext
byte[] ciphertext = null;
ciphertext = rsaCipher.doFinal(cleartext);
//byte[] ciphertext = rsaCipher.doFinal(cleartext);
//String ciphertext = rsaCipher.doFinal(cleartext);
System.out.println("the encrypted text is: " + new String(ciphertext));

ciphertext = rsaCipher.doFinal(cleartext);
//byte[] ciphertext = rsaCipher.doFinal(cleartext);
//String ciphertext = rsaCipher.doFinal(cleartext);
System.out.println("the encrypted text is: " + new String(ciphertext));

ciphertext = rsaCipher.doFinal(cleartext);
//byte[] ciphertext = rsaCipher.doFinal(cleartext);
//String ciphertext = rsaCipher.doFinal(cleartext);
System.out.println("the encrypted text is: " + new String(ciphertext));

// Initialize the same cipher for decryption
rsaCipher.init(Cipher.DECRYPT_MODE, priv);

// Decrypt the ciphertext
byte[] cleartext1 = rsaCipher.doFinal(ciphertext);
//String cleartext1 = rsaCipher.doFinal(ciphertext);
System.out.println("the final cleartext is: " + new String(cleartext1));
//System.out.println("the final cleartext is: " + cleartext1);
}
}