此条目 http://info.michael-simons.eu/2011/07/18/mysql-compatible-aes-encryption-decryption-in-java/解释说 mySQL 处理 AES-128 密钥的方式与您想象的有点不同:
.. MySQL 算法只是 or 给定密码短语的字节
如果密码长度超过 16 个字符,则针对前面的字节
当密码短于 16 个字符时,将它们保留为 0。
没有经过严格测试,但这似乎产生相同的结果(以十六进制表示)。
<cfscript>
function getMySQLAES128Key( key ) {
var keyBytes = charsetDecode( arguments.key, "utf-8" );
var finalBytes = listToArray( repeatString("0,", 16) );
for (var i = 1; i <= arrayLen(keyBytes); i++) {
// adjust for base 0 vs 1 index
var pos = ((i-1) % 16) + 1;
finalBytes[ pos ] = bitXOR(finalBytes[ pos ], keyBytes[ i ]);
}
return binaryEncode( javacast("byte[]", finalBytes ), "base64" );
}
key = "qLHVTZL9zF81kiTnNnK0Vg==";
input = "4111111111111111";
encrypted = encrypt(input, getMySQLAES128Key(key), "AES", "hex");
WriteDump("encrypted="& encrypted);
// note: assumes input is in "hex". either convert the bytes
// to hex in mySQL first or use binaryEncode
decrypted = decrypt(encrypted, getMySQLAES128Key(key), "AES", "hex");
WriteDump("decrypted="& decrypted);
</cfscript>
Note:如果您使用 mySQL 进行加密,请务必查看其文档,其中提到纯文本可能最终出现在各种日志中 http://dev.mysql.com/doc/refman/5.5/en/encryption-functions.html(复制、历史等)并且“可以由任何有权读取该信息的人阅读”。
Update:情况可能已经改变,但根据2004 年的错误报告 http://bugs.mysql.com/bug.php?id=3172 the .mysql_history
文件仅在 Unix 上存在。 (记住可能还有其他日志文件)清除.mysql_history的详细说明可以说明书上找到的 http://dev.mysql.com/doc/refman/5.0/en/mysql-history-file.html,但总而言之:
- Set the
MYSQL_HISTFILE
变量为 /dev/null (每次登录时)
- 创建 .mysql_history 作为 /dev/null 的符号链接(仅一次)