2015-07-15 9 views
2

Я реализую протокол в Node.js. Этот протокол использует шифрование 3DES в режиме CBC, хорошо.Расширение DESK с использованием JavaScript

Но для шифрования/дешифрования мне нужно развернуть/развернуть 14-байтовый DES-ключ до 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 

Что не так? :/

ответ

0

Код верный и работает правильно.

Проблема заключается в вводе, я использовал обычный массив, а не буфер.

Использование буфера кода работает правильно.

Вход:

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 

Спасибо!