是否存在具有密钥生成,加密和解密功能的ElGamal椭圆曲线密码系统的简单实现(使用Java BigInteger);可以用来向大学生讲课的那个吗?
例如,Paillier加密函数可以编码,而不失一般性,如下:
public BigInteger encrypt(BigInteger input) throws PaillierException {
if(!isInZN(input)) {
throw new PaillierException(PaillierException.TYPE_PLAINTEXT_NOT_IN_ZN, input);
}
BigInteger plaintext = handleNegative(input);
BigInteger r = randomInZStarN();
return (((n.multiply(plaintext).add(BigInteger.ONE)).multiply(r.modPow(n, nSquared)))).mod(nSquared);
}
包含优化g =(1 + n),这使得密文c =(1 + mn)r ^ n mod n ^ 2.
请不要建议Java 7本机实现或BouncyCastle实现,因为我不需要真正符合JCA标准的复杂实现。
感谢。
答案 0 :(得分:1)
答案 1 :(得分:1)
检查一下。 (根据您的需要进行修改)
//X9ECParameters ecParams = NISTNamedCurves.getByName("B-571");
X9ECParameters ecParams = NISTNamedCurves.getByName("B-163");
//X9ECParameters ecParams = NISTNamedCurves.getByName("P-384");
//X9ECParameters ecParams = NISTNamedCurves.getByName("P-521");
BigInteger privKey = new BigInteger("38e1", 16);
ECPoint pubKey = ecParams.getG().multiply(privKey);
System.out.println("Available curves:\n");
int counter = 0;
for ( Enumeration e = NISTNamedCurves.getNames(); e.hasMoreElements(); ){
if (counter == 3) {
counter = -1;
System.out.println( e.nextElement().toString() );
}else{
System.out.print( e.nextElement().toString() + " ");
}
counter++;
}
// System.out.println(privKey.toString(16));
// for (int i = 1; i < 30; i++) {
// ECPoint test = ecParams.getG().multiply(
// new BigInteger(Integer.toHexString(i), 16));
// System.out.println("X: " + test.getX().toBigInteger().toString(10)
// + "\n" + "Y: " + test.getY().toBigInteger().toString(10)
// + "\n");
// }
//Encryption "a" = first Point
ECPoint pMsg = ecParams.getG().multiply(
new BigInteger(Integer.toHexString(2), 16));
System.out.println("\n\n--------------------------------------------------------------------------------------------------------------------------------------------------");
System.out.println("Selected curve:\n");
System.out.println("Curve: " + ecParams.getCurve().getFieldSize() + "\n");
System.out.println("--------------------------------------------------------------------------------------------------------------------------------------------------");
System.out.println("Point: " + "X: " + pMsg.getX().toBigInteger().toString(16) + "\n" + " Y: " + pMsg.getY().toBigInteger().toString(16) + "\n");
System.out.println("--------------------------------------------------------------------------------------------------------------------------------------------------");
ECPoint one;
ECPoint two;
BigInteger random = new BigInteger(16, new SecureRandom());
/* g^r */
one = ecParams.getG().multiply(random);
/* pk^r+m */
two = pMsg.add(pubKey.multiply(random));
//encryptedData edM = new encryptedData(one, two);
System.out.println("Encrypted:\n");
System.out.println("One: " + "X: " + one.getX().toBigInteger().toString(16) + "\n" + " Y: " + one.getY().toBigInteger().toString(16) + "\n");
System.out.println("Two: " + "X: " + two.getX().toBigInteger().toString(16) + "\n" + " Y: " + two.getY().toBigInteger().toString(16) + "\n");
System.out.println("--------------------------------------------------------------------------------------------------------------------------------------------------");
//Decryption
ECPoint decrypted = edM.eData.subtract(edM.gInR.multiply(privKey));
System.out.println("Decrypted: " + "X: " + decrypted.getX().toBigInteger().toString(16) + "\n" + " Y: " + decrypted.getY().toBigInteger().toString(16) + "\n");