createDecipher的最终方法
当我加密security sentence
时,我不得不把cipher.final();但是当我解密时,不允许放入decipher.final()。我无法理解这种状态。当我使用'aes-128-gcm'时,我不需要最终方法吗?
const crypto = require('crypto');
const argo = 'aes-128-gcm';
const iv = Buffer.from(crypto.randomBytes(16)); //iv형성
const key = Buffer.from(crypto.randomBytes(16)); //key값 형성
const cipher = crypto.createCipheriv(argo, key, iv);
let result = cipher.update('security sentence','utf8', 'base64');
console.log(result);
result += cipher.final('base64');
console.log('암호화:', result);
const decipher = crypto.createDecipheriv(argo, key, iv);
let result2 = decipher.update(result, 'base64', 'utf8');
//result += decipher.final(); <- problem! occurring error.
console.log('복호화:', result2);
回答如下:
虽然您没有提供返回的实际错误,但我确定它是Unsupported state or unable to authenticate data
。
据说GCM
的AES
模式要求你从生成的密文中导出AuthenticationTag
并在解密过程中使用它。
我可以建议使用kruptein库使您的实现变得简单吗?
const kruptein = require('kruptein');
const options = {
secret: 'squirrel'
}
let ciphertext = JSON.stringify(kruptein.set('security sentence'));
// {"hmac":"Ó\u0002VBº7üG\t|\u0000ï4¯R¶\u0013g¾S\\¨®£øQö","ct":"5é >Å©\tPÈ¿R\tÒëß+ÂwßDâ¡\u000e\u0012Ã\fzö]5ç","at":{"type":"Buffer","data":[34,212,193,69,138,251,146,127,218,20,195,124,105,249,164,255]},"aad":"mKr:æY Ï¢oþ¼Yþ×Kù:5\u0005êeó\u0003!ÀÁk\"\u001d","iv":{"type":"Buffer","data":[80,14,4,251,14,240,168,121,208,220,165,150]}}
console.log(kruptein.get(ciphertext));
// security sentence
createDecipher的最终方法
当我加密security sentence
时,我不得不把cipher.final();但是当我解密时,不允许放入decipher.final()。我无法理解这种状态。当我使用'aes-128-gcm'时,我不需要最终方法吗?
const crypto = require('crypto');
const argo = 'aes-128-gcm';
const iv = Buffer.from(crypto.randomBytes(16)); //iv형성
const key = Buffer.from(crypto.randomBytes(16)); //key값 형성
const cipher = crypto.createCipheriv(argo, key, iv);
let result = cipher.update('security sentence','utf8', 'base64');
console.log(result);
result += cipher.final('base64');
console.log('암호화:', result);
const decipher = crypto.createDecipheriv(argo, key, iv);
let result2 = decipher.update(result, 'base64', 'utf8');
//result += decipher.final(); <- problem! occurring error.
console.log('복호화:', result2);
回答如下:
虽然您没有提供返回的实际错误,但我确定它是Unsupported state or unable to authenticate data
。
据说GCM
的AES
模式要求你从生成的密文中导出AuthenticationTag
并在解密过程中使用它。
我可以建议使用kruptein库使您的实现变得简单吗?
const kruptein = require('kruptein');
const options = {
secret: 'squirrel'
}
let ciphertext = JSON.stringify(kruptein.set('security sentence'));
// {"hmac":"Ó\u0002VBº7üG\t|\u0000ï4¯R¶\u0013g¾S\\¨®£øQö","ct":"5é >Å©\tPÈ¿R\tÒëß+ÂwßDâ¡\u000e\u0012Ã\fzö]5ç","at":{"type":"Buffer","data":[34,212,193,69,138,251,146,127,218,20,195,124,105,249,164,255]},"aad":"mKr:æY Ï¢oþ¼Yþ×Kù:5\u0005êeó\u0003!ÀÁk\"\u001d","iv":{"type":"Buffer","data":[80,14,4,251,14,240,168,121,208,220,165,150]}}
console.log(kruptein.get(ciphertext));
// security sentence