📄 elgamal.java
字号:
import java.math.*;import java.util.*;import java.security.*;import java.io.*;public class ElGamal{ 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); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -