📜  使用Python加密和解密图像

📅  最后修改于: 2022-05-13 01:55:43.028000             🧑  作者: Mango

使用Python加密和解密图像

在本文中,我们将使用简单的数学逻辑加密/解密图像。它需要两件事,数据和密钥,当对两个操作数(即数据和密钥)应用异或运算时,数据会被加密,但是当使用相同的密钥值再次执行相同的过程时,数据会被解密。

加密

这只不过是一个简单的过程,在这个过程中,我们将我们的数据或信息转换为密码,以防止未经授权的访问并保持其私密性和安全性。

首先,我们将选择一个图像,然后将该图像转换为字节数组,因此图像数据将完全转换为数字形式,然后我们可以轻松地对其进行 XOR 操作。现在,每当我们对字节数组的每个值应用 XOR函数时,数据都会发生变化,因此我们将无法访问它。但是我们应该记住一件事,在这里我们的加密密钥起着非常重要的作用,没有该密钥我们就无法解密我们的图像。它充当密码来解密它。

下面的程序描述了加密的基本方法:

Python3
# Assign values
data = 1281
key = 27
 
# Display values
print('Original Data:', data)
print('Key:', key)
 
# Encryption
data = data ^ key
print('After Encryption:', data)
 
# Decryption
data = data ^ key
print('After Decryption:', data)


Python3
# try block to handle exception
try:
    # take path of image as a input
    path = input(r'Enter path of Image : ')
     
    # taking encryption key as input
    key = int(input('Enter Key for encryption of Image : '))
     
    # print path of image file and encryption key that
    # we are using
    print('The path of file : ', path)
    print('Key for encryption : ', key)
     
    # open file for reading purpose
    fin = open(path, 'rb')
     
    # storing image data in variable "image"
    image = fin.read()
    fin.close()
     
    # converting image into byte array to
    # perform encryption easily on numeric data
    image = bytearray(image)
 
    # performing XOR operation on each value of bytearray
    for index, values in enumerate(image):
        image[index] = values ^ key
 
    # opening file for writing purpose
    fin = open(path, 'wb')
     
    # writing encrypted data in image
    fin.write(image)
    fin.close()
    print('Encryption Done...')
 
     
except Exception:
    print('Error caught : ', Exception.__name__)


Python3
# try block to handle the exception
try:
    # take path of image as a input
    path = input(r'Enter path of Image : ')
     
    # taking decryption key as input
    key = int(input('Enter Key for encryption of Image : '))
     
    # print path of image file and decryption key that we are using
    print('The path of file : ', path)
    print('Note : Encryption key and Decryption key must be same.')
    print('Key for Decryption : ', key)
     
    # open file for reading purpose
    fin = open(path, 'rb')
     
    # storing image data in variable "image"
    image = fin.read()
    fin.close()
     
    # converting image into byte array to perform decryption easily on numeric data
    image = bytearray(image)
 
    # performing XOR operation on each value of bytearray
    for index, values in enumerate(image):
        image[index] = values ^ key
 
    # opening file for writing purpose
    fin = open(path, 'wb')
     
    # writing decryption data in image
    fin.write(image)
    fin.close()
    print('Decryption Done...')
 
 
except Exception:
    print('Error caught : ', Exception.__name__)


输出:

Original Data: 1281
Key: 27
After Encryption: 1306
After Decryption: 1281

在上面的程序中,我们可以看到 XOR 操作的工作原理,它需要两个变量datakey,每当我们第一次对它们进行 XOR 操作时,我们都会得到加密数据。然后当我们再次在我们的数据密钥之间执行 XOR 操作时,我们得到与输入变量数据(解密数据)相同的值。在加密和解密期间,相同的逻辑将适用于 Image 的字节数组。

加密的可执行代码:

蟒蛇3

# try block to handle exception
try:
    # take path of image as a input
    path = input(r'Enter path of Image : ')
     
    # taking encryption key as input
    key = int(input('Enter Key for encryption of Image : '))
     
    # print path of image file and encryption key that
    # we are using
    print('The path of file : ', path)
    print('Key for encryption : ', key)
     
    # open file for reading purpose
    fin = open(path, 'rb')
     
    # storing image data in variable "image"
    image = fin.read()
    fin.close()
     
    # converting image into byte array to
    # perform encryption easily on numeric data
    image = bytearray(image)
 
    # performing XOR operation on each value of bytearray
    for index, values in enumerate(image):
        image[index] = values ^ key
 
    # opening file for writing purpose
    fin = open(path, 'wb')
     
    # writing encrypted data in image
    fin.write(image)
    fin.close()
    print('Encryption Done...')
 
     
except Exception:
    print('Error caught : ', Exception.__name__)

输出:

Enter path of Image : C:\Users\lenovo\Pictures\Instagram\enc.png
Enter Key for encryption of Image : 22
The path of file :  C:\Users\lenovo\Pictures\Instagram\enc.png
Key for encryption :  22
Encryption done...

在上面的代码中,首先我们将图像的路径和加密密钥作为用户的输入,然后我们使用文件处理概念来处理二进制文件并打开该文件进行读取,然后读取图像的二进制数据并将其存储在图像中多变的。现在我们将该二进制形式的数据转换为字节数组,然后我们对字节数组的每个值应用 XOR 操作,这会更改数据,因此我们将无法打开图像。

解密

它只不过是将我们的加密数据转换为可读形式的过程。在这里,我们将再次对加密图像应用相同的 XOR 操作来解密它。但永远记住,我们的加密密钥和解密密钥必须相同。

解密的可执行代码:

蟒蛇3

# try block to handle the exception
try:
    # take path of image as a input
    path = input(r'Enter path of Image : ')
     
    # taking decryption key as input
    key = int(input('Enter Key for encryption of Image : '))
     
    # print path of image file and decryption key that we are using
    print('The path of file : ', path)
    print('Note : Encryption key and Decryption key must be same.')
    print('Key for Decryption : ', key)
     
    # open file for reading purpose
    fin = open(path, 'rb')
     
    # storing image data in variable "image"
    image = fin.read()
    fin.close()
     
    # converting image into byte array to perform decryption easily on numeric data
    image = bytearray(image)
 
    # performing XOR operation on each value of bytearray
    for index, values in enumerate(image):
        image[index] = values ^ key
 
    # opening file for writing purpose
    fin = open(path, 'wb')
     
    # writing decryption data in image
    fin.write(image)
    fin.close()
    print('Decryption Done...')
 
 
except Exception:
    print('Error caught : ', Exception.__name__)

输出:

Enter path of Image : C:\Users\lenovo\Pictures\Instagram\enc.png
Enter Key for Decryption of Image : 22
The path of file :  C:\Users\lenovo\Pictures\Instagram\enc.png
Note : Encryption key and Decryption key must be same.
Key for Decryption :  22
Decryption done...

在上面的解密程序中,我们使用与加密图像期间使用的程序相同的程序。

下面的视频使用给定的图像和密钥描述了上述程序的功能。