java 的cipher AES⼀CBC⼀PKCS5Padding 加密后,使用openssl的AES_cbc_encrypt无法解密

2024-12-04 19:50:55
推荐回答(1个)
回答1:

你看一下这个例子吧。可以参考下面的地址:前面加上http,把句号改成点。

likang。me/blog/2013/06/05/python-pycrypto-aes-ecb-pkcs-5/

# -*- coding: utf-8 -*-
from Crypto.Cipher import AES
import os

BS = AES.block_size
pad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS)
unpad = lambda s : s[0:-ord(s[-1])]

key = os.urandom(16) # the length can be (16, 24, 32)
text = 'to be encrypted'

cipher = AES.new(key)

encrypted = cipher.encrypt(pad(text)).encode('hex')
print encrypted # will be something like 'f456a6b0e54e35f2711a9fa078a76d16'

decrypted = unpad(cipher.decrypt(encrypted.decode('hex')))
print decrypted # will be 'to be encrypted'