Sim*_*on. 5 java bouncycastle diffie-hellman
我正在尝试执行代码来执行Diffie-Hellman密钥交换.我从在线示例中获取代码(忘记现在的位置).我不得不导入bouncycastle.jar,我假设它一直在执行.

我的代码:
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.
}
}
Run Code Online (Sandbox Code Playgroud)
这是导致问题的一行:
KeyPair aPair = keyGen.generateKeyPair();
Run Code Online (Sandbox Code Playgroud)
我对错误是什么感到困惑,因为我发现每个方法都返回'未知来源'.
任何关于此的光线都将非常受欢迎.
你已经选择了bouncycastle版本.但是为了学习目的,我实现了一个helloworld版本.对于那些只想在没有依赖关系的纯Java中使用Diffie-Hellman的人来说,它可能会有所帮助:
// 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
Run Code Online (Sandbox Code Playgroud)
https://github.com/firatkucuk/diffie-hellman-helloworld
这个评论是完全错误的:
private static BigInteger g512 = new BigInteger("1234567890", 16);
//generates a random, non-negative integer for Base
Run Code Online (Sandbox Code Playgroud)
您在那里所做的就是0x1234567890每次都创建数字。没有什么是随机的。
看起来您是从http://www.java2s.com/Tutorial/Java/0490__Security/DiffieHellmanKeyAgreement.htm复制的。正如这个答案所同意的那样,那里的代码没有意义。
您可以在该站点上尝试实际的密钥交换示例。
| 归档时间: |
|
| 查看次数: |
13527 次 |
| 最近记录: |