python Crypto encryption and decryption

The film written records of Crypto using python tool for pictures or text encryption and decryption methods:

import numpy as np
from PIL import Image
from base64 import b64encode, b64decode
from Crypto.Cipher import  AES


BS = 16
iv = 16 * b'\0'
key = b'25jkUjx14hkc@q58gxU3mcaaaaaaaaaa'
imgPath = './test/data/0_biaoge.jpg'

# pad for length not multiple of string
pad_txt = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS)

# jiami
obj = AES.new(key, AES.MODE_CBC, iv)
img = open(imgPath, 'rb').read()
img_base64 = b64encode(img)
img_base64_str = str(img_base64, encoding='utf-8')
data_jiami = obj.encrypt(pad_txt(img_base64_str))


# jiemi
obj2 = AES.new(key, AES.MODE_CBC, iv)
data_jiemi = obj2.decrypt(data_jiami)
STR = data_jiemi_str (data_jiemi, encoding = 'UTF-. 8'). Replace ( '\ X04', '') 
data_jiemi_base64 = b64decode (data_jiemi_str) 
IMG = np.asarray (ByteArray (data_jiemi_base64), DTYPE = "uint8")

  

 

Guess you like

Origin www.cnblogs.com/Fosen/p/11586149.html