Python實現AES加密與解密

2021-10-11 09:29:39 字數 3133 閱讀 2394

原文:

aes加密方式有五種:ecb, cbc, ctr, cfb, ofb

python 在windows下使用aes時要安裝的是pycryptodome 模組   pip install pycryptodome 

python 在linux下使用aes時要安裝的是pycrypto模組   pip install pycrypto 

cbc加密需要乙個十六位的key(金鑰)和乙個十六位iv(偏移量)

ecb加密不需要iv

aes cbc 加密的python實現

# pip install pycryptodome

from crypto.cipher import aes

from binascii import b2a_hex, a2b_hex

class encryptdecode:

# 如果text不足16位的倍數就用空格補足為16位

@staticmethod

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 + ('\0' * add)

return text.encode('utf-8')

# 加密函式

def encrypt(self, text):

key = 'vekg**tw3ax20uds'.encode('utf-8')

mode = aes.mode_cbc

iv = b'zlk5fas7v628jiqx'

text = encryptdecode.add_to_16(text)

cryptos = aes.new(key, mode, iv)

cipher_text = cryptos.encrypt(text)

# 因為aes加密後的字串不一定是ascii字符集的,輸出儲存可能存在問題,所以這裡轉為16進製制字串

return b2a_hex(cipher_text)

# 解密後,去掉補足的空格用strip() 去掉

def decrypt(self, text):

key = 'vekg**tw3ax20uds'.encode('utf-8')

iv = b'zlk5fas7v628jiqx'

mode = aes.mode_cbc

cryptos = aes.new(key, mode, iv)

plain_text = cryptos.decrypt(a2b_hex(text))

return bytes.decode(plain_text).rstrip('\0')

if __name__ == '__main__':

e = encryptdecode().encrypt("123abc") # 加密

d = encryptdecode().decrypt(e) # 解密

print("加密:", e)

print("解密:", d)

結果:

加密: b'7507349391e0709c56de8b41cda5815d'

解密: admin123abc

位元組與字串相互轉換:

print('*'*60)

print(type(e)) # bytes_to_str = str(e, encoding="utf-8")

print(bytes_to_str) # 23656432beed7f9f17a91de0469c4a2e

str_to_bytes = bytes(bytes_to_str, encoding="utf8")

print(str_to_bytes) # b'23656432beed7f9f17a91de0469c4a2e'

aes ecb加密的python實現

"""

ecb沒有偏移量

"""from crypto.cipher import aes

from binascii import b2a_hex, a2b_hex

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 + ('\0' * add)

return text.encode('utf-8')

# 加密函式

def encrypt(text):

key = '9999999999999999'.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)

# 解密後,去掉補足的空格用strip() 去掉

def decrypt(text):

key = '9999999999999999'.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('\0')

if __name__ == '__main__':

e = encrypt("hello world") # 加密

d = decrypt(e) # 解密

print("加密:", e)

print("解密:", d)

原文:

python實現AES加密與解密

aes加密方式有五種 ecb,cbc,ctr,cfb,ofb python 在 windows下使用aes時要安裝的是pycryptodome 模組 pip install pycryptodome python 在 linux下使用aes時要安裝的是pycrypto模組 pip install p...

Python 實現 AES 加密 解密

一 前言 金鑰 k 用來加密明文的密碼,在對稱加密演算法中,加密與解密的金鑰是相同的。金鑰為接收方與傳送方協商產生,但不可以直接在網路上傳輸,否則會導致金鑰洩漏,通常是通過非對稱加密演算法加密金鑰,然後再通過網路傳輸給對方,或者直接面對面商量金鑰。金鑰是絕對不可以洩漏的,否則會被攻擊者還原密文,竊取...

Python實現AES加密解密

報錯 typeerror decrypt cannot be called after encrypt 原因 cryptor不能寫在主函式中同時給加密函式與解密函式使用,儘管加密解密都是用 cryptor aes.new key,mode,iv 但若將其定義在main並分別傳給加密解密同時使用時,會...