javascript - 使用 JavaScript 扩展 DESKey

标签 javascript node.js encryption 3des parity

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

但是为了加密/解密,我需要将 14 字节 DES key 扩展/扩展为 16 字节,只需添加奇偶校验位即可。但。我在使用 JavaScript/Node.js 时陷入困境。

我有一些使用 C 和 Python 的实现,任何人都可以帮助我使用 JavaScript/Node.js 做同样的事情(我的试用如下)?

uint8 *des_key_spread(uint8 *normal){
  static uint8 spread[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;
}

void des_key_parity_adjust(uint8 *key, uint8 len){
  uint8 i, j, parity;

    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;
    }
}

从这里开始Python expand/spread

我的实现:

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 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e

使用 C 实现的输出:

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

以及我的 Node.js 实现:

01 80 80 61 40 29 19 0e 08 04 43 40 b0 61 34 1c

怎么了? :/

最佳答案

代码正确并且工作正常。

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

使用 Buffer 代码可以正常工作。

输入:

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

使用我的 Node.js 实现

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

谢谢!

关于javascript - 使用 JavaScript 扩展 DESKey,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31419517/

相关文章:

javascript - Mongo/Meteor 游标更新事件

javascript - 更新具有不同值的多个文档

javascript - 错误node.js无法加载资源

encryption - 有没有办法从现有的 Realm 数据库中删除加密?

javascript - 是否有 AES 的任何客户端 Javascript/JQuery 实现?

javascript - 在单选按钮选择上显示表格,然后将单选按钮值传递到表中的字段,选择添加更多,然后附加到现有表格

javascript - JQuery 手机 : dynamically loaded page source still available in DOM even after changing the page

javascript - 如何最初仅扩展此 d3.js 中选定的 Node ?

.net - RijndaelManaged "Padding is invalid and cannot be removed"仅在生产中解密时发生

javascript - 弹出式旋转木马 - 显示 3 张不同透明度的幻灯片