python实现AES算法

pycrypto库下载:http://pan.baidu.com/s/1nvNtCvB (python2.7的版本),直接安装就可以了

#!/usr/bin/python
#-*- coding:utf-8-*-
from    Crypto.Cipher   import AES
from    binascii    import b2a_hex,a2b_hex

class   aes():#自己实现了一个aes类,用于aes的加密和解密
    def __init__(self,key,mode):#key为aes秘钥,mode为加密模式
        self.key = key
        self.mode = mode

    def encrypt(self,text,count):#text的为要加密的文本或者二进制流,count为加密数据的长度
        cryptor = AES.new(self.key,self.mode,b'0000000000000000')
        length = 16
        if count < length:
            add = (length-count)
            #\0 backspace
            text = text + ('\0' * add)
        elif count > length:
            add = (length-(count % length))
            text = text + ('\0' * add)
        self.cipherText = cryptor.encrypt(text)
        return b2a_hex(self.cipherText)#将内存中的数据已16进制字符串打印出来
    def decrypt(self,cipherText):#cipherText类型为16进制字符串如:"fa0345da",一般为32个字节长度
        cryptor = AES.new(self.key,self.mode,b'0000000000000000')
        plainText = cryptor.decrypt(a2b_hex(cipherText))#将秘钥转换为二进制流
        return plainText#返回值为二进制流,因为有很多不可打印字符

def     main():

        key = '1234567890123456' #秘钥的长度必须为16
        cryptor = aes(key,AES.MODE_ECB)#这里选用的是ECB模式
        msg = 'helloworld' #加密字符串
        cipher = cryptor.encrypt(msg,len(msg)) #返回值是内存中的16进制字符串
        print cipher
        plainText = cryptor.decrypt(cipher)#解密函数,秘钥在aes初始化的时候设置好了,aes是对称加密的,加密的秘钥和解密的秘钥是一样的
        print plainText
if      __name__=="__main__":
        main()

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值