【发布时间】:2014-06-18 08:14:28
【问题描述】:
我使用 $.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
我在加密和解密时使用了相同的模式,但是 print 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 中使用相同的解密技术。
【问题讨论】:
-
encrypted.toString()是将密文转换为字符串的不好方法。使用 Base64 或 HEX 文本表示,并在 JS 和 python 代码中进行相应的转换。 -
@OlegEstekhin 你能给我推荐任何可行的例子吗?
-
@OlegEstekhin 我已经更新了我的问题。
标签: python encryption cryptography flask pycrypto