我想使用Rijndael加密与密钥和块256位大小的python和填充应该是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)发布于 2018-08-01 02:51:02
您要导入的rijndael库是为Python2编写的,但您正在使用Python3运行它。请参阅下面的语法,了解适用于Python2但不适用于Python3的语法。
$ 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工具,查看是否有人编写了一个端口,或者使用Python2编写并执行您的程序。
尝试pip2 install rijndael,然后尝试python2 testForRijndael.py。
要在本地代码上运行2to3 (实际上不推荐这样做,但它可能会起作用),请运行2to3 -w /opt/odoo/odoo11-venv/lib/python3.6/site-packages/rijndael/**/*.py
https://stackoverflow.com/questions/51619643
复制相似问题