Java Diffie

编程入门 行业动态 更新时间:2024-10-25 20:23:22
本文介绍了Java Diffie-Hellman密钥交换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试执行代码以执行Diffie-Hellman密钥交换。我从在线示例中获取了代码(现在忘记了)。 我必须导入bouncycastle.jar,我假设它会一直执行直到执行。

I'm trying to execute code to perform the Diffie-Hellman key exchange. I sourced the code from an example online (forget where now). I had to import the bouncycastle.jar, which I assumed worked up until execution.

我的代码:

package testproject; import java.math.BigInteger; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.MessageDigest; import java.security.SecureRandom; import java.security.Security; import javax.crypto.KeyAgreement; import javax.crypto.spec.DHParameterSpec; public class KeyGen { private static BigInteger g512 = new BigInteger("1234567890", 16); //generates a random, non-negative integer for Base private static BigInteger p512 = new BigInteger("1234567890", 16); //generates a random, non-negative integer for Prime public static void main(String[] args) throws Exception { Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); DHParameterSpec dhParams = new DHParameterSpec(p512, g512); //Specify parameters to use for the algorithm KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DH", "BC"); //Define specific algorithm to use "diffie-hellman", with provider "bc" keyGen.initialize(dhParams, new SecureRandom()); //initialize with parameters & secure random seed KeyAgreement aKeyAgree = KeyAgreement.getInstance("DH", "BC"); //define algorithm for A's key agreement KeyPair aPair = keyGen.generateKeyPair(); //generate keyPair for A KeyAgreement bKeyAgree = KeyAgreement.getInstance("DH", "BC"); //define algorithm for B's key agreement KeyPair bPair = keyGen.generateKeyPair(); //generate keyPair for B aKeyAgree.init(aPair.getPrivate()); //initialize A's keyAgreement with A's private key bKeyAgree.init(bPair.getPrivate()); //initialize B's keyAgreement with B's private key aKeyAgree.doPhase(bPair.getPublic(), true); //do last phase of A's keyAgreement with B's public key bKeyAgree.doPhase(aPair.getPublic(), true); //do last phase of B's keyAgreement with A's public key MessageDigest hash = MessageDigest.getInstance("SHA1", "BC"); System.out.println(new String(hash.digest(aKeyAgree.generateSecret()))); //generate secret key for A, hash it. System.out.println(new String(hash.digest(bKeyAgree.generateSecret()))); //generate secret key for B, hash it. } }

这是引起问题的行:

KeyPair aPair = keyGen.generateKeyPair();

我对错误的含义感到困惑,因为我发现了每种方法返回未知来源。

I'm confused as to what the error is, as I've found each of the methods it's returning 'unknown source' for.

对此有任何了解,将不胜感激。

Any light shed on this would be much appreciated.

续(编辑): Java-Diffie-Hellman加密-错误的输出

推荐答案

您已经更喜欢bouncycastle版本。但出于学习目的,我实现了一个helloworld版本。也许对那些只想在纯Java中不依赖地使用Diffie-Hellman的人有帮助:

You already preferred bouncycastle version. But I implemented a little helloworld version of it for learning purposes. Maybe it can be helpful for those who simply wants to use Diffie-Hellman in pure Java without dependencies:

// 1. ------------------------------------------------------------------ // This is Alice and Bob // Alice and Bob want to chat securely. But how? final Person alice = new Person(); final Person bob = new Person(); // ? ? // // O O // /|\ /|\ // / \ / \ // // ALICE BOB // 2. ------------------------------------------------------------------ // Alice and Bob generate public and private keys. alice.generateKeys(); bob.generateKeys(); // // O O // /|\ /|\ // / \ / \ // // ALICE BOB // _ PUBLIC KEY _ PUBLIC KEY // _ PRIVATE KEY _ PRIVATE KEY // 3. ------------------------------------------------------------------ // Alice and Bob exchange public keys with each other. alice.receivePublicKeyFrom(bob); bob.receivePublicKeyFrom(alice); // // O O // /|\ /|\ // / \ / \ // // ALICE BOB // + public key + public key // + private key + private key // _ PUBLIC KEY <-------------------------> _ PUBLIC KEY // 4. ------------------------------------------------------------------ // Alice generates common secret key via using her private key and Bob's public key. // Bob generates common secret key via using his private key and Alice's public key. // Both secret keys are equal without TRANSFERRING. This is the magic of Diffie-Helman algorithm. alice.generateCommonSecretKey(); bob.generateCommonSecretKey(); // // O O // /|\ /|\ // / \ / \ // // ALICE BOB // + public key + public key // + private key + private key // + public key + public key // _ SECRET KEY _ SECRET KEY // 5. ------------------------------------------------------------------ // Alice encrypts message using the secret key and sends to Bob alice.encryptAndSendMessage("Bob! Guess Who I am.", bob); // // O O // /|\ []--------------------------------> /|\ // / \ / \ // // ALICE BOB // + public key + public key // + private key + private key // + public key + public key // + secret key + secret key // + message _ MESSAGE // 6. ------------------------------------------------------------------ // Bob receives the important message and decrypts with secret key. bob.whisperTheSecretMessage(); // // O ((( ((( ((( \O/ ))) // /|\ | // / \ / \ // // ALICE BOB // + public key + public key // + private key + private key // + public key + public key // + secret key + secret key // + message + message

github/firatkucuk/diffie-hellman-helloworld

更多推荐

Java Diffie

本文发布于:2023-11-16 23:14:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1607893.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Java   Diffie

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!