程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python AES encryption and decryption of pictures

編輯:Python
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from Crypto.Cipher import AES
import base64
PADDING = '\0'
#PADDING = ' '
pad_it = lambda s: s+(16 - len(s)%16)*PADDING
key = '1234567812345678'
iv = '1234567812345678'
source = 'Test String'
generator = AES.new(key, AES.MODE_CBC, iv)
crypt = generator.encrypt(pad_it(source))
cryptedStr = base64.b64encode(crypt)
print(cryptedStr)
generator = AES.new(key, AES.MODE_CBC, iv)
recovery = generator.decrypt(crypt)
print(recovery.decode('utf8').strip())
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from Crypto.Cipher import AES
import matplotlib.pyplot as plt
from PIL import Image
from io import BytesIO
import base64
PADDING = '\0'
#PADDING = ' '
pad_it = lambda s: s+(16 - len(s)%16)*PADDING
key = '1234567812345678'
iv = '1234567812345678'
f = open(r"C:\Users\UZC1SZH\Pictures\Chrysanthemum_en.jpg",'rb+')
byt = f.read()
generator = AES.new(key, AES.MODE_CBC, iv)
recovery = generator.decrypt(byt)
with open('res.jpg', 'wb') as f:
f.write(recovery)
# The binary data of the picture Converted to an array
img_data = plt.imread(BytesIO(recovery),"jpg")
print(type(img_data))
print(img_data.shape)
img = Image.fromarray(img_data, 'RGB')
img.show()
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from Crypto.Cipher import AES
import matplotlib.pyplot as plt
from PIL import Image
from io import BytesIO
key = '1234567812345678'
iv = '1234567812345678'
def pad(s):
x = AES.block_size - len(s) % AES.block_size
return s + ((bytes([x])) * x)
# encode file
f = open(r"org.jpg", 'rb')
bytes1 = f.read()
generator = AES.new(key.encode('utf-8'), AES.MODE_CBC, iv.encode('utf-8'))
encodedimg = generator.encrypt(pad(bytes1))
with open('res/res.jpg', 'wb') as f:
f.write(encodedimg)
#decode file
f = open(r"D:\project\cuicui\demo\res\res.jpg", 'rb+')
encodedimg = f.read()
generator = AES.new(key.encode('utf-8'), AES.MODE_CBC, iv.encode('utf-8'))
recovery = generator.decrypt(encodedimg)
# with open('res/res2.PNG', 'wb') as f:
# f.write(recovery)
imgdata = plt.imread(BytesIO(recovery), 'jpg')
img = Image.fromarray(imgdata, "RGB")
img.show()


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved