AES-CFB8加解密【Python实现】

AES-CFB8加解密【Python实现】,第1张

AES-CFB8加解密【Python实现】

之前帮小伙伴用Python实现了AES-CFB8算法,需要自取

代码如下:

import json
from base64 import b64encode, b64decode
from Crypto.Cipher import AES
from Crypto.Random import get_random_bytes

def cfb_encrypt(data, key, iv=None):
    if iv is None:
        iv = get_random_bytes(16)
    cipher = AES.new(key, AES.MODE_CFB, iv, segment_size=8)
    ct_bytes = cipher.encrypt(data)
    ct = b64encode(ct_bytes).decode('utf-8')
    iv = b64encode(iv).decode('utf-8')
    return encrypt_data, iv
 
def cfb8_decrypt(ciphertext, iv, key):
    iv = b64decode(iv)
    ct = b64decode(ciphertext)
    cipher = AES.new(key, AES.MODE_CFB, iv, segment_size=8)
    decrypt_data = cipher.decrypt(ct)
    return decrypt_data
总结

希望可以帮助到大家。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5712183.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存