Python AES加密

一.初級加密(此方法同樣適用於linux系統)
1.python環境:3.5.3(python3的環境必須導入這兩個包才能使用)
2.安裝模塊:pip install Crypto
3.再安裝pycrypto:pip install pycrypto(此模塊在3.4之後已經不再使用,使用下面的模塊代替其功能)
4.或者可以安裝pycryptodome:pip install pycryptodome(此模塊是3.4之後AES加密模塊的一個分支)
二.python3.6.5版本無法使用該模塊(No module named Crypto.Cipher)
1.公司給我新配的電腦,然後就開始在上面安裝各種包,在導入AES模塊的過程中,提示一直找不到Crypto這個包,我就很納悶,在之前我個人的電腦python3.5.3的版本運行都是正常的,怎麼到這就不行了呢?
2.之前在網上有看到說安裝這個模塊可能下載的是小寫的,需要將下載的包改寫成大寫,然後我就通過查看文件路徑找到安裝包的目錄

Lib\site-packages

找到 crypto 這個庫,更改爲首字母大寫 Crypto 即可

這裏發現安裝包真的是小寫的,而我導入的包是大寫的,我就將安裝包改寫成大寫,重新運行一下程序,OK,正常了!!!

還有一種方法就是:

pip install pycryptodome

安裝這個庫就可以了。因爲 pycryptodome pycrypto 這兩個庫是同一個庫,但是 pycrypto 已經不維護了。

pycrypto 這個庫用的話是需要更改庫名爲大寫,不知道爲什麼作者這麼搞,非要更改一下庫名,不更改的話,你會發現庫裏面的引用都是問題

from Crypto.Cipher import AES
import base64

class prpcrypt():
    def __init__(self):
        # key值(密碼)
        self.key = '***************'.encode("utf-8")  # 因爲在python3中AES傳入參數的參數類型存在問題,需要更換爲 bytearray , 所以使用encode編碼格式將其轉爲字節格式(linux系統可不用指定編碼)
        # vi偏移量
        self.iv = '****************'.encode("utf-8")  # 編碼
        self.mode = AES.MODE_CBC
        self.BS = AES.block_size
        self.pad = lambda s: s + (self.BS - len(s) % self.BS) * chr(self.BS - len(s) % self.BS)
        self.unpad = lambda s: s[0:-ord(s[-1])]

    # 加密
    def encrypt(self, text):
        text = self.pad(text).encode("utf-8")
        cryptor = AES.new(self.key, self.mode, self.iv)
        # 目前AES-128 足夠目前使用(CBC加密)
        ciphertext = cryptor.encrypt(text)
        # base64加密
        return base64.b64encode(bytes(ciphertext))

    # 解密
    def decrypt(self, text):
        # base64解密
        text = base64.b64decode(text)
        cryptor = AES.new(self.key, self.mode, self.iv)
        # CBC解密
        plain_text = cryptor.decrypt(text)
        # 去掉補足的空格用strip() 去掉
        return self.unpad(bytes.decode(plain_text).rstrip('\0'))    # 解密字節???


if __name__ == '__main__':
    pc = prpcrypt()  # 初始化密鑰 和 iv
    # text='access&a494fcbc-9aa1-4718-bd7d-a90d01211d97&0&2&'
    text = 'access&a494fcbc-9aa1-4718-bd7d-a90d01211d97&0&1&'
    # text='access&a494fcbc-9aa1-4718-bd7d-a90d01211d97&1&1&'
    # text='access&a494fcbc-9aa1-4718-bd7d-a90d01211d97&1&2&'
    # text='update&a494fcbc-9aa1-4718-bd7d-a90d01211d97&0&2&'
    # text='update&a494fcbc-9aa1-4718-bd7d-a90d01211d98&0&2&'
    # text='logout&'
    e = pc.encrypt(text + str(int(time.time() / 10)))  # 加密
    d = pc.decrypt(e)  # 解密
    print("加密:%s" % e.decode("utf-8"))
    print("解密:%s"% d)
    print("長度:%s"% len(d))

二.高級加密
python環境:2.7.3(也可以是3.5.3環境)

# -*- coding:utf-8 -*-
from Crypto.Cipher import AES  
from binascii import b2a_hex, a2b_hex  
import sys,time
import base64


class prpcrypt():  
    def __init__(self):
        # key值(密碼)  
        self.key = '******************'
        # vi偏移量  
        self.iv  = '****************'  
        self.mode = AES.MODE_CBC  
        self.BS = AES.block_size  
        self.pad = lambda s: s + (self.BS - len(s) % self.BS) * chr(self.BS - len(s) % self.BS)   
        self.unpad = lambda s : s[0:-ord(s[-1])]  
    
    # 加密   
    def encrypt(self,text):  
        text = self.pad(text)  
        cryptor = AES.new(self.key,self.mode,self.iv)  
        # 目前AES-128 足夠目前使用(CBC加密) 
        ciphertext = cryptor.encrypt(text)  
        # base64加密
        return base64.b64encode(bytes(ciphertext))
       
    # 解密
    def decrypt(self,text):
        # base64解密
        text = base64.b64decode(text)    
        cryptor = AES.new(self.key,self.mode, self.iv)
        # CBC解密
        plain_text  = cryptor.decrypt(text)
        # 去掉補足的空格用strip() 去掉  
        return self.unpad(plain_text.rstrip('\0'))  
  
   
if __name__ == '__main__':
    pc = prpcrypt() #初始化密鑰 和 iv
    #text='access&a494fcbc-9aa1-4718-bd7d-a90d01211d97&0&2&'
    text='access&a494fcbc-9aa1-4718-bd7d-a90d01211d97&0&1&'
    #text='access&a494fcbc-9aa1-4718-bd7d-a90d01211d97&1&1&'
    #text='access&a494fcbc-9aa1-4718-bd7d-a90d01211d97&1&2&'
    #text='update&a494fcbc-9aa1-4718-bd7d-a90d01211d97&0&2&'
    #text='update&a494fcbc-9aa1-4718-bd7d-a90d01211d98&0&2&'
    #text='logout&'
    e = pc.encrypt(text+str(int(time.time()/10))) #加密      
    d = pc.decrypt(e) #解密 
    print "加密:",e  
    print "解密:",d  
    print "長度:",len(d)  

原文 :https://www.jianshu.com/p/123ae4b5684f 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章