当前位置:   article > 正文

python实现AES加解密小工具_aes加解密工具

aes加解密工具

需求

测试过程中需要对加密流量包进行加解密,有时候js太大调试过程中浏览器器会卡死。需要手动编写一个加解密工具对加解密信息处理。

实现

小工具是使用python3编写的

使用

python3 aes_tool.py encrypt "Hello, World!" key.txt --mode cfb
python3 aes_tool.py decrypt "encrypted_text" key.txt --mode ecb
  • 1
  • 2

可以将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()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/正经夜光杯/article/detail/794777
推荐阅读
相关标签
  

闽ICP备14008679号