Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
17 views6 pages

Exp 3

The document contains two Java programs implementing cryptographic algorithms: the Hill Cipher and RSA encryption. The Hill Cipher program encrypts and decrypts a message using a predefined matrix, while the RSA program generates keys based on two prime numbers and allows for message encryption and decryption. Both programs utilize user input for plaintext and prime numbers, respectively.

Uploaded by

garisharmaa13
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views6 pages

Exp 3

The document contains two Java programs implementing cryptographic algorithms: the Hill Cipher and RSA encryption. The Hill Cipher program encrypts and decrypts a message using a predefined matrix, while the RSA program generates keys based on two prime numbers and allows for message encryption and decryption. Both programs utilize user input for plaintext and prime numbers, respectively.

Uploaded by

garisharmaa13
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Experiment No: 3

Aim: Write a program to implement Hill Cipher.


Source Code:

import java.util.Scanner;

public class HillCipher {


public static void main(String[] args) {
int[][] a = {
{6, 24, 1},
{13, 16, 10},
{20, 17, 15}
};

int[][] b = {
{8, 5, 10},
{21, 8, 21},
{21, 12, 8}
};

Scanner scanner = new Scanner(System.in);


System.out.println("Enter plain text (3 uppercase letters): ");
String msg = scanner.next().toUpperCase();

int[] c = new int[3];


int[] d = new int[3];

for (int i = 0; i < msg.length(); i++) {


c[i] = msg.charAt(i) - 'A';
System.out.print(c[i] + " ");
}

System.out.print("\nEncrypted Cipher Text: ");


for (int i = 0; i < 3; i++) {
int t = 0;
for (int j = 0; j < 3; j++) {
t += a[i][j] * c[j];
}
d[i] = t % 26;
System.out.print((char) (d[i] + 'A') + " ");
}

System.out.print("\nDecrypted Cipher Text: ");


for (int i = 0; i < 3; i++) {
int t = 0;
for (int j = 0; j < 3; j++) {
t += b[i][j] * d[j];
}
c[i] = t % 26;
System.out.print((char) (c[i] + 'A') + " ");
}

scanner.close();
}
}
Output:
Experiment No: 7
Aim: WAP to implement RSA encryption algorithm.

Source Code:

import java.util.Scanner;
import java.math.BigInteger;
import java.util.Random;

public class RSAEncryption {


private BigInteger p, q, n, t, e, d;
private BigInteger[] encrypted;
private String message;
private Random rand;

public RSAEncryption() {
rand = new Random();
}

private boolean isPrime(BigInteger num) {


return num.isProbablePrime(1);
}

public void generateKeys(BigInteger p, BigInteger q) {


if (!isPrime(p) || !isPrime(q) || p.equals(q)) {
throw new IllegalArgumentException("Invalid prime numbers");
}
this.p = p;
this.q = q;
n = p.multiply(q);
t = (p.subtract(BigInteger.ONE)).multiply(q.subtract(BigInteger.ONE));
e = BigInteger.probablePrime(16, rand);
while (t.gcd(e).compareTo(BigInteger.ONE) > 0 && e.compareTo(t) < 0) {
e = e.add(BigInteger.ONE);
}
d = e.modInverse(t);
}

public BigInteger[] encrypt(String message) {


this.message = message;
encrypted = new BigInteger[message.length()];
for (int i = 0; i < message.length(); i++) {
BigInteger m = BigInteger.valueOf((int) message.charAt(i));
encrypted[i] = m.modPow(e, n);
}
return encrypted;
}

public String decrypt(BigInteger[] encryptedMessage) {


StringBuilder decryptedMessage = new StringBuilder();
for (BigInteger c : encryptedMessage) {
BigInteger m = c.modPow(d, n);
decryptedMessage.append((char) m.intValue());
}
return decryptedMessage.toString();
}

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
RSAEncryption rsa = new RSAEncryption();

System.out.println("Enter first prime number:");


BigInteger p = scanner.nextBigInteger();

System.out.println("Enter second prime number:");


BigInteger q = scanner.nextBigInteger();

rsa.generateKeys(p, q);

System.out.println("Enter the message:");


scanner.nextLine();
String message = scanner.nextLine();

BigInteger[] encryptedMessage = rsa.encrypt(message);


System.out.println("Encrypted Message:");
for (BigInteger b : encryptedMessage) {
System.out.print(b + " ");
}
System.out.println();

String decryptedMessage = rsa.decrypt(encryptedMessage);


System.out.println("Decrypted Message: " + decryptedMessage);
scanner.close();
}
}
Output:

You might also like