Node.js crypto key creation using Java

南笙酒味 提交于 2020-04-13 08:00:40

问题


I have this code in node.js, it create Key using Crypto. Can i create same key using Java?

diffieHellmanConfig': {
      'group': 'modp14',
      'encoding': 'base64'
    }


 const clientDHInstance = crypto.getDiffieHellman(config.userCardCrypto.diffieHellmanConfig.group);
 clientDHInstance.generateKeys();

 const clientPublicKey = clientDHInstance.getPublicKey(config.userCardCrypto.diffieHellmanConfig.encoding);

I try to use

KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DH");
keyGen.initialize(2048);
KeyPair keypair = keyGen.genKeyPair();
PrivateKey privateKey = keypair.getPrivate();
PublicKey publicKey = keypair.getPublic();
byte[] encoded = publicKey.getEncoded();
String s = Base64.getEncoder().encodeToString(encoded);
System.out.println(s);

But key is wrong

Node key is t2EBMu8wCShfu8Dm45i9nNQ+TXyG5Oz1/izjTwRD0dchUK2400R9cP+NjLlOqjLstXnTbG5/aa0WaCoP187J90piiebGOjZUlF/Bu/xkkrYncQHAJ403J8+R2V5eHYCSQXbS6CSO7x+eEhNz8QTMgwkOR9w1R3gvbibaqL7qyQARPFak6+VIKFLUakSzMvdAIjLNPu2dva1QdJixid+EYiZE/DxA7lqpje74I7wynZj7kmUZXtiIWu46suf5CaVONtjEVZilvErJNpVlPX5TXoMVNrWkl9g5Aa6moXg4K0M6Gc4taumnDr9gh4PEuw+/QVauEld27/5TQlfAlalvzg==

Java key is MIICKTCCARsGCSqGSIb3DQEDATCCAQwCggEBAP//////////yQ/aoiFowjTExmKLgNwc0SkCTgiKZ8x0Agu+pjsTmyJRSgh5jjQE3e+VGbPNOkMbMCsKbfJfFDdP4TVtbVHCReSFtXZiXn7G9ExC6aY37WsL/1y29Aa37e44a/taiZ+lrp8kEXxLH+ZJKGZR7ORbPcIAfLihY78FmNpINhxV05ppFj+o/STPX4NlXSPco62WHGLzViCFUrue1SkHcJaWbWcMNU5KvJgE8XRsCMoYIXwykF5GLjbOO+OedywYDoYDmyeDouwHoo+1xV3wb0xSyd4ry/aVWBcYOZVJfOqVauUV0iYYmPoFEBVyjlqKrKpo//////////8CAQICAgQAA4IBBgACggEBAI+NYSvMh0bfh1ptt62vHEHENz6ZAYvnnrwmRhQRdYMBZiqu60AvJ4F6qL99EfesxvH3n8YaC+cG7bKAqkw74rRHJXkMF5xOy/kpTVvmQGAPjvTi5o4BJtHLOBgiwFKy7CYFKFksCJzkqNdAuPri/mfMm6GNG5MBYtQIurWkgOnnrVl3Nra2CSVUixQ5zCANOSnnNYNFPanr01bI6KZXsiRZRqfA4oYxBPySy4Sp1dx2IvSQe8EjNWTicTQQj/HP7hl1yf3uiYlM4h3dMbmfqv6Y10hW8kvoD88/mh09pdz+HxxDz+mVSMe+3+N7VIYUEGRHhrAvjbXmwh5zyCMIJiI=


回答1:


In your example keys, the DH keys created by node are the expected 2048 bits in size, however, the Java DH keys are pushing into ~4k bits.

So I think there is something wrong with the way you are initialising the DH keys on Java.

Looking at your code, I don't think it's appropriate to provide an integer literal to specify the size of the DH cyclic group.

Instead try instantiating your Java DH keys like this:

  final DHParameterSpec keySpec=new DHParameterSpec(DH_MODULUS,DH_BASE);
  final KeyPair keyPair;
  try {
    KeyPairGenerator keyGen=KeyPairGenerator.getInstance("DH");
    keyGen.initialize(keySpec);
    keyPair=keyGen.generateKeyPair();
    PrivateKey privateKey = keyPair.getPrivate();
    PublicKey publicKey = keyPair.getPublic();
    byte[] encoded = publicKey.getEncoded();
    String s = Base64.getEncoder().encodeToString(encoded);
    System.out.println(s);
  }

You need to create a DHParameterSpec object in Java providing a modulus and base, similar to the values provided in node.

Example:

public DHParameterSpec modp14() {
  final BigInteger p =
      new BigInteger(
          "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1"
              + "29024E088A67CC74020BBEA63B139B22514A08798E3404DD"
              + "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245"
              + "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED"
              + "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D"
              + "C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F"
              + "83655D23DCA3AD961C62F356208552BB9ED529077096966D"
              + "670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B"
              + "E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9"
              + "DE2BCBF6955817183995497CEA956AE515D2261898FA0510"
              + "15728E5A8AACAA68FFFFFFFFFFFFFFFF",
          16);
  final BigInteger g = new BigInteger("2");
  return new DHParameterSpec(p, g);
}

Pls see here for specification on prime group.



来源:https://stackoverflow.com/questions/60563334/node-js-crypto-key-creation-using-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!