今晚有点迟了,具体说明明天再来补
Elgamal.java
package elgamal;
import java.math.BigInteger;
import java.lang.Math;
import java.util.Random;
import java.io.*;
public class ElGamal {
private static final BigInteger ONE = BigInteger.ONE;
// 获取随机大素数
public static BigInteger getPrime(int bitLenth) {
BigInteger p = BigInteger.probablePrime(bitLenth, new Random());
while(!p.isProbablePrime(6)) {
p.nextProbablePrime();
}
return p;
}
// 枚举遍历求生成元g
public static BigInteger getG(BigInteger p, BigInteger p_MinusOne) {
BigInteger g = null;
outterLoop: for (int i = 2; i < 50; i++) {
for (int x1 = 1; x1 <= Integer.valueOf(p_MinusOne.toString()); x1++) {
String str1 = String.valueOf(i);
String str2 = String.valueOf(x1);
BigInteger tmp1 = new BigInteger(str1);
BigInteger tmp2 = new BigInteger(str2);
if (tmp1.modPow(tmp2, p).compareTo(ONE) == 0 && tmp2.compareTo(p_MinusOne) == -1) {
break;
} else if (tmp1.modPow(tmp2, p).compareTo(ONE) == 0 && tmp2.compareTo(p_MinusOne) == 0) {
g = tmp1;
break outterLoop;
}
}
}
return g;
}
}
ReadFile.java
package elgamal;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class ReadFile {
public static String txt2String(File file) throws IOException{
StringBuilder result = new StringBuilder();
// 建立数据的输入通道
FileReader fileReader = new FileReader(file);
//建立缓冲字符数组读取文件数据
char[] buf = new char[1024];
int length = 0 ;
while((length = fileReader.read(buf))!=-1){
result.append(new String(buf,0,length));
}
return result.toString();
/*StringBuilder result = new StringBuilder();
try{
BufferedReader br = new BufferedReader(new FileReader(file));//构造一个BufferedReader类来读取文件
String s = null;
while((s = br.readLine())!=null){//使用readLine方法,一次读一行
result.append(System.lineSeparator()+s);
}
br.close();
}catch(Exception e){
e.printStackTrace();
}
return result.toString(); */
}
public static void main(String[] args) throws IOException{
File file = new File("H:/test.txt");
System.out.println(txt2String(file));
}
}
Main.java
package elgamal;
import java.io.File;
import java.io.IOException;
import java.math.BigInteger;
import java.util.Random;
public class Main {
private static final BigInteger ONE = BigInteger.ONE;
public static void main(String[] args) throws IOException {
File file = new File("H:/test.txt");
String m = ReadFile.txt2String(file);
System.out.println("消息m = " + m);
int length = m.length();
// 随机的大素数p
BigInteger p = ElGamal.getPrime(length);
System.out.println("p = " + p.toString());
// 下面计算时用到的p-1
BigInteger p_MinusOne = p.subtract(ONE);
System.out.println("p-1 = " + p_MinusOne.toString());
// 枚举遍历求生成元g
BigInteger g = null;
g = ElGamal.getG(p, p_MinusOne);
System.out.println("g = " + g.toString());
// 随机数x,满足区间[2,p-1)
BigInteger x = new BigInteger(length,new Random());
System.out.println("x = " + x.toString());
// y ≡ g^x( mod p )
BigInteger y = g.modPow(x,p);
System.out.println("y = " + y.toString());
// 随机的大素数k,满足区间[2,p-1)
BigInteger k = ElGamal.getPrime(length);
System.out.println("k = " + k.toString());
// k的逆元
BigInteger k_Reverse = k.modInverse(p_MinusOne);
System.out.println("k对p-1的逆元 = " + k_Reverse.toString());
// a ≡ g^k ( mod p )
BigInteger a = g.modPow(k,p);
System.out.println("a = " + a.toString());
// h(m)
int hm = Math.abs(m.hashCode());
String temp = String.valueOf(hm);
String hmm = temp.substring(1, 5);
BigInteger h_m = new BigInteger(hmm);
System.out.println("h(m) = " + h_m.toString());
BigInteger tmp1 = h_m.subtract(x.multiply(a));
System.out.println("tmp1 = " + tmp1.toString());
BigInteger tmp2 = tmp1.multiply(k_Reverse);
System.out.println("tmp2 = " + tmp2.toString());
BigInteger b = tmp2.mod(p_MinusOne);
System.out.println("b = " + b.toString());
System.out.println("消息m的数字签名为: " + a.toString() + "," + b.toString());
BigInteger left = y.pow(a.intValue()).multiply(a.pow(b.intValue())).mod(p);
System.out.println("left = " + left.toString());
int unsignedH_m = h_m.intValue();
if(unsignedH_m < 0) {
unsignedH_m = -unsignedH_m;
}
BigInteger right = g.pow(unsignedH_m).mod(p);
System.out.println("right = " + right.toString());
}
}