AES對稱加密演算法實踐 python3

2021-09-29 10:33:43 字數 3552 閱讀 3342

下面是在python3中使用mode_cbc(需要salt)的最佳實踐

python2環境的見:

如果aes加密是mode_ecb模式,則不需要隨機向量,**中注釋部分

加解密過程:

通過 python encode.py 加密本地data目錄下的file.jpg檔案(二進位制),經過加密後生成'./data/file.jpg.cipher'

通過 python decode.py 解密本地密文'./data/file.jpg.cipher',生成明文檔案decrypted_file.jpg

通過md5對比file.jpg 和解密後的decrypted_file.jpg

加密模組:

1. 先隨機生成乙個256歲的隨機金鑰,存入本地檔案key中。

2. 隨機生成乙個16位的隨機向量salt存入密文檔案'./data/file.jpg.cipher'

3. 使用 key和salt 通過aes加密(mode_cbc模式)生成密文ciphertext

4. 將隨機向量salt 和ciphertext 經過base64編碼後按行寫入密文檔案

python3 加密**:

import os

import time

import traceback

import json

import base64

from crypto.cipher import aes

def readtextfile(in_file, mode):

file = open(in_file, mode)

content = file.read()

file.close()

return content

def writetextfile(out_file, lines):

file = open(out_file, 'w')

for ln in lines:

file.write(ln+'\n')

file.close()

def addto16(value):

length = 16 - (len(value) % 16)

value += bytes([length])*length

return value

def encryptdata(key,text):

#初始化加密器

aes = aes.new(key, aes.mode_cbc,iv)

# aes = aes.new(base64.b64decode(key), aes.mode_ecb)

#先進行 aes 加密,返回bytes

encryptaes = aes.encrypt(addto16(text))

#用 base64 轉成字串形式,用於寫入檔案

encrypted_text = str(base64.b64encode(encryptaes), encoding='utf-8')

return encrypted_text

def encrypt(key):

#python3 中以二進位制模式讀取

plaintext = readtextfile(in_file,'rb')

ciphertext = encryptdata(key, plaintext)

#將密文寫入檔案[aes加密使用的鹽|加密後的資料密文]

iv = os.urandom(16) #aes cbc模式加密使用的salt

#生成256 位金鑰

key = os.urandom(32)

with open("./key","wb+") as f:

f.write(key)

#使用金鑰進行加密

encrypt(key)

解密過程:1.  先讀取密文檔案decrypted_file.jpg,第一行位隨機向量salt,第二行位密文資料

2. 使用本地金鑰 key和salt 通過aes解密(mode_cbc模式)生成明文plaintext

3. 將明文以二進位制形式寫入檔案decrypted_file.jpg

python3 解密**:

import time

import json

import base64

import traceback

from crypto.cipher import aes

def readtextfile(in_file):

file = open(in_file, 'r')

lines =

for ln in file:

file.close()

return lines

def writetextfile(out_file, content):

file = open(out_file, 'wb')

file.write(content)

file.close()

def decryptdata(datakey, iv, ciphertext):

#初始化加密器

cryptos = aes.new(datakey, aes.mode_cbc, iv)

#執行解密運算,並將末尾為0的位元組去掉

#讀取本地儲存的加密檔案,檔案按行寫入格式:[aes加密使用的鹽|加密後的資料密文]

in_lines = readtextfile(in_file)

#讀取金鑰

key = open('./key', 'rb').read()

#使用明文資料金鑰和鹽解密密文資料

plain_text = decryptdata(key, base64.b64decode(in_lines[0]), base64.b64decode(in_lines[1]))

#將明文資料以二進位制形式寫入檔案,通過 md5sum 對比原始檔案和經過解密後的檔案

writetextfile(out_file, plain_text)

測試:

對比檔案,md5一致

aes 對稱加密演算法 rsa 非對稱加密演算法

aes 對稱加密演算法 加密和解密用到的金鑰是相同的,這種加密方式加密速度非常快,適合經常傳送資料的場合。缺點是金鑰的傳輸比較麻煩。金鑰長度可以為16,24或者32位元組 128,192,256位 根據金鑰的長度,演算法被稱為aes 128,aes 192或者ae 256。兩種模式 ecb cbc ...

AES加密演算法

aes加密演算法 加密模式 ecb模式 優點 1.簡單 2.有利於平行計算 3.誤差不會被傳送 缺點 1.不能隱藏明文的模式 2.可能對明文進行主動攻擊 cbc模式 優點 1.不容易主動攻擊,安全性好於ecb,適合傳輸長度長的報文,是ssl ipsec的標準。缺點 1.不利於平行計算 2.誤差傳遞 ...

AES加密演算法

aes對稱加密演算法下有好多種演算法,往往很難做到垮語言的加密解密,本文提供一套c 和node.js可以相互加密解密通用的 之aes 256 cbc演算法 1 aes所有的鑰匙必須 128位 16位元組 192位 24位元組 或256位 32位元組 長 2 有幾種操作模式,每個都有不同的優點和缺點。...