我使用$.ajax方法从前端发送凭据,并使用crypto.js对凭据进行加密。
javascript代码
var encrypted = CryptoJS.AES.encrypt("Message", "This is a key123", { mode: CryptoJS.mode.CFB});
$.ajax({
type: "POST",
url: $SCRIPT_ROOT + "/test",
contentType: "application/json",
data:JSON.stringify({key:encrypted.toString()}),
dataType: "json",
success: function (response) {
alert(response);
}
});我想在python烧瓶中的后端解密相同的凭据。
python代码
data = request.json
key = data["key"]
obj2 = AES.new('This is a key123', AES.MODE_CFB)
s = obj2.decrypt(key)
print s我在加密和解密时使用了相同的模式,但打印s将打印下面的字符串。
�Qg%��qNˮ�Ŵ�M��ĦP�
"~�JB���w���#]�v?W有没有人能建议我在前端和后端进行加解密的更好的方法?
我只在python中尝试过同样的加密-解密,
>>> from Crypto.Cipher import AES
>>> obj = AES.new('This is a key123', AES.MODE_CFB)
>>> message = "The answer is no"
>>> ciphertext = obj.encrypt(message)
>>> ciphertext
'\x1f\x99%8\xa8\x197%\x89U\xb6\xa5\xb6C\xe0\x88'
>>> obj2 = AES.new('This is a key123', AES.MODE_CFB)
>>> obj2.decrypt(ciphertext)
'The answer is no'它工作得很好,但我想在前端使用javascript加密数据,我想在python中使用相同的解密技术。
发布于 2014-06-18 16:34:30
传递给CryptoJS.AES.encrypt的字符串并不像Python代码那样直接用作密钥(在utf8编码之后),而是以某种方式用作密码来派生密钥。请参阅:https://code.google.com/p/crypto-js/#The_Cipher_Input
此外,输出还编码为可打印字符串,并不表示Python代码所需的原始字节字符串。请参阅:https://code.google.com/p/crypto-js/#The_Cipher_Output
https://stackoverflow.com/questions/24280364
复制相似问题