gpt4 book ai didi

python - python中的Rijndael加密

转载 作者:太空宇宙 更新时间:2023-11-04 04:36:21 25 4
gpt4 key购买 nike

我想在 python 中使用带 key 和 block 大小为 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)

最佳答案

您从中导入的 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

关于python - python中的Rijndael加密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51619643/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com