from Crypto.Cipher import AES
import base64
class PrpCrypt(object):
def __init__(self, key, iv):
self.key = key
self.iv = iv
self.mode = AES.MODE_CBC
# pkcs7填充函数:
@staticmethod
def pkcs7_padding(in_bytes: str):
pad_len: int = 16 - len(in_bytes) % 16
if pad_len == 0:
pad_len = 16
in_str: str = str(in_bytes) + str(hex(pad_len) * pad_len).replace('0x', '')
return in_str
# pkcs7反填充函数:
@staticmethod
def pkcs7_unpadding(in_hex_str: str) -> str:
end_str: str = in_hex_str[-2:]
if end_str == '01':
end_str: str = in_hex_str[0:-2]
else:
num: int = int(end_str[-2:], 16)
end_str: str = in_hex_str[0:-(2 * num)]
return end_str
# AES加密函数:
def encrypt(self, text: bytes):
# Encrypt the padded plaintext bytes with the key and IV.
ciphertext = AES.new(self.key, self.mode, self.iv)
output_bytes: bytes = ciphertext.encrypt(bytes(text))
return output_bytes
# AES解密函数:
def decrypt(self, text: bytes):
plaintext = AES.new(self.key, self.mode, self.iv)
output_bytes = plaintext.decrypt(bytes(text))
return output_bytes
if __name__ == '__main__':
# Read a text string from the console input.
p_str: str = input('plaintext input:')
# Read a Base64 string from the console input.
key_b64: str = input('key input:')
key: bytes = base64.b64decode(key_b64)
IV_b64: str = input('IV input:')
IV: bytes = base64.b64decode(IV_b64)
# 异常处理函数:
def judge(bytes_1: bytes, bytes_2: bytes):
if len(bytes_1) % 24 != 0:
if len(bytes_1) % 16 != 0:
raise Exception('key length mismatch')
if len(bytes_2) % 16 != 0:
raise Exception('IV length mismatch ')
judge(key, IV)
pc = PrpCrypt(key, IV) # 初始化密钥
# Encode the text string with utf-8 encoding, as the plaintext bytes.
plaintext_str: str = p_str.encode('utf-8').hex()
# Print the padded bytes as a hex string.
print(pc.pkcs7_padding(plaintext_str))
padded_plaintext: bytes = bytes.fromhex(pc.pkcs7_padding(plaintext_str))
# Encrypt the padded plaintext bytes with the key and IV.
ciphertext: str = base64.b64encode(pc.encrypt(padded_plaintext)).decode('utf-8')
# Print the ciphertext bytes as a Base64 string.
print(ciphertext)
# Decrypt the ciphertext bytes with the key and IV.
ciphertext_bytes: bytes = bytes.fromhex(base64.b64decode(ciphertext).hex())
plaintext: str = base64.b64encode(pc.decrypt(ciphertext_bytes)).decode('utf-8')
if base64.b64decode(plaintext).hex() == ciphertext:
print('identical')
else:
print('not identical')
# Print the unpadded bytes as a hex string.
print(pc.pkcs7_unpadding(base64.b64decode(plaintext).hex()))
# Decode the unpadded bytes with utf-8 encoding, and print the decoded text string.
text_str: str = pc.pkcs7_unpadding(base64.b64decode(plaintext).hex())
text_string: str = bytes.fromhex(text_str).decode('utf-8')
print(text_string)

timerring
- 粉丝: 18w+
最新资源
- 国家开放大学网络核心课程运行探究.docx
- XX软件有限公司人力资源规划(doc).doc
- 数学教学中如何运用计算机技术.docx
- 单片机任务书(寻迹小车设计).doc
- 云计算加速未来.pptx
- 物联网智慧社区云对讲系统技术方案.doc
- 机械手设计方案论文-关于PLC控制的智能机械手设计方案探究.doc
- 基于神经网络模型的空燃比非线性模型预测控制.docx
- 大学计算机考试试题.doc
- 电子商务行业发展研究报告.pptx
- 物联网与工业自动化的关系.ppt
- 计算机网络通信协议的分析研究.docx
- C语言课程设计方案:学生宿舍管理系统[].doc
- 基于RFID的传感器网络.doc
- 信息化教学设计实施方案高中语文《再别康桥》.doc
- 粉色花卉水彩卡通信纸word信纸模板.docx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈



评论0