MATLAB study notes steganography - hide messages/images in images

1. Steganography

1 Overview

        Steganography is a technique of hiding secret information in another document or image and extracting it by a different person. We'll learn how to hide a text message or image within an image without the modification of the original image being noticed.

2. Brief description of the process

(1) There is such a matrix

(2) Use the "de2bi" method to convert to binary format

        The de2bi method defaults to right MSB order.

7 for decimal

  (3) The values ​​corresponding to 'A' and 'h' are 65 and 104, respectively

        65 binary representation

        Replace part of the LSB bits in the matrix.

 (4) Process h, 104 in the same way

 (5) The final matrix becomes

 Two, matlab example hidden text message

1. Encryption code

        The string to be encrypted is "this is a matlab demo, if this lenght small", and the encrypted image is saved as Encrypt1.png. Note that the length of the message matches the size of the image, neither too short nor too long.

    %ENCRYPTION

    clear all
    %READ THE INPUT IMAGE
    A = imread('图片/cameraman.tif');

    %
    Output = A;

    %MESSAGE TO BE HIDDEN INSIDE THE IMAGE
    Str = 'this is a matlab demo, if this lenght small';

    %NUMBER OF CHARACTERS IN THE MESSAGE
    numLetters = numel(Str);

    %REPRESENT THE LENGTH OF THE MESSAGE IN BINARY FORMAT
    Bi_Total = de2bi(numLetters,8);
    Bi_Msg = de2bi(double(Str),8)';

    Encrypt_msg = Bi_Msg(:)';

    %FIND THE LENGTH OF THE MESSAGE IN THE BINARY FORMAT
    num_En_msg = numel(Encrypt_msg);


    %DEFINE THE INTERVAL BETWEEN EACH VALUE
    dt=floor(numel(A)/num_En_msg);

    %BINARY VALUE OF THE INTERVAL
    Bi_dt = de2bi(dt,8);

    % FIRST 8 BITS - INTERVAL
    % SECOND 8 BITS - TOTAL LENGTH OF THE BINARY VALUES
    Info_data = [Bi_dt,Bi_Total];
    Output(1:16) = bitset(A(1:16),1,Info_data);

    %REPLACE THE LEAST SIGNIFICANT BIT IN THE IMAGE
    Start_pt =17;
    End_pt = (num_En_msg*dt)+16;
    Output(Start_pt:dt:End_pt) = bitset(A(Start_pt:dt:End_pt),1,Encrypt_msg);


    figure,subplot(121),imshow(A);title('Original Image');
    subplot(122),imshow(Output);title('Image with the hidden message');

    %SHOW THE DIFFERENCE BETWEEN THE ORIGINAL AND THE ENCRYPTED IMAGE
    figure,imagesc(double(A)-double(Output));colormap(jet);colorbar;

    %WRITE THE ENCRYPTED IMAGE IN THE PNG FORMAT
    imwrite(Output,'Encrypt1.png');

    display('Message is hidden inside the image!');

        The message is hidden in the image by converting it to binary format and placing it in the least significant bits of the image pixel. Encrypted images are saved as "PNG" image files for lossless compression. With lossless compression techniques, pixel values ​​do not change, so the information embedded in the LSBs is not lost.

        The maximum absolute value difference between the original image and the encrypted image is 1. Because we only deal with the LSB bits of the image.

Left: Original image; Right: Image with hidden message

        Instead of replacing pixels adjacent to each other, we can place the bits of the secret message in a specific pattern or at regular intervals. The figure below shows that these bits are placed at specific interval values.

2. Decryption code

%DECRYPT THE MESSAGE
clear all
    clc
    %READ THE ENCRYPTED IMAGE
    A = imread('Encrypt1.png');

    %FIRST 8 BITS REPRESENT THE INTERVAL
    Dt = bitget(A(1:8),1);
    Dt = double(bi2de(Dt));


    %LENGTH OF THE TEXT MESSAGE
    Len = bitget(A(9:16),1);
    Len = double(bi2de(Len));

    %FETCH THE LEAST SIGNIFICANT BIT FROM THE ENCRYPTD IMAGE
    Start_pt = 17;
    End_pt = (Len*Dt*8)+16;
    Decrypt_msg = bitget(A(Start_pt:Dt:End_pt),1);

    %CONVERT THE IMAGE INTO DECIMAL
    Decrypt_msg = reshape(Decrypt_msg,8,[])';
    Dec_Char   = bi2de(Decrypt_msg)';

    display('Text Message inside the image:');
    display(char(Dec_Char));

        Run the code, it will output, decrypt successfully.

Text Message inside the image:
this is a matlab demo, if this lenght small

Third, the matlab example hides the picture

1. Picture example

        Image used to hide the image

        The image to be hidden, note that the image to be hidden should be less than 10% of the larger image 

 1. Encryption

         Reference Code

%ENCRYPTION
    clear all

    %READ THE COVER IMAGE
    I = imread('123.png');

    %READ THE IMAGE TO HIDE
    J = imread('a12345.png');

    %PREALLOCATE THE OUTPUT IMAGE
    Output = zeros(size(I));
    %REPRESENT THE NUMBER OF ROWS AND COLUMNS OF THE SECRET IMAGE
    %IN BINARY FORMAT
    Jsize = de2bi([size(J,1),size(J,2)]);

    %RED,GREEN AND BLUE COMPONENTS OF THE SECRET IMAGE
    Red_Ch = J(:,:,1);
    Green_Ch = J(:,:,2);
    Blue_Ch = J(:,:,3);

    %CONVERT THE RED COMPONENT OF THE SECRET IMAGE INTO BINARY FORMAT
    Encrypt_Red = de2bi(Red_Ch(:),8)';
    Encrypt_Red = Encrypt_Red(:)';

    %CONVERT THE GREEN COMPONENT OF THE SECRET IMAGE INTO BINARY FORMAT
    Encrypt_Green = de2bi(Green_Ch(:),8)';
    Encrypt_Green = Encrypt_Green(:)';

    %CONVERT THE BLUE COMPONENT OF THE SECRET IMAGE INTO BINARY FORMAT
    Encrypt_Blue = de2bi(Blue_Ch(:),8)';
    Encrypt_Blue = Encrypt_Blue(:)';

    %CALCULATE THE INTERVAL AND CONVERT IT INTO BINARY FORMAT
    dt=floor((size(I,1)*size(I,2))/numel(Encrypt_Red));
    Bi_dt = de2bi(dt,8);

    Info_data = [Bi_dt,Jsize(1,:),Jsize(2,:)];

    %RED,GREEN AND BLUE COMPONENT OF THE COVER IMAGE
    Red_mat = I(:,:,1);
    Green_mat = I(:,:,2);
    Blue_mat = I(:,:,3);

    % INTERVAL,NUMBER OF COLUMNS, NUMBER OF ROWS
    Red_mat(1:24) = bitset(Red_mat(1:24),1,Info_data);
    Green_mat(1:24) = bitset(Green_mat(1:24),1,Info_data);
    Blue_mat(1:24) = bitset(Blue_mat(1:24),1,Info_data);

    %%REPLACE THE LEAST SIGNIFICANT BIT OF THE COVER IMAGE
    %WITH THE BINARY FORMAT OF THE SECRET IMAGE
    EndValue = numel(Encrypt_Red)*dt+24;
    Red_mat(25:dt:EndValue) = bitset(Red_mat(25:dt:EndValue),1,Encrypt_Red);
    Green_mat(25:dt:EndValue) = bitset(Green_mat(25:dt:EndValue),1,Encrypt_Green);
    Blue_mat(25:dt:EndValue) = bitset(Blue_mat(25:dt:EndValue),1,Encrypt_Blue);

    %ENCRYPTED IMAGE
    Output(:,:,1) = Red_mat;
    Output(:,:,2) = Green_mat;
    Output(:,:,3) = Blue_mat;
    Output = uint8(Output);

    figure,subplot(121),imshow(I); subplot(122),imshow(Output);

    %DIFFERENCE BETWEEN THE ORIGINAL AND THE ENCRYPTED IMAGE
    figure,imagesc(double(I)-double(Output));colormap(jet);colorbar;

    %WRITE THE ENCRYPTED IMAGE IN THE PNG FORMAT
    imwrite(Output,'Encrypt_Image2.png');

        Original image and encrypted image

         The distribution of hidden images in the big picture.

 2. Decryption

%DECRYPT IMAGE
    clear all
    %READ THE ENCRYPTED IMAGE
    A = imread('Encrypt_Image2.png');
    %RED,GREEN AND BLUE COMPONENTS
    Red_ch = A(:,:,1);
    Green_ch = A(:,:,2);
    Blue_ch = A(:,:,3);
    %INTERVAL
    Dt = bitget(Red_ch(1:8),1);
    Dt = double(bi2de(Dt));
    %NUMBER OF COLUMNS
    Ncols = bitget(Red_ch(9:16),1);
    Ncols = double(bi2de(Ncols));
    %NUMBER OF ROWS
    Nrows = bitget(Red_ch(17:24),1);
    Nrows = double(bi2de(Nrows));
    %FETCH THE LEAST SIGNIFICANT BIT FROM THE ENCRYPTED IMAGE
    EndPoint = (Dt*Nrows*Ncols*8)+24;
    Decrypt_Red = bitget(Red_ch(25:Dt:EndPoint),1);
    Decrypt_Green = bitget(Green_ch(25:Dt:EndPoint),1);
    Decrypt_Blue = bitget(Blue_ch(25:Dt:EndPoint),1);
    Decrypt_Red = reshape(Decrypt_Red,8,[])';
    Decrypt_Red = bi2de(Decrypt_Red);
    Decrypt_Red = reshape(Decrypt_Red,Ncols,Nrows);
    Decrypt_Green = reshape(Decrypt_Green,8,[])';
    Decrypt_Green = bi2de(Decrypt_Green);
    Decrypt_Green = reshape(Decrypt_Green,Ncols,Nrows);
    Decrypt_Blue = reshape(Decrypt_Blue,8,[])';
    Decrypt_Blue = bi2de(Decrypt_Blue);
    Decrypt_Blue = reshape(Decrypt_Blue,Ncols,Nrows);
    Decrypted_Image = zeros([Ncols,Nrows,3]);
    Decrypted_Image(:,:,1)=Decrypt_Red;
    Decrypted_Image(:,:,2)=Decrypt_Green;
    Decrypted_Image(:,:,3)=Decrypt_Blue;
    Decrypted_Image = uint8(Decrypted_Image);
    imwrite(Decrypted_Image,'Decrypted_Image2.png');
    figure,imshow(Decrypted_Image);title('Secret Image');

        Decrypted secret image. 

Guess you like

Origin blog.csdn.net/bashendixie5/article/details/123260488