在python中加密在Javascrip中解密

2024-04-26 13:22:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我只需要用python加密一些文本,然后用JavaScrypt解密。

到目前为止,我在python中有:

from Crypto import Random
from Crypto.Cipher import AES
import base64

BLOCK_SIZE = 16
key = "1234567890123456" # want to be 16 chars
textToEncrypt = "This is text to encrypt"

def encrypt(message, passphrase):
    # passphrase MUST be 16, 24 or 32 bytes long, how can I do that ?
    IV = Random.new().read(BLOCK_SIZE)
    aes = AES.new(passphrase, AES.MODE_CFB, IV)
    return base64.b64encode(aes.encrypt(message))

def decrypt(encrypted, passphrase):
    IV = Random.new().read(BLOCK_SIZE)
    aes = AES.new(passphrase, AES.MODE_CFB, IV)
    return aes.decrypt(base64.b64decode(encrypted))

print encrypt( textToEncrypt, key )

正在生成文本:ZF9as5JII5TlqcB5tAd4sxPuBXd5TrgE

在JavaScript中:

<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/aes.js"></script>
<script>
    var decrypted = CryptoJS.AES.decrypt( "ZF9as5JII5TlqcB5tAd4sxPuBXd5TrgE", "1234567890123456");
    console.log ( decrypted.toString( CryptoJS.enc.Utf8 ) );
</script>

但是它不会产生原始字符串(而是空字符串)。 我做错什么了?

专注于AES是个好主意吗-如果我有某种加密方法可以模糊数据,我会很高兴的。


Tags: from文本importnewsizescriptrandomblock