图像加密与解密
通过异或运算实现简单的加密与解密
加密原理:
将明文a与密钥b进行按位异或运算,完成加密,得到密文c
解密原理:
将密文c与密钥b进行按位异或运算,完成解密,得到明文a
示例代码
import cv2
import numpy as np
lena = cv2.imread("lena.jpg",0)
r,c = lena.shape
key = np.random.randint(0,256,size=[r,c],dtype=np.uint8)
encryption = cv2.bitwise_xor(lena,key)
decryption = cv2.bitwise_xor(encryption,key)
cv2.imshow("lena",lena)
cv2.imwrite("yuanshi.jpg",lena)
cv2.imshow("key",key)
cv2.imshow("encryption",encryption)
cv2.imwrite("jiamihou.jpg",encryption)
cv2.imshow("decryption",decryption)
cv2.imwrite("jiemihou.jpg",decryption)
cv2.waitKey()
cv2.destoryAllWindows()
结果
|
|
|