hunter
hunter

Reputation: 111

image encryption using XOR

Okay I am working on a video processing project and this include encryption of each frame and writing it to a file (outputenc.avi). I use a key.jpg to encrypt each file using XOR operation and its going well but the problem is during decryption I am getting a noisy original image the key and the frame under process are GRAY SCALE images with dimension 384*288.

encyption

capWebcam.read(matOriginal);
if(matOriginal.empty()==true)
    return;
cv::Mat temp;
cv::resize(matOriginal,matOriginal,dsize,0,0,cv::INTER_CUBIC);
cv::cvtColor(matOriginal,matProcessed,CV_BGR2GRAY);

cv::bitwise_xor(matProcessed,key,temp);
output_enc_cap.write(temp);

decryption

capfile.read(temp);
if(temp.empty()==true)
      return;

cvtColor(temp,temp,CV_BGR2GRAY);
cv::bitwise_xor(temp,key,temp);

Upvotes: 0

Views: 2108

Answers (1)

Sam
Sam

Reputation: 20056

There are more issues with your code:

First, you convert your frame to grayscale:

cv::cvtColor(matOriginal,matProcessed,CV_BGR2GRAY);

then send it to your file. From this point on, there is NO way to get your color image back.

Then, you are saving the image with a (most probably lossy) codec. A lossy codec looses some information in the process. And it only guarantees that an compressed image will look similar to the original one. No guarantee that it will be identical. And because the "encrypted" image is noise, the result will be noise. But probably a completely different noise.

Then, this line tries to do in-place an algorithm that cannot work in place. But more than that, you wrote a grayscale image in the file, then you try to convert it to grayscale as if it was color. Complete nonsense.

cvtColor(temp,temp,CV_BGR2GRAY);

Then, you try the "decription algorithm" on an image that is anything but the "encrypted" one.

Sorry, but each line in your code is nonsense.

So, my advice is to start lower: Learn about codecs, learn about encryption and security, read what others have done on this topic, and then start.

Btw, creating your own encryption algorithm is not the best idea (at least when you are not a specialist in criptography): https://security.stackexchange.com/questions/25585/is-my-developers-home-brew-password-security-right-or-wrong-and-why

Upvotes: 1

Related Questions