NodeJS AESCFB + pkcs7填充解密

huangapple go评论95阅读模式
英文:

NodeJS AESCFB + pkcs7 padding decryption

问题

我正在尝试使用crypt或crypt-js将以下Go函数移植到nodeJS,但我遇到了一些问题,无法确定问题出在哪里:

Go加密代码可在https://go.dev/play/p/O88Bslwd-qh找到(加密和解密都有效)。

当前的nodejs实现代码如下:

var decryptKey = "93D87FF936DAB334C2B3CC771C9DC833B517920683C63971AA36EBC3F2A83C24";

const crypto = require('crypto');
const algorithm = 'aes-256-cfb';
const BLOCK_SIZE = 16;

var message = "8a0f6b165236391ac081f5c614265b280f84df882fb6ee14dd8b0f7020962fdd";

function encryptText(keyStr, text) {
  const hash = crypto.createHash('sha256');

  //Decode hex key
  keyStr = Buffer.from(keyStr, "hex");

  hash.update(keyStr);
  const keyBytes = hash.digest();

  const iv = crypto.randomBytes(BLOCK_SIZE);
  const cipher = crypto.createCipheriv(algorithm, keyBytes, iv);
  cipher.setAutoPadding(true);
  let enc = [iv, cipher.update(text, 'latin1')];
  enc.push(cipher.final());
  return Buffer.concat(enc).toString('hex');
}

function decryptText(keyStr, text) {
  const hash = crypto.createHash('sha256');
  //Decode hex key
  keyStr = Buffer.from(keyStr, "hex");

  hash.update(keyStr);
  const keyBytes = hash.digest();

  const contents = Buffer.from(text, 'hex');
  const iv = contents.slice(0, BLOCK_SIZE);
  const textBytes = contents.slice(BLOCK_SIZE);
  const decipher = crypto.createDecipheriv(algorithm, keyBytes, iv);
  decipher.setAutoPadding(true);
  let res = decipher.update(textBytes, 'latin1');
  res += decipher.final('latin1');
  return res;
}

console.log(message);
result = decryptText(decryptKey, message);
console.log(result);
message = encryptText(decryptKey, 'hola').toString();
console.log(message);
result = decryptText(decryptKey, message);
console.log(result);

有任何想法为什么它不能按预期工作吗?

注意:我知道在cfb模式下不需要填充,但我不能修改加密代码,这只是作为参考。

英文:

I'm trying to port the following Go functions to nodeJS using crypt or crypt-js but i'm having issues trying to figure out what's wrong:

The Go encryption code is available at https://go.dev/play/p/O88Bslwd-qh ( both encrypt and decrypt work)

The current nodejs implementation is:

var decryptKey= "93D87FF936DAB334C2B3CC771C9DC833B517920683C63971AA36EBC3F2A83C24";
const crypto = require('crypto');
const algorithm = 'aes-256-cfb';
const BLOCK_SIZE = 16;
var message = "8a0f6b165236391ac081f5c614265b280f84df882fb6ee14dd8b0f7020962fdd"
function encryptText(keyStr, text) {
const hash = crypto.createHash('sha256');
//Decode hex key
keyStr = Buffer.from(keyStr, "hex")
hash.update(keyStr);
const keyBytes = hash.digest();
const iv = crypto.randomBytes(BLOCK_SIZE);
const cipher = crypto.createCipheriv(algorithm, keyBytes, iv);
cipher.setAutoPadding(true);
let enc = [iv, cipher.update(text,'latin1')];
enc.push(cipher.final());
return Buffer.concat(enc).toString('hex');
}
function decryptText(keyStr, text) {
const hash = crypto.createHash('sha256');
//Decode hex key
keyStr = Buffer.from(keyStr, "hex")
hash.update(keyStr);
const keyBytes = hash.digest();
const contents = Buffer.from(text, 'hex');
const iv = contents.slice(0, BLOCK_SIZE);
const textBytes = contents.slice(BLOCK_SIZE);
const decipher = crypto.createDecipheriv(algorithm, keyBytes, iv);
decipher.setAutoPadding(true);
let res = decipher.update(textBytes,'latin1');
res += decipher.final('latin1');
return res;
}
console.log(message)
result = decryptText(decryptKey,message);
console.log(result);
message = encryptText(decryptKey,'hola').toString();
console.log(message)
result = decryptText(decryptKey,message);
console.log(result);

Any idea why it is not working as expected?

Note: I know that padding is not required with cfb but i can't modify the encryption code, it just for reference.

答案1

得分: 1

我不了解Go语言或者aes.NewCipher(key)的具体情况,但根据它的文档,看起来它并没有对密钥进行哈希处理。你提供的Go代码也没有对密钥进行哈希处理,所以我不确定你在Node.js代码中为什么要进行哈希处理。

以下代码应该足够了:

function encryptText(keyStr, text) {
  const keyBytes = Buffer.from(keyStr, "hex");
  
}

function decryptText(keyStr, text) {
  const keyBytes = Buffer.from(keyStr, 'hex');
  
}

另外,看起来你可能正在使用这些函数对JSON块进行加密。如果是这样的话,我建议在加密/解密过程中不要使用任何编码(比如latin1),因为JSON文本必须使用UTF-8进行编码。

英文:

I don't know Go or the specifics of aes.NewCipher(key), but from its documentation it doesn't look like it's hashing the key in any way. The Go code you're linking to also doesn't hash it, so I'm not sure why you're hashing it in the Node.js code.

This should be sufficient:

function encryptText(keyStr, text) {
const keyBytes = Buffer.from(keyStr, "hex")
…
}
function decryptText(keyStr, text) {
const keyBytes = Buffer.from(keyStr, 'hex');
…
}

As an aside: it looks like you may be encrypting JSON blocks with these functions. If so, I would suggest not using any encoding (like latin1) during the encryption/decryption process, given that JSON text must be encoded using UTF-8.

huangapple
  • 本文由 发表于 2022年8月2日 01:39:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/73197571.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定