-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathelGamaImplementation.java
More file actions
75 lines (67 loc) · 2.49 KB
/
elGamaImplementation.java
File metadata and controls
75 lines (67 loc) · 2.49 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
// implemented by fullarray
import java.math.*;
import java.util.*;
import java.lang.String; // this import is optional and may not be needed in some instances.
import java.security.*;
import java.io.*;
public class elGamaImplementation extends Tools
{ public static void main(String[] args) throws IOException
{
BigInteger p, b, c, secretKey;
Random sc = new SecureRandom();
secretKey = new BigInteger("12345678901234567890");
//
// public key calculation
//
System.out.println("secretKey = " + secretKey);
p = BigInteger.probablePrime(64, sc);
b = new BigInteger("3");
c = b.modPow(secretKey, p);
System.out.println("p = " + p);
System.out.println("b = " + b);
System.out.println("c = " + c);
// Encryption
System.out.print("Enter your Big Number message -->");
String s = Tools.GetString();
BigInteger X = new BigInteger(s);
BigInteger r = new BigInteger(64, sc);
BigInteger EC = X.multiply(c.modPow(r, p)).mod(p);
BigInteger brmodp = b.modPow(r, p);
System.out.println("Plaintext = " + X);
System.out.println("r = " + r);
System.out.println("EC = " + EC);
System.out.println("b^r mod p = " + brmodp);
//
// Decryption
//
BigInteger crmodp = brmodp.modPow(secretKey, p);
BigInteger d = crmodp.modInverse(p);
BigInteger ad = d.multiply(EC).mod(p);
System.out.println("\n\nc^r mod p = " + crmodp);
System.out.println("d = " + d);
System.out.println("Alice decodes: " + ad);
}
}
class Tools{
public static String GetString() throws IOException {
BufferedReader stringIn = new BufferedReader (new InputStreamReader(System.in));
return stringIn.readLine();
}// GetString
public static int GetInt( ) throws IOException {
String aux = GetString();
return Integer.parseInt(aux);
}// GetInt
public static long GetLong( ) throws IOException {
String aux = GetString();
return Long.parseLong(aux);
}// GetInt
public static char GetChar( ) throws IOException {
String aux = GetString();
return aux.charAt(0);
}// GetChar
public static double GetReal( ) throws IOException {
String aux = GetString();
Double d = new Double(aux) ;
return d.doubleValue() ;
}// GetReal
}