Video Encryption using Caesar Cipher - Matlab Tutorial

 In this matlab tutorial we are going to see video encryption using Caesar cipher. In cryptography, a Caesar cipher, also known as Caesar’s cipher, the shift cipherCaesar’s code or Caesar shift, is one of the simplest and most widely known encryption techniques. It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. For example, with a left shift of 3, D would be replaced by A, E would become B, and so on. The method is named after Julius Caesar, who used it in his private correspondence.


The transformation can be represented by aligning two alphabets; the cipher alphabet is the plain alphabet rotated left or right by some number of positions. For instance, here is a Caesar cipher using a left rotation of three places, equivalent to a right shift of 23 (the shift parameter is used as the key):


The encryption can also be represented using modular arithmetic by first transforming the letters into numbers, according to the scheme, A = 0, B = 1,…, Z = 25. Encryption of a letter  by a shift n can be described mathematically as:

 E(X)=(X+n) mod 26

Decryption is performed similarly,

D(X)=(X-n) mod 26

The Caesar cipher can be easily broken even in a ciphertext-only scenario. Two situations can be considered:

  • an attacker knows (or guesses) that some sort of simple substitution cipher has been used, but not specifically that it is a Caesar scheme;
  • an attacker knows that a Caesar cipher is in use, but does not know the shift value.

In the first case, the cipher can be broken using the same techniques as for a general simple substitution cipher, such as frequency analysis or pattern words. While solving, it is likely that an attacker will quickly notice the regularity in the solution and deduce that a Caesar cipher is the specific algorithm employed.

Video, Its Representation and Encryption:

            Any video is the collection of frames. These frames can be extracted from video or can be added to existing video at different speed (frame rates). In this project we have read the video directly from webcam frame by frame and the apply Caesar Cipher Encryption on the data from those frames. Every frame in video is consists of three matrices, 1st for representing Red color, 2nd for Green and 3rd for Blue


For image of size 4 X 4

Matrix representing Red Color

(1 ; ; )

            123      124      10        09

            100      200      200      232

            134      155      156      172

            10        60        45        123

(; 2 ; ; )

            123      124      10        09

            100      200      200      232

            134      155      156      172

            10        60        45        123

(; ; 3 ; )

            123      124      10        09

            100      200      200      232

            134      155      156      172

            10        60        45        123

For encrypting such frame using Matlab following is the code snippet ( see complete code)

…..….….….img = getsnapshot(vid);img_size = size(img);img1=zeros(img_size(1),img_size(2),3);% loading data from frame to variable img1for a =1:img_size(1)for b=1:img_size(2)for c=1:img_size(3)img1(a,b,c)= img(a,b,c);end;end;end;% encrypting data in img1for a =1:img_size(1)for b=1:img_size(2)for c=1:img_size(3)img1(a,b,c)= img1(a,b,c) + 200;if(img1(a,b,c)>255)img1(a,b,c)=img1(a,b,c)-255;end;%saving encrypted data back to imageimg(a,b,c)=img1(a,b,c);end;end;end;….….

Note that here img_size is the matrix which will contain dimension details of the frame/image, here for example for 4 X 4 image its values will be [ 4 4 4 1] and the img1 matrix will contain encrypted image data which will be loaded back to frame.

Video Decryption:

Decryption process is exactly opposite to encryption. Here video will be read from the file which is already encrypted, frames will be extracted and then decrypted, later stored to other video file, and following is the code snippet (see complete code )

for a =1:img_size(1)                       for b=1:img_size(2)                           for c=1:img_size(3)                                   img1(a,b,c)= img1(a,b,c)-200;                                   if(img1(a,b,c)<0)                                   img1(a,b,c)=255+img1(a,b,c);                                   end;                                   img(a,b,c)=img1(a,b,c);                           end;                       end;                   end;


Snapshots of Encrypted Video :

Video Encryption using Matlab


Popular posts from this blog

MATLAB code for Circular Convolution using Matrix method

Positive number pipe in angular 2+