Project Submission:
Following is required for Final Project
Source code of any cryptographic Algorithm RSA.
Source code should contain following operations
a. Key Generation
b. Data (Alphanumeric) Encryption
c. Data (Alphanumeric) Decryption
This part done by me
Project Report with
a. Introduction of the project work
b. Complete details of the Project Cryptographic Algorithm ( RSA )
c. Source Code
import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.*;
class RsaAlgorithm {
public Random r;
public int bitlength = 1024;
public BigInteger p;
public BigInteger q;
public BigInteger N;
public BigInteger phi;
public BigInteger e;
public BigInteger d;
public static String bytesToString(byte[] encryptedString) {
String test = “”;
for (byte b : encryptedString) {
test += Byte.toString(b);
}
return test;
}
// encrypting the message
public byte[] encryption(byte[] message) {
return (new BigInteger(message)).modPow(e, N).toByteArray();
}
// decrypting the message
public byte[] decryption(byte[] message) {
return (new BigInteger(message)).modPow(d, N).toByteArray();
}
public RsaAlgorithm() {
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 RsaAlgorithm(BigInteger e, BigInteger d, BigInteger N) {
this.e = e;
this.d = d;
this.N = N;
}
@SuppressWarnings(“deprecation”)
public static void main(String[] args) throws java.lang.Exception {
String inp;
DataInputStream in = new DataInputStream(System.in);
RsaAlgorithm rsa = new RsaAlgorithm();
System.out.println(“Salem Alhosani – A00049893 (RSA)”);
System.out.println(“Enter the private Message:”);
inp = in.readLine();
System.out.println(“Encrypting the given String: ” + inp);
System.out.println(“String in Bytes: ” + bytesToString(inp.getBytes()));
// encryption is performed first
byte[] encryptedString = rsa.encryption(inp.getBytes());
// decryption
// at second stage
byte[] decryptedString = rsa.decryption(encryptedString);
System.out.println(“Decrypting Bytes: ” + bytesToString(decryptedString));
System.out.println(“Decrypted String recieved : ” + new String(decryptedString));
}
}
d. Screen shots of code execution.