-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathelGamaImplementation.java
More file actions
99 lines (80 loc) · 3.44 KB
/
elGamaImplementation.java
File metadata and controls
99 lines (80 loc) · 3.44 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
* Author: fullarray
* Description: This program was built using the following Java version:
* java version "1.8.0_141"
* Java(TM) SE Runtime Environment (build 1.8.0_141-b15)
* Java HotSpot(TM) 64-Bit Server VM (build 25.141-b15, mixed mode)
*
*/
import java.math.*;
import java.util.*;
import java.lang.String;
import java.security.*;
import java.io.*;
public class elGamaImplementation
{
public static void main(String[] args) throws IOException
{
BigInteger privateKey;
Random sc = new SecureRandom();
// Get key size from user.
System.out.print("Enter your key size: E.g. 64. 128, 256, etc.\n");
int keySize = elGamaImplementation.GetInt();
while(keySize != 64 && keySize != 128 && keySize != 256){
System.out.println("Please enter a correct bit size. For example: 64, 128, 256.");
System.out.println("Try again.\n");
System.out.print("Enter your key size: E.g. 64. 128, 256, etc.\n");
keySize = elGamaImplementation.GetInt();
}
// For this example, private key is created randomly
privateKey = new BigInteger(keySize,sc);
System.out.println("Generated private key: \n" + privateKey +"\n");
//ElGama
ProcessAlgorithm(keySize, sc, privateKey);
}
public static void ProcessAlgorithm(int keySize, Random rand, BigInteger privateKey ) throws IOException{
//Variables
BigInteger alpha, beta, c;
/*
* Based on key bit size, define a large prime number
* as well as generate public key.
*/
alpha = BigInteger.probablePrime(keySize, rand);
beta = new BigInteger(keySize, rand);
c = beta.modPow(privateKey, alpha);
System.out.println("alpha: \n" + alpha );
System.out.println("beta: \n" + beta );
System.out.println("c: \n" + c +"\n");
// Get plain text
System.out.print("Enter plain text message: E.g. 2345245245425 \n");
String s = elGamaImplementation.GetString();
// Process encryption
BigInteger x = new BigInteger(s);
BigInteger r = new BigInteger(keySize, rand);
BigInteger EC = x.multiply(c.modPow(r, alpha)).mod(alpha);
BigInteger brmodp = beta.modPow(r, alpha);
System.out.println("Plaintext: \n" + x + "\n\n");
System.out.println("Initializing keys and encrypting.");
System.out.println("-----------------------");
System.out.println("r Value: \n" + r);
System.out.println("EC Value: \n" + EC);
System.out.println("Encrypted: \n" + brmodp +"\n\n");
// Process decryption
BigInteger crmodp = brmodp.modPow(privateKey, alpha);
BigInteger d = crmodp.modInverse(alpha);
BigInteger ad = d.multiply(EC).mod(alpha);
System.out.println("Calculating new keys and performing decryption. ");
System.out.println("-----------------------");
System.out.println("d Value: \n" + crmodp);
System.out.println("Ad Value: \n" + d);
System.out.println("Original plain text Message: \n" + ad + "\n");
}
public static String GetString() throws IOException {
BufferedReader stringIn = new BufferedReader (new InputStreamReader(System.in));
return stringIn.readLine();
}
public static int GetInt( ) throws IOException {
String aux = GetString();
return Integer.parseInt(aux);
}
}