我在使用AES解密器来缓冲对象时遇到问题,我希望有人知道,我做错了什么......
myexample中: 我有一个带有AES_ENCRYPT的MySQL表用于用户名和密码......
CREATE TABLE Accounts
(
id INT(4) NOT NULL AUTO_INCREMENT,
username VARBINARY(128) NOT NULL,
password VARBINARY(128) NOT NULL,
PRIMARY KEY(id)
);
DELIMITER |
CREATE TRIGGER encodeAccounts BEFORE INSERT ON Accounts
FOR EACH ROW BEGIN
SET NEW.username = AES_ENCRYPT(NEW.username, 'password');
SET NEW.password = AES_ENCRYPT(NEW.password, 'password');
END;
的node.js: 我正在使用node-mysql模块向我的数据发送查询:
SELECT * FROM Accounts;
为密码和用户名返回此类值:
<Buffer 07 86 95 ee 77 df 86 50 ae 18 4c d5 3e 48 42 75>
如何使用Node.JS解密方法对其进行解码?我的尝试都失败了...... 我希望AES-128-ECB正确选择......
在Node.JS中取消内容应该如下所示:
var decipher = crypto.createDecipher('AES-128-ECB', 'password');
var dec = decipher.update(rows[i].username); // an example, value look like <Buffer ...
dec += decipher.final('utf8');
console.log('dec: ' + dec);
感谢所有帮助:)
答案 0 :(得分:8)
您的密码必须以与MySQL内部相同的方式进行转换。 必须产生16Byte长度的密码XORd以获得更长的密码或填充0字节。 这个功能应该完成这项工作:
function convertCryptKey(strKey) {
var newKey = new Buffer([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]);
strKey = new Buffer(strKey);
for(var i=0;i<strKey.length;i++) newKey[i%16]^=strKey[i];
return newKey;
}
与MySQL兼容的加密 - 请注意使用带有空IV的“createCipheriv”。 (createCipher创建一个自己的IV,与MySQL中的不同)
var c = crypto.createCipheriv("aes-128-ecb", convertCryptKey("myPassword"), "");
var crypted = c.update('Take a trip at galaxytrek.com :)', 'utf8', 'hex') + c.final('hex');
console.log(crypted.toUpperCase());
>> 92068A5DAECE1E080EF4BA08A45CFF8D2262401F988A9241F1E4CEAAAB80BEAF7E0B50A1D5FD57CA56E92621622F018D
与...相同:
mysql> select hex(aes_encrypt('Take a trip at galaxytrek.com :)','myPassword'));
+--------------------------------------------------------------------------------------------------+
| hex(aes_encrypt('Take a trip at galaxytrek.com :)','myPassword')) |
+--------------------------------------------------------------------------------------------------+
| 92068A5DAECE1E080EF4BA08A45CFF8D2262401F988A9241F1E4CEAAAB80BEAF7E0B50A1D5FD57CA56E92621622F018D |
+--------------------------------------------------------------------------------------------------+
<强>解密强>
var dc = crypto.createDecipheriv("aes-128-ecb", convertCryptKey("myPassword"), "");
var decrypted = dc.update('92068A5DAECE1E080EF4BA08A45CFF8D2262401F988A9241F1E4CEAAAB80BEAF7E0B50A1D5FD57CA56E92621622F018D', 'hex', 'utf8') + dc.final('utf8');
console.log(decrypted);
>> Take a trip at galaxytrek.com :)