python 实现aes加密解密 ecb模式和其他模式

Posted 徐小白

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 实现aes加密解密 ecb模式和其他模式相关的知识,希望对你有一定的参考价值。

ecb模式:(这种不需要偏移向量iv,安全性较低,代码出处忘了)

# -*- coding=utf-8-*-
from Crypto.Cipher import AES
import os
from Crypto import Random
import base64
from Crypto.Cipher import AES
from binascii import b2a_hex, a2b_hex

"""
aes加密算法
ECB模式
"""


def add_to_16(text):
    if len(text.encode(utf-8))%16:
        add = 16 - len(text.encode(utf-8)) % 16
    else:
        add = 0
    text = text + (""*add)
    return text.encode(utf-8)


def encrypt(text):
    key = "1234567890123456".encode(utf-8)
    mode = AES.MODE_ECB
    text = add_to_16(text)
    cryptos = AES.new(key, mode)
    cipher_text = cryptos.encrypt(text)
    return b2a_hex(cipher_text)


def decrypto(text):
    key = "1234567890123456".encode(utf-8)
    mode = AES.MODE_ECB
    cryptor = AES.new(key,mode)
    plain_text = cryptor.decrypt(a2b_hex(text))
    return bytes.decode(plain_text).rstrip()


text = open(rxxxxxx, r, encoding=utf-8).read()
print(text[:100])
d = decrypto(text)
f = open(xxxxxxxxx1, w+)
f.write(d)
f.close()

 

其他模式:参考百度,没有测试。

https://www.jianshu.com/p/d18c13681bbc

以上是关于python 实现aes加密解密 ecb模式和其他模式的主要内容,如果未能解决你的问题,请参考以下文章

python 实现AES加解密

python 实现AES加解密

python3 aes加解密代码

Js AES(ECB模式)加密和解密

AES 对称加密中的 ECB 实现

iOS AES128加密解密的两种模式(CBC和ECB)