OpenCV-Python Development Guide (5) --- Image encryption and decryption

Bitwise XOR

To realize image encryption and decryption, we first need to master the bitwise XOR calculation method in mathematics.

The exclusive-or operation is also called half-add operation, and its algorithm is similar to binary addition without carry. In python, the XOR calculation is performed through the "^" symbol. Below, the blogger specifically lists a table to explain the bitwise XOR operation:

Arithmetic 1 Arithmetic 2 result python code
0 0 0 0^0
0 1 1 0^1
1 0 1 1^0
1 1 0 1^1

A simple summary of the rule of bitwise XOR operation is: the same two numbers operate as 0, and the different two numbers operate as 1. Bitwise XOR is not only used for image encryption and decryption, but also can be used to count different numbers.

What is image encryption and decryption

The definition of image encryption: through the bitwise XOR operation of the original image and the key image.

Definition of image decryption: Perform bitwise XOR operation on the encrypted image and the key image.

It can be seen from the image encryption and decryption that they are all the same operation.

We now stipulate that the literal symbol of XOR is xor. According to the above bitwise XOR operation, we assume:

xor(a,b)=c

You can get:

xor(c,b)=a

Or:

xor(c,a)=b

In summary, we assume that a is the original image data and b is the key, then c calculated by xor(a,c) is the encrypted ciphertext. Simply summarize encryption and decryption.

Encryption process: Perform a bitwise XOR operation on the image a and the key b to complete the encryption and obtain the ciphertext c.

Decryption process: Perform a bitwise XOR operation on the ciphertext c and the key b, complete the decryption, and get the image a.

Encrypt the image

Now that we have fully grasped the principles of image encryption and decryption, let's use code to implement an image encryption. Similarly, here we first obtain a grayscale image.

import cv2
import numpy as np

img = cv2.imread("4.jpg", 0)
r, c = img.shape
key = np.random.randint(0, 256, size=[r, c], dtype=np.uint8)
encryption = cv2.bitwise_xor(img, key)

cv2.imshow("111", encryption)
cv2.waitKey()
cv2.destroyAllWindows()

After running, we will get garbled images:
Encrypt the image

Decrypt the image

The image is decrypted through the bitwise XOR operation. Here we only need to perform the bitwise XOR between the encrypted image and the key. The complete code is as follows:

import cv2
import numpy as np

img = cv2.imread("4.jpg", 0)
r, c = img.shape
key = np.random.randint(0, 256, size=[r, c], dtype=np.uint8)
encryption = cv2.bitwise_xor(img, key)
decryption = cv2.bitwise_xor(encryption, key)
cv2.imshow("111", encryption)
cv2.imshow("222", decryption )
cv2.waitKey()
cv2.destroyAllWindows()

After running, we can get the original image and the encrypted image:
Comparison of original image and encrypted image

Guess you like

Origin blog.csdn.net/liyuanjinglyj/article/details/113774344