如何在Python中encryption和在Javascript中解密?

我可以使用Python或JavaScriptencryption/解密,但将由Python生成的encryption数据传递给我的JavaScript代码失败。

base64编码/解码跨语言工作,以便Python上的基本编码和JavaScript上的解码检索原始的encryptionstring。

testingfunction之外,我没有使用Python解密或JavaScriptencryption,但他们在这里是为了完整性,因为一些读者错过了文本说他们存在。

在Python 2中:

import base64 from pyaes import AESModeOfOperationCTR SECRET_KEY = "This_key_for_demo_purposes_only!" def encrypt(raw_data, key=SECRET_KEY): aes = AESModeOfOperationCTR(key) encrypted_data = aes.encrypt(raw_data) base64_encrypted_data = base64.b64encode(encrypted_data) return base64_encrypted_data def decrypt(base64_encrypted_data, key=SECRET_KEY): encrypted_data = base64.b64decode(base64_encrypted_data) aes = AESModeOfOperationCTR(key) decrypted = aes.decrypt(encrypted_data) return decrypted 

在JavaScript(运行服务器端,在Parse.com云代码):

 var Buffer = require('buffer').Buffer; var Crypto = require('crypto'); encrypt: function(raw) { var cryptoAlgorithm = "aes-256-ctr"; var key = "This_key_for_demo_purposes_only!" var cipher = Crypto.createCipher(cryptoAlgorithm, key); var encrypted = cipher.update(raw, 'utf8', 'binary'); encrypted += cipher.final('binary'); var base = toBase64(encrypted); return base; }, decrypt: function(base64raw) { var raw = fromBase64(base64raw); var cryptoAlgorithm = "aes-256-ctr"; var key = "This_key_for_demo_purposes_only!" var decipher = Crypto.createDecipher(cryptoAlgorithm, key); var decrypted = decipher.update(raw, 'binary', 'utf8'); decrypted += decipher.final('utf8'); return decrypted; } 

JavaScript函数decrypt()的输出是无稽之谈。 不匹配在哪里?

给定相同的原始string时,两个encryption函数的输出不匹配。

我的猜测:在JavaScript中,我不应该使用“二进制”。 如果为从Python端生成的数据指定'hex' ,则JavaScript decrypt()函数会扼杀。