gpt4 book ai didi

Java - Diffie-Hellman 加密 - 错误的输出

转载 作者:行者123 更新时间:2023-12-02 06:03:21 26 4
gpt4 key购买 nike

我正在尝试实现 Diffie-Hellman key 交换。我对生成后如何使用 key 有点困惑。

如输出所示,2 个 key 是在 key 交换中使用相同的质数和基数生成的,并在生成 key 时交换公钥,但是它们没有输出与我预期相同的值。

我对如何实现这种加密方法感到非常困惑,并且非常感谢您的指导。我的总体目标是实现一个加密的 SMS Android 应用程序。

enter image description here
(截图无法捕捉到a和b的底基和素数的整个长度)

AlgorithmParameterGenerator paramGen = AlgorithmParameterGenerator.getInstance("DH");
paramGen.init(512); // number of bits
AlgorithmParameters params = paramGen.generateParameters();
DHParameterSpec dhSpec = (DHParameterSpec)params.getParameterSpec(DHParameterSpec.class);

BigInteger p512 = dhSpec.getP();
BigInteger g512 = dhSpec.getG();

//A
KeyPairGenerator akpg = KeyPairGenerator.getInstance("DiffieHellman");

DHParameterSpec param = new DHParameterSpec(p512, g512);
System.out.println("Prime: " + p512);
System.out.println("Base: " + g512);
akpg.initialize(param);
KeyPair kp = akpg.generateKeyPair();

//B
KeyPairGenerator bkpg = KeyPairGenerator.getInstance("DiffieHellman");

DHParameterSpec param2 = new DHParameterSpec(p512, g512);
System.out.println("Prime: " + p512);
System.out.println("Base: " + g512);
bkpg.initialize(param2);
KeyPair kp2 = bkpg.generateKeyPair();


KeyAgreement aKeyAgree = KeyAgreement.getInstance("DiffieHellman");
KeyAgreement bKeyAgree = KeyAgreement.getInstance("DiffieHellman");

aKeyAgree.init(kp.getPrivate());
bKeyAgree.init(kp2.getPrivate());

aKeyAgree.doPhase(kp2.getPublic(), true);
bKeyAgree.doPhase(kp.getPublic(), true);

//System.out.println("Alice Secret Key: " + aKeyAgree.generateSecret());
//System.out.println("Bob's Secret Key: " + bKeyAgree.generateSecret());

MessageDigest hash = MessageDigest.getInstance("SHA-256");

byte[] ASharedSecret = hash.digest(aKeyAgree.generateSecret());
byte[] BSharedSecret = hash.digest(bKeyAgree.generateSecret());

System.out.println("Alice's Shared Secret: " + ASharedSecret.toString());
System.out.println("Bob's Shared Secret: " + BSharedSecret.toString());

最佳答案

byte[].toString() 不打印内容。尝试 Arrays.toString()。

关于Java - Diffie-Hellman 加密 - 错误的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22492417/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com