【发布时间】:2015-01-21 15:37:17
【问题描述】:
我在加密明文时遇到问题。
我在 Python 中做什么:
def encrypt(plaintext):
import hashlib, base64
hashed = hashlib.sha256(plaintext).digest()
return base64.b64encode(hashed)
def main():
input_value = "a"
output = encrypt(plaintext=input_value)
print output
if __name__ == "__main__":
main()
Python 中的结果:
ypeBEsobvcr6wjGzmiPcTaeG7/gUfE5yuYB3ha/uSLs=
我在 JS 中做什么:
var result = '';
var plaintext = 'a';
if(plaintext != null && plaintext != undefined) {
var hashed = CryptoJS.SHA256(plaintext);
result = hashed.toString(CryptoJS.Base64);
}
alert(result);
在 JS 中的结果:
ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb
有人知道我做错了什么吗?
或者有没有办法在两种语言中获得相同的加密结果?
顺便说一句:我更改 python 代码会更容易,因为我的数据库中已经有 CryptoJS 加密的值。
【问题讨论】:
-
感谢您的评论。这只是我的代码中的复制和粘贴错误。它应该是“输出=加密(纯文本=输入值)”
标签: encoding hash cryptography cryptojs cross-language