我正在编写一个脚本,使用python中的RSA算法对视频进行加密和解密。现在,我已从视频中提取帧并分别加密每个图像,然后将这些图像组合以创建视频。然后,我再次读取加密视频的帧,并且在应用解密密钥时,我没有找回原始图像。但是,当我在制作视频的任何图像上应用相同的密钥时,我将获得原始图像。假设我们有image1,它是加密的,当我在该图像上应用密钥时,将使用它来制作加密的视频。现在我有从加密的视频中读取的image2,如果应用了密钥,那么它会给出一个更加密的图像。这是代码:
import cv2
import numpy
import os
import imageio
import time
from tkinter.filedialog import askopenfilename
from tkinter.ttk import *
from tkinter import *
from tkinter import filedialog
from tqdm import tqdm
from tkinter import messagebox
import subprocess
def load_image_decrypt(folder):
videofile = 'envid.avi'
try:
if not os.path.exists('Dedata'):
os.makedirs('Dedata')
except OSError:
messagebox.showinfo('Error Occured', 'Error: Creating directory of decrypted data')
vid_to_image(videofile)
for filename1 in tqdm(os.listdir(folder)):
imgV = imageio.imread(os.path.join(folder, filename1), format='PNG-FI')
if imgV is not None:
RGBdecryption(imgV, filename1)
else:
break
vidname = 'devid.avi'
image_to_vid(dedata2, vidname)
messagebox.showinfo('Finish!', 'Decryption Done succesfully!')
def RGBdecryption(img, filename):
img1 = img
img = img.astype(numpy.uint16)
img1= img1.tolist()
for i1 in tqdm(range(len(img1))):
for j1 in (range(len(img1[i1]))):
for k1 in (range(len(img1[i1][j1]))):
x1 = img1[i1][j1][k1]
x1 = pow(x1,16971,25777)
img1[i1][j1][k1] = x1
img1 = numpy.array(img1).astype(numpy.uint16)
name = './Dedata/'+str(filename)
imageio.imwrite(name, img1, format='PNG-FI')
def vid_to_image(filename):
# Playing video from file:
cap = cv2.VideoCapture(filename)
try:
if not os.path.exists('data'):
os.makedirs('data')
messagebox.showinfo('Info!', 'Data directory is created where the frames are stored')
except OSError:
print ('Error: Creating directory of data')
currentFrame = 0
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
if not ret:
break
# Saves image of the current frame in jpg file
name = './data/frame' + str(currentFrame) + '.png'
print ('Creating...' + name)
imageio.imwrite(name, frame,format='PNG-FI')
# To stop duplicate images
currentFrame += 1
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
def image_to_vid(folder, vidname): #the code which is creating a video out of images stored in the folder
image_folder = folder
video_name = vidname
sort_image = []
images = [img for img in os.listdir(image_folder) if img.endswith(".png")]
print(images)
print('\n\n')
for i in range(0,1000):
for j in range(len(images)):
name = 'frame' + str(i) + '.png'
if ((str(images[j])) == str(name)):
sort_image.append(images[j])
print(sort_image)
frame = cv2.imread(os.path.join(image_folder, sort_image[0]))
height, width, layers = frame.shape
video = cv2.VideoWriter(video_name, 0, 29, (width,height)) #29 is the fs of the original video and I don't know what the 0 is for
for image in sort_image:
video.write(cv2.imread(os.path.join(image_folder, image)))
cv2.destroyAllWindows()
video.release()
data = './data'
load_image_decrypt(data)
我不知道我在哪里弄错了。我是opencv和视频处理的新手。任何帮助将不胜感激。谢谢。
答案 0 :(得分:2)
视频帧are subject to lossy compression。因此,您不能以图像为幌子向编解码器提供一些二进制数据,也不能对其进行编码并期望在播放结果视频时获得完全相同的二进制数据。
您最好的选择是按照Encryption of video files?或How can I Encrypt Video in Real Time?对视频文件进行整体加密。它需要解密才能播放;这显然是what OSX's "content protection" does, encrypting and decrypting data transparently。
IEEE(收费专栏)的文章Video Encryption Based on OpenCV - IEEE Conference Publication说,他们将Arnold Transform应用于图像数据。它是transposition cipher,因此是can be broken。它的主要优势似乎在于它使内容在常规播放中难以理解,并且保留了视频编解码器至关重要的图像特性(照明,帧差异),并且不需要用于解密的确切密文,因此它不会因无法修复而受到损坏。有损压缩。