最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

NodeBE javascript实现PBEWithMD5AndTripleDESCBCPKCS5Padding

IT培训 admin 12浏览 0评论

NodeBE javascript实现PBEWithMD5AndTripleDES / CBC / PKCS5Padding

为了编写与使用Java编写的服务器对话的简单nodejs应用程序,我必须为nodejs实现以下功能。

public class Crypto {
  Cipher decipher;

  byte[] salt = {
      (byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04,
      (byte) 0x0A, (byte) 0x0B, (byte) 0x0C, (byte) 0x0D
  };
  int iterationCount = 10;

  public Crypto(String pass) {
    try {
      KeySpec keySpec = new PBEKeySpec(pass.toCharArray(), salt, iterationCount);

      SecretKey key = SecretKeyFactory.getInstance(
          "PBEWithMD5AndTripleDES").generateSecret(keySpec);

      ecipher = Cipher.getInstance("PBEWithMD5AndTripleDES/CBC/PKCS5Padding");

      AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, iterationCount);

      decipher.init(Cipher.DECRYPT_MODE, key, paramSpec);

    } catch (Exception ex) {
    }
  }
}

我使用crypto module of nodejs

var crypto = require('crypto'),
      pass = new Buffer(wek),
      salt = new Buffer([0x01, 0x02, 0x03, 0x04, 0x0A, 0x0B, 0x0C, 0x0D])
      password = 'mySecretPassword'
      key = crypto.pbkdf2(pass, salt, 10, 256)
      cipher, 
      encrypted;

cipher = crypto.createCipher('des-ede-cbc', key);
encrypted = cipher.update(new Buffer('the very secred information'));

将加密的信息发送到服务器后,我无法使用上面的Java代码示例中列出的decipher对象对消息进行解密。我认为主要问题是md5部分。我无法弄清楚如何使用crypto nodejs模块实现该功能。有谁知道如何解决这个问题?还是有其他模块或库可以实现这一目标?

编辑:我为nodejs尝试了另一个模块:node-forge

node-forge

但是我有几个问题/问题:

  • 我在JavaScript中使用正确的算法吗?
  • forge = require('node-forge') var numIterations = 10, keyLength = 24, password = forge.util.createBuffer('mySecretPassword'), salt = new forge.util.ByteBuffer(new Uint8Array([0x01, 0x02, 0x03, 0x04, 0x0A, 0x0B, 0x0C, 0x0D])), derivedKey = forge.pkcs5.pbkdf2(password, salt.getBytes(), numIterations, keyLength, forge.md.md5.create()) iv = {}; // TODO... ??? var cipher = forge.des.createEncryptionCipher(derivedKey); cipher.start(iv); cipher.update('the very secred information'); cipher.finish(); var encrypted = cipher.output; 计算是否与Java实现匹配?
  • 我如何确定在Java实现中使用哪个salt
  • 如何在Java实现中生成keyLength?在带有initialization vector的最后一个代码示例中,我必须在node-forge上提供iv。在Java代码中,我看不到它是如何完成的。我认为cipher.start(iv)在客户端和服务器上必须相同,还是不正确?
回答如下:

我对com.sun.crypto.provider.PBES1Core#deriveCipherKey()中找到的密钥派生函数的DESede部分进行了反向工程。

我们将Jasypt用作Java服务器中的加密库,而我们的node.js服务器能够以此进行加密和解密。希望对您有所帮助(写在ES2015中,在节点v4.0.0及更高版本中运行):

iv

解释一下发生了什么:

  • 加密后的消息以十六进制格式返回,其他可能更适合您的实现。
  • _ generateKey()是来自Java源代码的直接副本。
  • 使用的密钥长度为32个字节,并假设前24个字节是TripleDES的密钥,后8个是salt
  • 生成的消息的前缀是用于加密消息的随机生成的盐。
  • 取决于JVM的安全性设置,可能您实际上并未使用des-ede3(cbc似乎是固定设置)。您绝对应该仔细检查这是否适用于您的设置。

这里可能需要清理一些代码,但这至少应该使您朝正确的方向开始。

NodeBE javascript实现PBEWithMD5AndTripleDES / CBC / PKCS5Padding

为了编写与使用Java编写的服务器对话的简单nodejs应用程序,我必须为nodejs实现以下功能。

public class Crypto {
  Cipher decipher;

  byte[] salt = {
      (byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04,
      (byte) 0x0A, (byte) 0x0B, (byte) 0x0C, (byte) 0x0D
  };
  int iterationCount = 10;

  public Crypto(String pass) {
    try {
      KeySpec keySpec = new PBEKeySpec(pass.toCharArray(), salt, iterationCount);

      SecretKey key = SecretKeyFactory.getInstance(
          "PBEWithMD5AndTripleDES").generateSecret(keySpec);

      ecipher = Cipher.getInstance("PBEWithMD5AndTripleDES/CBC/PKCS5Padding");

      AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, iterationCount);

      decipher.init(Cipher.DECRYPT_MODE, key, paramSpec);

    } catch (Exception ex) {
    }
  }
}

我使用crypto module of nodejs

var crypto = require('crypto'),
      pass = new Buffer(wek),
      salt = new Buffer([0x01, 0x02, 0x03, 0x04, 0x0A, 0x0B, 0x0C, 0x0D])
      password = 'mySecretPassword'
      key = crypto.pbkdf2(pass, salt, 10, 256)
      cipher, 
      encrypted;

cipher = crypto.createCipher('des-ede-cbc', key);
encrypted = cipher.update(new Buffer('the very secred information'));

将加密的信息发送到服务器后,我无法使用上面的Java代码示例中列出的decipher对象对消息进行解密。我认为主要问题是md5部分。我无法弄清楚如何使用crypto nodejs模块实现该功能。有谁知道如何解决这个问题?还是有其他模块或库可以实现这一目标?

编辑:我为nodejs尝试了另一个模块:node-forge

node-forge

但是我有几个问题/问题:

  • 我在JavaScript中使用正确的算法吗?
  • forge = require('node-forge') var numIterations = 10, keyLength = 24, password = forge.util.createBuffer('mySecretPassword'), salt = new forge.util.ByteBuffer(new Uint8Array([0x01, 0x02, 0x03, 0x04, 0x0A, 0x0B, 0x0C, 0x0D])), derivedKey = forge.pkcs5.pbkdf2(password, salt.getBytes(), numIterations, keyLength, forge.md.md5.create()) iv = {}; // TODO... ??? var cipher = forge.des.createEncryptionCipher(derivedKey); cipher.start(iv); cipher.update('the very secred information'); cipher.finish(); var encrypted = cipher.output; 计算是否与Java实现匹配?
  • 我如何确定在Java实现中使用哪个salt
  • 如何在Java实现中生成keyLength?在带有initialization vector的最后一个代码示例中,我必须在node-forge上提供iv。在Java代码中,我看不到它是如何完成的。我认为cipher.start(iv)在客户端和服务器上必须相同,还是不正确?
回答如下:

我对com.sun.crypto.provider.PBES1Core#deriveCipherKey()中找到的密钥派生函数的DESede部分进行了反向工程。

我们将Jasypt用作Java服务器中的加密库,而我们的node.js服务器能够以此进行加密和解密。希望对您有所帮助(写在ES2015中,在节点v4.0.0及更高版本中运行):

iv

解释一下发生了什么:

  • 加密后的消息以十六进制格式返回,其他可能更适合您的实现。
  • _ generateKey()是来自Java源代码的直接副本。
  • 使用的密钥长度为32个字节,并假设前24个字节是TripleDES的密钥,后8个是salt
  • 生成的消息的前缀是用于加密消息的随机生成的盐。
  • 取决于JVM的安全性设置,可能您实际上并未使用des-ede3(cbc似乎是固定设置)。您绝对应该仔细检查这是否适用于您的设置。

这里可能需要清理一些代码,但这至少应该使您朝正确的方向开始。

发布评论

评论列表 (0)

  1. 暂无评论