如何在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()
函数会扼杀。
- 解密来自PHP RIJNDAEL_128 CBC的node.js中的string
- 用python和nodejsencryption和解密
- 适用于NodeJs和mbedtls的encryption程序
- 尝试使用crypto-js和nodejs进行解密
- Node.jsencryptioninput/输出types
- 节点encryption解码stream抛出EVP_DecryptFinal_ex:如果stream将被中断,则错误的最终块长度
- Node.js Crypto AES密码
- AES CryptoJSencryption和phpseclib解密
- 使用phpseclib使用AES进行encryption,并使用CryptoJS进行解密