赞
踩
测试过程中需要对加密流量包进行加解密,有时候js太大调试过程中浏览器器会卡死。需要手动编写一个加解密工具对加解密信息处理。
小工具是使用python3编写的
python3 aes_tool.py encrypt "Hello, World!" key.txt --mode cfb
python3 aes_tool.py decrypt "encrypted_text" key.txt --mode ecb
可以将AESkey秘钥保存到key.txt文件中
import base64 # 导入加密库 from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes from cryptography.hazmat.backends import default_backend # 导入命令行解析的库 import argparse def read_key_from_file(file_path): with open(file_path, 'rb') as file: return file.read() # 加密函数 def encrypt(text, key, mode): key = read_key_from_file(key) key = key.ljust(32)[:32] iv = b'1234567890123456' # 目前支持的AES解密模式,CFB模式和ECB模式 if mode == 'ecb': cipher = Cipher(algorithms.AES(key), modes.ECB(), backend=default_backend()) elif mode == 'cfb': cipher = Cipher(algorithms.AES(key), modes.CFB(iv), backend=default_backend()) else: raise ValueError('Unsupported encryption mode') encryptor = cipher.encryptor() ciphertext = encryptor.update(text.encode()) + encryptor.finalize() return base64.b64encode(ciphertext).decode() # 解密函数 def decrypt(ciphertext, key, mode): key = read_key_from_file(key) key = key.ljust(32)[:32] iv = b'1234567890123456' if mode == 'ecb': cipher = Cipher(algorithms.AES(key), modes.ECB(), backend=default_backend()) elif mode == 'cfb': cipher = Cipher(algorithms.AES(key), modes.CFB(iv), backend=default_backend()) else: raise ValueError('Unsupported encryption mode') decryptor = cipher.decryptor() plaintext = decryptor.update(base64.b64decode(ciphertext)) + decryptor.finalize() return plaintext.decode() def main(): # 支持的命令行 parser = argparse.ArgumentParser(description='AES加解密工具') parser.add_argument('action', choices=['encrypt', 'decrypt'], help='加密或解密') parser.add_argument('text', help='要加密或解密的字符串') parser.add_argument('key', help='AES加解密的密钥文件路径') parser.add_argument('--mode', choices=['ecb', 'cfb'], default='cfb', help='加密模式,默认为CFB模式') args = parser.parse_args() if args.action == 'encrypt': result = encrypt(args.text, args.key, args.mode) print('加密结果:', result) elif args.action == 'decrypt': result = decrypt(args.text, args.key, args.mode) print('解密结果:', result) if __name__ == '__main__': main()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。