-
Notifications
You must be signed in to change notification settings - Fork 0
/
RSA_Algorithm.java
66 lines (48 loc) · 2.2 KB
/
RSA_Algorithm.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import java.math.BigInteger;
import java.security.SecureRandom;
public class RSA_Algorithm {
private BigInteger privateKey;
private BigInteger publicKey;
private BigInteger modulus;
// Key size in bits (for demonstration purposes; should be larger in practice)
private static final int KEY_SIZE = 512;
// Secure random number generator
private static final SecureRandom random = new SecureRandom();
public RSA_Algorithm() {
// Generate key pair
generateKeyPair();
}
private void generateKeyPair() {
// Step 1: Choose two distinct large random prime numbers, p and q
BigInteger p = BigInteger.probablePrime(KEY_SIZE, random);
BigInteger q = BigInteger.probablePrime(KEY_SIZE, random);
// Step 2: Compute n = pq
modulus = p.multiply(q);
// Step 3: Compute the totient (ϕ) of n: ϕ(n) = (p-1)(q-1)
BigInteger totient = p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE));
// Step 4: Choose a public exponent 'e' such that 1 < e < ϕ(n) and e is coprime to ϕ(n)
publicKey = new BigInteger("65537"); // Commonly used value for e
// Step 5: Compute the private exponent 'd' as the modular multiplicative inverse of 'e' mod ϕ(n)
privateKey = publicKey.modInverse(totient);
}
public BigInteger encrypt(BigInteger message) {
// Encryption: C ≡ M^e mod n
return message.modPow(publicKey, modulus);
}
public BigInteger decrypt(BigInteger ciphertext) {
// Decryption: M ≡ C^d mod n
return ciphertext.modPow(privateKey, modulus);
}
public static void main(String[] args) {
RSA_Algorithm rsa = new RSA_Algorithm();
// Message to be encrypted
BigInteger message = new BigInteger("2");
// Encryption
BigInteger encryptedMessage = rsa.encrypt(message);
System.out.println("Encrypted Message: " + encryptedMessage);
System.out.println("")
// Decryption
BigInteger decryptedMessage = rsa.decrypt(encryptedMessage);
System.out.println("Decrypted Message: " + decryptedMessage);
}
}