Image encryption / decryption using Python+OpenCV

Posted by we4freelance on Fri, 11 Feb 2022 07:15:28 +0100

Basic principle: logical XOR operation

Python is a programming language without much introduction; OpenCV is a cross platform computer vision and machine learning (partially open source) software library based on BSD license. At present, it has supported many languages, such as C, C + +, python, Java and MATLAB.

Here, choose one of the languages, Python, to demonstrate the whole operation process. The steps of other languages are roughly the same, so I won't go into too much detail.

First, let's look at a basic (binary) calculation problem to help you understand the following things:

[before looking at the question, if you don't know the rules of XOR operation, you can look at this truth table first]

(logical XOR truth table)

Number A = 1011 0011

Number B = 0100 1110

So, A xor B =? (Note: Here I use xor to represent the "logical xor" operator)

According to the truth table above, it is not difficult to find that the final result should be 1111 1101

The result is indeed calculated, but it is not enough. We have to observe a conclusion. Before that, we set C = 1111 1101 (that is, the result obtained above)

Then calculate C xor B=1011 0011. We set this result as D (i.e. D = 1011 0011). Through careful observation, it is not difficult to find that the value of D is equal to that of A

This is where the XOR operation is more subtle. Then I will express those a, B, C and D mentioned above in vivid words. A: The original text, B: key, C: ciphertext, D: plaintext, according to the above mathematical expression, can be converted into the following expression: the original text can be combined with the key to obtain the ciphertext (encrypted data), and then if the ciphertext is combined with the key, the plaintext (decrypted data) can be obtained, so as to realize the encryption / decryption of data.

In fact, pictures are stored in the form of 0 and 1 inside the computer, that is to say, we can process pictures in this way

Well, the theory exists. Let's start practice:

First, import the two libraries we will use next, namely OpenCV and NumPy libraries (for friends who do not install these two libraries, please refer to other tutorials for installation)

import cv2
import numpy

Then, we use random numbers to generate a Key (or we can directly use an image as the Key)

Key_1 = numpy.random.randint(0, 255, size=[300, 500], dtype=numpy.uint8)

In order to enable us to decrypt the pictures normally in the future, we should store the key in the file and keep it properly

cv2.imwrite("Key_1.jpg", Key_1)  # Save key

Open the output path of the key and you can see our key (also a picture)

(example of key generated with random number)

The next step is to load the image to be encrypted and display it

img_originalData = cv2.imread("demo.jpg", -1)
img_originalData = cv2.resize(img_originalData, (500, 300))
cv2.imshow("OriginalData", img_originalData)

(picture to be encrypted)

Because this is a color picture, we need to decompose the picture into three channels (Blue, Green and Red) before encryption, so as to process the three channels in subsequent operations

img_originalData_B = img_originalData[:, :, 0]
img_originalData_G = img_originalData[:, :, 1]
img_originalData_R = img_originalData[:, :, 2]

Then we use the key to perform logical XOR operation with the three channels respectively, and then merge the three channels

ciphertext_B = cv2.bitwise_xor(img_originalData_B, Key_1)
ciphertext_G = cv2.bitwise_xor(img_originalData_G, Key_1)
ciphertext_R = cv2.bitwise_xor(img_originalData_R, Key_1)
ciphertext_BGR = cv2.merge([ciphertext_B, ciphertext_G, ciphertext_R])

At this point, we will get an encrypted picture. Let's preview it and see the encryption effect

cv2.imshow("Ciphertext", ciphertext_BGR)  # Show encrypted pictures

(encrypted picture)

Can't you see what the original picture looks like at all? So far, the encryption of the picture has been completed

If you want to view the original image, you have to decrypt it with exactly the same key. The basic idea is the same as the encryption process, so you don't need to analyze it in detail, but go to the code directly

decryptedtext_B = cv2.bitwise_xor(ciphertext_B, Key_1)  # XOR the B-channel of the ciphertext with the key
decryptedtext_G = cv2.bitwise_xor(ciphertext_G, Key_1)  # XOR the G-channel of the ciphertext with the key
decryptedtext_R = cv2.bitwise_xor(ciphertext_R, Key_1)  # XOR the R channel of the ciphertext with the key
decryptedtext_BGR = cv2.merge([decryptedtext_B, decryptedtext_G, decryptedtext_R])  # Merge channel
cv2.imshow("Decryptedtext", decryptedtext_BGR)  # Show decrypted pictures

The following is the decrypted image. Is it exactly the same as the original image? So far, we have successfully realized the encryption and decryption of pictures

(decrypted image)

Finally: the encryption method demonstrated above is mainly operated according to the characteristics of logical XOR operation (OPenCV is mainly introduced to facilitate the operation of binary data of pictures). It is easy to realize through code, but it is not so easy to crack the encrypted pictures violently, and it can hardly be cracked successfully. There are many aspects of binary data that we can explore, such as an application of image bit plane decomposition: we can add digital watermark to the "least significant bit" bit plane of the image to realize the identification of copyright. The above mentioned are actually the basic applications of image processing. The later so-called higher-level usages are based on this foundation.




Topics: Python OpenCV image processing Encryption