Write program in C++ or Java to implement RSA algorithm for key generation and cipher verification

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.
 * RSA ALGORITHM performed and executed in NetBeans 7.4

package rsa;
import java.math.*;
import java.security.SecureRandom;

 * @author Professional Cipher[www.professionalcipher.blogspot.com]
public class RSA {
    private static BigInteger one=new BigInteger("1");
    private static BigInteger n;
    private static BigInteger phi;
    private static BigInteger e;
    private static BigInteger d;
    public RSA()
        BigInteger p=BigInteger.probablePrime(40, new SecureRandom());
        BigInteger q=BigInteger.probablePrime(40, new SecureRandom());
        e=new BigInteger("65537"); //only use prime number
    public BigInteger encrypt(BigInteger msg)
        return msg.modPow(e, n);
    public BigInteger decrypt(BigInteger msg)
        return msg.modPow(d, n);

    public String toString() {
        return "Public key="+e.longValue()+"\nPrivate key="+d.longValue()+"\nModuli="+n.longValue();
    public static void main(String[] args) {
        RSA rsa=new RSA();
        String msg="BEIT";
        BigInteger plaintext=new BigInteger(msg.getBytes()); 
        BigInteger ciphertext=rsa.encrypt(plaintext); 
        System.out.println("Encrypted message="+new String(ciphertext.toByteArray()));
        BigInteger dplaintext=rsa.decrypt(ciphertext); 
        System.out.println("Decrypted message="+new String(dplaintext.toByteArray()));

Public key=65537
Private key=-7970827358922742135
Encrypted message='��R�2��] 
Decrypted message=BEIT
BUILD SUCCESSFUL (total time: 0 seconds)

Write program in C++ or Java to implement RSA algorithm for key generation and cipher verification Write program in C++ or Java to implement RSA algorithm for key generation and cipher verification Reviewed by Huzaif Sayyed on July 20, 2017 Rating: 5


  1. Thanks for your extraordinary blog. Your idea for this was so brilliant. This would provide people with an excellent tally resource from someone who has experienced such issues. You would be coming at the subject from a different angle and people would appreciate your honesty and frankness. Good luck for your next blog!
    Tally ERP 9 Training
    tally classes
    Tally Training institute in Chennai
    Tally course in Chennai
    seo training classes
    seo training course
    seo training institute in chennai
    seo training institutes
    seo courses in chennai
    seo institutes in chennai
    seo classes in chennai
    seo training center in chennai


Powered by Blogger.