使用JavaScript扩展DESKey

使用JavaScript扩展DESKey,javascript,node.js,encryption,3des,parity,Javascript,Node.js,Encryption,3des,Parity,我正在Node.js中实现一个协议。 这个协议在CBC模式下使用3DES加密,好的 但要加密/解密,我需要将14字节的DES密钥扩展到16字节,只需添加奇偶校验位。但是我一直在使用JavaScript/Node.js 我有一些使用C和Python的实现,有谁能帮助我使用JavaScript/Node.js实现同样的功能吗(下面是我的试用版) 使用C实现的输出: function deskey_spread(normal){ spread = new Buffer(16); spread[

我正在Node.js中实现一个协议。 这个协议在CBC模式下使用3DES加密,好的

但要加密/解密,我需要将14字节的DES密钥扩展到16字节,只需添加奇偶校验位。但是我一直在使用JavaScript/Node.js

我有一些使用C和Python的实现,有谁能帮助我使用JavaScript/Node.js实现同样的功能吗(下面是我的试用版)

使用C实现的输出:

function deskey_spread(normal){
  spread = new Buffer(16);
  spread[ 0] = normal[ 0] & 0xfe;
  spread[ 1] = ((normal[ 0] << 7) | (normal[ 1] >> 1)) & 0xfe;
  spread[ 2] = ((normal[ 1] << 6) | (normal[ 2] >> 2)) & 0xfe;
  spread[ 3] = ((normal[ 2] << 5) | (normal[ 3] >> 3)) & 0xfe;
  spread[ 4] = ((normal[ 3] << 4) | (normal[ 4] >> 4)) & 0xfe;
  spread[ 5] = ((normal[ 4] << 3) | (normal[ 5] >> 5)) & 0xfe;
  spread[ 6] = ((normal[ 5] << 2) | (normal[ 6] >> 6)) & 0xfe;
  spread[ 7] = normal[ 6] << 1;
  spread[ 8] = normal[ 7] & 0xfe;
  spread[ 9] = ((normal[ 7] << 7) | (normal[ 8] >> 1)) & 0xfe;
  spread[10] = ((normal[ 8] << 6) | (normal[ 9] >> 2)) & 0xfe;
  spread[11] = ((normal[ 9] << 5) | (normal[10] >> 3)) & 0xfe;
  spread[12] = ((normal[10] << 4) | (normal[11] >> 4)) & 0xfe;
  spread[13] = ((normal[11] << 3) | (normal[12] >> 5)) & 0xfe;
  spread[14] = ((normal[12] << 2) | (normal[13] >> 6)) & 0xfe;
  spread[15] = normal[13] << 1;

  des_key_parity_adjust(spread, 16);
  return spread;
}

function des_key_parity_adjust(key, len){
    var i = new Buffer(1);
    var j = new Buffer(1);
    var parity = new Buffer(1);
    for (i = 0; i < len; i++){
      parity = 1;
      for (j = 1; j < 8; j++) 
        if ((key[i] >> j) & 0x1) parity = ~parity & 0x01;
      key[i] |= parity;
    }
}
01 80 80 61 40 29 19 0E 08 04 45 02 10 91 4C 29
01 80 80 61 40 29 19 0e 08 04 43 40 b0 61 34 1c
和my Node.js实现:

function deskey_spread(normal){
  spread = new Buffer(16);
  spread[ 0] = normal[ 0] & 0xfe;
  spread[ 1] = ((normal[ 0] << 7) | (normal[ 1] >> 1)) & 0xfe;
  spread[ 2] = ((normal[ 1] << 6) | (normal[ 2] >> 2)) & 0xfe;
  spread[ 3] = ((normal[ 2] << 5) | (normal[ 3] >> 3)) & 0xfe;
  spread[ 4] = ((normal[ 3] << 4) | (normal[ 4] >> 4)) & 0xfe;
  spread[ 5] = ((normal[ 4] << 3) | (normal[ 5] >> 5)) & 0xfe;
  spread[ 6] = ((normal[ 5] << 2) | (normal[ 6] >> 6)) & 0xfe;
  spread[ 7] = normal[ 6] << 1;
  spread[ 8] = normal[ 7] & 0xfe;
  spread[ 9] = ((normal[ 7] << 7) | (normal[ 8] >> 1)) & 0xfe;
  spread[10] = ((normal[ 8] << 6) | (normal[ 9] >> 2)) & 0xfe;
  spread[11] = ((normal[ 9] << 5) | (normal[10] >> 3)) & 0xfe;
  spread[12] = ((normal[10] << 4) | (normal[11] >> 4)) & 0xfe;
  spread[13] = ((normal[11] << 3) | (normal[12] >> 5)) & 0xfe;
  spread[14] = ((normal[12] << 2) | (normal[13] >> 6)) & 0xfe;
  spread[15] = normal[13] << 1;

  des_key_parity_adjust(spread, 16);
  return spread;
}

function des_key_parity_adjust(key, len){
    var i = new Buffer(1);
    var j = new Buffer(1);
    var parity = new Buffer(1);
    for (i = 0; i < len; i++){
      parity = 1;
      for (j = 1; j < 8; j++) 
        if ((key[i] >> j) & 0x1) parity = ~parity & 0x01;
      key[i] |= parity;
    }
}
01 80 80 61 40 29 19 0E 08 04 45 02 10 91 4C 29
01 80 80 61 40 29 19 0e 08 04 43 40 b0 61 34 1c

怎么了/

代码正确且工作正常

问题是输入,我使用的是普通数组,而不是缓冲区

使用缓冲区,代码可以正常工作

输入:

01 80 80 61 40 29 19 0E 08 04 45 02 10 91 4C 29
使用C代码:

01 80 80 61 40 29 19 0E 08 04 45 02 10 91 4C 29
使用my Node.js实现

01 80 80 61 40 29 19 0E 08 04 45 02 10 91 4C 29
谢谢