Функция расшифровки не работает в python, ниже приведен код
импортировать hashlib from binascii import hexlify из binascii import unhexlify
from Crypto.Cipher import AES
class AppCrypto: __KEY: str = None __iv = "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"
def __init__(self, key: str = None) -> None:
"""
Initialize the class.
:param key: The key for the Encryption and Decryption.
"""
self.__KEY = key
def pad(self, data: str):
"""
Pad the data to be encrypted.
:param data: The str data to be encrypted.
"""
length = 16 - (len(data) % 16)
data += chr(length) * length
return data
def unpad(data):
return data[0:-ord(data[-1])]
def __get_cipher(self) -> AES:
"""
Get the cipher object.
:param key: The key to be used for encryption.
"""
bytearrayKey = bytearray()
bytearrayKey.extend(map(ord, self.__KEY))
return AES.new(
hashlib.md5(bytearrayKey).digest(),
AES.MODE_CBC,
self.__iv.encode("utf-8"),
)
def encrypt(self, plainText: str) -> bytearray:
"""
:param data: The data to be encrypted.
:return: The encrypted data.
"""
plainText = self.pad(plainText)
enc_cipher = self.__get_cipher()
return hexlify(enc_cipher.encrypt(plainText.encode("utf-8"))).decode("utf-8")
def decrypt(self, encryptedText: str) -> str:
"""
:param data: The data to be decrypted.
:return: The decrypted data.
"""
dec_cipher = self.__get_cipher()
data = unhexlify(dec_cipher.decrypt(encryptedText).decode('utf-8')).encode("utf-8")
return self.unpad(data)
======================================
from AppCrypto import AppCrypto
appCrypto = AppCrypto("729C6F92987DCEF8D955D23ED2269354")
enc_data = appCrypto.encrypt("Hello, Ninranjan")
Выход - 0f657d48535896ab573a4cb7e9a967e8d409b7f4f537c3157949abd91e221bf2
plain_tx = appCrypto.decrypt("0f657d48535896ab573a4cb7e9a967e8d409b7f4f537c3157949abd91e221bf2")
выдает ошибку типа TypeError at /propertytax/receipt_entry/
Тип объекта <class 'str'> не может быть передан в код C
Метод запроса: GET URL запроса: http://192.168.1.10:90/propertytax/receipt_entry/?cmd=clear. Версия Django: 3.2.5 Тип исключения: TypeError Значение исключения:
Тип объекта <class 'str'> не может быть передан в код C
Exception Location: /usr/local/lib64/python3.6/site-packages/Crypto/Util/_raw_api.py, строка 143, in c_uint8_ptr Исполняемый файл Python: /usr/bin/python3 Версия Python: 3.6.8