我在加密明文方面有问题。
我在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);联合来文的结果:
ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb有人知道我做错了什么吗?
还是有一种方法可以在两种语言中获得相同的加密结果?
顺便提一下:更改python代码会更容易,因为我的数据库中已经有了CryptoJS加密的值。
发布于 2015-01-21 16:53:11
CryptoJS通常不会抛出错误。你要把undefined传给hashed.toString(CryptoJS.Base64);。使用CryptoJS.enc.Base64,因为默认情况下使用CryptoJS.enc.Hex。
但是由于您更喜欢更改python,我建议您进行散列这边请。
def encrypt(plaintext):
import hashlib, base64
return hashlib.sha256(plaintext).hexdigest()当JavaScript更改默认行为时,仍然应该将CryptoJS代码更改为十六进制编码。
https://stackoverflow.com/questions/28071071
复制相似问题