本文介绍了Java中的Diffie-Hellman密钥交换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Java项目,涉及通过不安全的渠道发送敏感数据。我需要知道如何使用其库在Java中实现Diffie Hellman密钥交换(DHKE)。我知道关于它的所有加密理论所以不需要详细说明,我只需要一个非常基本的实现,所以我可以让2个程序共享一个密钥。我从java2s.com获得了示例,但它没有完成:

I am working on a personal project in Java which involves sending sensitive data over an insecure channel. I need to know how to implement Diffie Hellman Key Exchange (DHKE) in java using its libraries. I know all the cryptographic theory about it so no need to go into details, I just need a very basic implementation so I cand have 2 programs share a secret key. I got the example from java2s.com, but it is not complete:

import java.math.BigInteger;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.SecureRandom;

import javax.crypto.spec.DHParameterSpec;
import javax.crypto.spec.DHPublicKeySpec;

public class Main {
  public final static int pValue = 47;

  public final static int gValue = 71;

  public final static int XaValue = 9;

  public final static int XbValue = 14;

  public static void main(String[] args) throws Exception {
    BigInteger p = new BigInteger(Integer.toString(pValue));
    BigInteger g = new BigInteger(Integer.toString(gValue));
    BigInteger Xa = new BigInteger(Integer.toString(XaValue));
    BigInteger Xb = new BigInteger(Integer.toString(XbValue));

    int bitLength = 512; // 512 bits
    SecureRandom rnd = new SecureRandom();
    p = BigInteger.probablePrime(bitLength, rnd);
    g = BigInteger.probablePrime(bitLength, rnd);

    createSpecificKey(p, g);
  }

  public static void createSpecificKey(BigInteger p, BigInteger g) throws Exception {
    KeyPairGenerator kpg = KeyPairGenerator.getInstance("DiffieHellman");

    DHParameterSpec param = new DHParameterSpec(p, g);
    kpg.initialize(param);
    KeyPair kp = kpg.generateKeyPair();

    KeyFactory kfactory = KeyFactory.getInstance("DiffieHellman");

    DHPublicKeySpec kspec = (DHPublicKeySpec) kfactory.getKeySpec(kp.getPublic(),
        DHPublicKeySpec.class);
  }
}

我如何继续?任何人都可以帮我完成剩余的代码吗?

How do I go on from this? Could anyone help me complete the remaining code?

推荐答案

?他们在那里显示代码中的DH密钥交换。

What about the official Oracle Docs? They show a DH key exchange in code there.

这篇关于Java中的Diffie-Hellman密钥交换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 20:32