***********************************************************************************ASSIGNMENT
NUMBER : 1
ASSIGNMENT NAME : Write program in C++or Java to implement RSA algorithm for key generation and cipher
verification
NAME : Ashwini S. Patil
ROLL NO : 20
BATCH : A DATE :
***********************************************************************************============
==========Program Code: (Number)====================
//package rsa;
import java.io.*;
import java.util.*;
class Rsa
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int p,q,n,phi,d=0,e,i;
System.out.println("Enter the number to be encrypted and decrypted");
int msg=sc.nextInt();
double c,msgback;
System.out.println("Enter 1st prime number p");
p=sc.nextInt();
System.out.println("Enter 2nd prime number q");
q=sc.nextInt();
n=p*q;
phi=(p-1)*(q-1);
System.out.println("the value of phi ="+phi);
for(e=2;e<phi;e++)
{
if(gcd(e,phi)==1)
{
break;
}
}
System.out.println("the value of e = "+e);
for(i=0;i<=9;i++)
{
int x=1+(i*phi);
if(x%e==0)
{
d=x/e;
break;
}
}
System.out.println("the value of d = "+d);
c=(Math.pow(msg,e))%n;
System.out.println("Encrypted meassage is :-");
System.out.println(c);
msgback=(Math.pow(c,d))%n;
System.out.println("Decrypted meassage is :-");
System.out.println(msgback);
}
static int gcd(int e,int phi)
{
if(e==0)
return phi;
else
return gcd(phi%e,e);
}
}
===========================OUTPUT=============================
Enter the number to be encrypted and decrypted
4
Enter 1st prime number p
Enter 2nd prime number q
the value of phi =12
the value of e = 5
the value of d = 5
Encrypted meassage is :-
16.0
Decrypted meassage is :-
4.0
________________________________________________________________________
===========================Program Code: (String)==================
import java.io.DataInputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.util.Random;
public class rsa_algo_string {
private BigInteger p;
private BigInteger q;
private BigInteger N;
private BigInteger phi;
private BigInteger e;
private BigInteger d;
private int bitlength = 1024;
private Random r;
public rsa_algo_string()
{
r = new Random();
p = BigInteger.probablePrime(bitlength, r);
q = BigInteger.probablePrime(bitlength, r);
N = p.multiply(q);
phi = p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE));
e = BigInteger.probablePrime(bitlength / 2, r);
while (phi.gcd(e).compareTo(BigInteger.ONE) > 0 && e.compareTo(phi) < 0)
{
e.add(BigInteger.ONE);
}
d = e.modInverse(phi);
}
public rsa_algo_string(BigInteger e, BigInteger d, BigInteger N)
{
this.e = e;
this.d = d;
this.N = N;
}
public static void main(String[] args) throws IOException
{
rsa_algo_string rsa = new rsa_algo_string();
DataInputStream in = new DataInputStream(System.in);
String teststring;
System.out.println("Enter the plain text:");
teststring = in.readLine();
System.out.println("Encrypting String: " + teststring);
System.out.println("String in Bytes: "
+ bytesToString(teststring.getBytes()));
// encrypt
byte[] encrypted = rsa.encrypt(teststring.getBytes());
System.out.println("encrypted String: " + new String(encrypted));
// decrypt
byte[] decrypted = rsa.decrypt(encrypted);
System.out.println("Decrypting Bytes: " + bytesToString(decrypted));
System.out.println("Decrypted String: " + new String(decrypted));
}
private static String bytesToString(byte[] encrypted)
{
String test = "";
for (byte b:encrypted)
{
test += Byte.toString(b);
}
return test;
}
// Encrypt message
public byte[] encrypt(byte[] message)
{
return (new BigInteger(message)).modPow(e, N).toByteArray();
}
// Decrypt message
public byte[] decrypt(byte[] message)
{
return (new BigInteger(message)).modPow(d, N).toByteArray();
}
}
==================================OUTPUT===============================
Enter the plain text:
Info Tech
Encrypting String:Info Tech
String in Bytes: 77698445667567
encrypted String: p Ѓb ] `Ͼ: no 챑 1\f Eԁ ': z ' &[@Ԣ)B6Jl / 拘 d }"j ˖h J Ǝ a G* Z Y^ l 2 اH R ` B?
6 eaR Ak 85 F
Decrypting Bytes: 77698445667567
Decrypted String:Info Tech