python 实现 AES ECB模式加解密

python 实现 AES ECB模式加解密,第1张

概述AES ECB模式加解密 使用cryptopp完成AES的ECB模式进行加解密。 AES加密数据块分组长度必须为128比特,密钥长度可以是128比特、192比特、256比特中的任意一个。(8比特 == 1字节) 在CBC、CFB、OFB、CTR模式下除了密钥外,还需要一个初始化向IV。(ECB模式不用IV)   代码:    # -*- coding=utf-8-*-from Crypto.C

AES ECB模式加解密
使用cryptopp完成AES的ECB模式进行加解密。

AES加密数据块分组长度必须为128比特,密钥长度可以是128比特、192比特、256比特中的任意一个。(8比特 == 1字节)

在CBC、CFB、OFB、CTR模式下除了密钥外,还需要一个初始化向IV。(ECB模式不用IV)

 

代码:

  

# -*- Coding=utf-8-*-from Crypto.Cipher import AESimport osfrom Crypto import Randomimport base64"""aes加密算法padding : PKCS7"""class AESUtil:    __BLOCK_SIZE_16 = BLOCK_SIZE_16 = AES.block_size    @staticmethod    def encryt(str,key,iv):        cipher = AES.new(key,AES.MODE_ECB,iv)        x = AESUtil.__BLOCK_SIZE_16 - (len(str) % AESUtil.__BLOCK_SIZE_16)        if x != 0:            str = str + chr(x)*x        msg = cipher.encrypt(str)        # msg = base64.urlsafe_b64encode(msg).replace(‘=‘,‘‘)        msg = base64.b64encode(msg)        return msg    @staticmethod    def decrypt(enStr,iv)        # enStr += (len(enStr) % 4)*"="        # decryptByts = base64.urlsafe_b64decode(enStr)        decryptByts = base64.b64decode(enStr)        msg = cipher.decrypt(decryptByts)        paddingLen = ord(msg[len(msg)-1])        return msg[0:-paddingLen]if __name__ == "__main__":    key = "1234567812345678"    iv = "1234567812345678"    res = AESUtil.encryt("123456",iv)    print res # mdsm0RmB+xAKrTah3DG31A==    print AESUtil.decrypt(res,iv) # 123456
总结

以上是内存溢出为你收集整理的python 实现 AES ECB模式加解密全部内容,希望文章能够帮你解决python 实现 AES ECB模式加解密所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

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

原文地址: https://outofmemory.cn/langs/1191623.html

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

发表评论

登录后才能评论

评论列表(0条)

保存