【发布时间】:2018-07-31 18:13:32
【问题描述】:
我想在 python 中使用带有密钥和块 256 位大小的 Rijndael 加密,填充应该是 PKCS7。要么使用 utf-8 编码。 我搜索了很多,最后写了这段代码,我不知道这是一个好方法,但我知道的就这么多。运行代码时出现此错误:
Traceback (most recent call last):
File "testForRijndael.py", line 1, in <module>
from rijndael.cipher import crypt
File "/opt/odoo/odoo11-venv/lib/python3.6/site-
packages/rijndael/cipher/crypt.py", line 1, in <module>
from rijndael.cipher.blockcipher import *
File "/opt/odoo/odoo11-venv/lib/python3.6/site-
packages/rijndael/cipher/blockcipher.py", line 64
raise Exception,"the IV length should be %i bytes"%self.blocksize
^
SyntaxError: invalid syntax
如果有人可以帮助我,我会很感激他/她
这是我的代码:
from rijndael.cipher import crypt
from rijndael.cipher.blockcipher import MODE_CBC
from pkcs7 import PKCS7Encoder
class Rijndael():
def __init__(self, key, iv):
self.KEY = key
self.IV = iv
self.BLOCKSIZE = 32
def encrypt(self, plain_text):
rjn = crypt.new(self.KEY, MODE_CBC , self.IV,
blocksize=self.BLOCKSIZE)
pad_text = PKCS7Encoder.encode(plain_text)
return rjn.encrypt(pad_text).encode()
def decrypt(self, cipher_text):
rjn = crypt.new(self.KEY, MODE_CBC , self.IV,
blocksize=self.BLOCKSIZE)
cipher_text = cipher_text.decode()
return rjn.decrypt(cipher_text)
r = Rijndael('abcdefghijklmnopqrstuvwxyz123456',
'abcdefghijklmnopqrstuvwxyzgh3456')
test_text = "this is a test :)"
encrypt = r.encrypt(test_text)
decrypt = r.decrypt(encrypt)
print(test_text)
print(encrypt)
print(decrypt)
【问题讨论】:
标签: python encryption aes rijndael