我想使用Rijndael加密并在python中使用密钥和256bit块大小,并且填充应为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)
答案 0 :(得分:1)
您要导入的rijndael
库是针对python 2编写的,但是您正在使用python 3运行它。请参见下面的内容,了解适用于python 2而非python 3的语法。
$ cat raise.py
raise Exception,"text"
$ python2 raise.py
Traceback (most recent call last):
File "raise.py", line 1, in <module>
raise Exception,"text"
Exception: text
$ python3 raise.py
File "raise.py", line 1
raise Exception,"text"
^
SyntaxError: invalid syntax
您可以尝试使用2to3
工具自己进行迁移,查看是否有人编写了端口,或者使用python 2编写并执行了程序。
先尝试pip2 install rijndael
,然后再尝试python2 testForRijndael.py
。
要在本地代码上运行2to3
(不建议这样做,但是可能会起作用),请运行2to3 -w /opt/odoo/odoo11-venv/lib/python3.6/site-packages/rijndael/**/*.py