Issues in encrypting a string in binary form and embed in image matlab


I was working on a project that encrypts a string and embeds it in the image, how I do is by making it binary.
My program fully worked when I generated random data using the rand() function, but when I try real string, it is not working, how I know it isn’t working is by comparing data before and after encrypting, embedding, and decrypting, extract.
The code I used to generate random data is as follows

%% generate binary secret data
num = 1000;
rand('seed',0); % set the seed
D = round(rand(1,num)*1); % Generate stable random numbers

The code I used for encryption is

num_D = length(D); % Find the length of the  data D
Encrypt_D = D; % build a container that stores encrypted secrets
%% Generate a random 0/1 sequence of the same length as D based on the key
rand('seed',Data_key); % set the seed
E = round(rand(1,num_D)*1); % Randomly generate a 0/1 sequence of length num_D
%% XOR encryption of the original secret information D according to E
for i=1:num_D
    Encrypt_D(i) = bitxor(D(i),E(i));

The code I used to replace randomly generated data(The first code above)

D = uint8('Hello, this is the data I want to encrypt and embed rather than randomly generated data');
D = de2bi(D,8);
D = reshape(D,[1, size(D,1)*8]);

But this is not working, I wonder why, can anyone help me with this?


To encrypt and decrypt a string using bitxor you can do something similar to:

% Dummy secret key:
secret_key = 1234;

% String to encrypt:
D = double('Very secret string');
D = de2bi(D,8).';
D = D(:).';

% Encryption binary array
rand('seed',secret_key); % set the seed
E = round(rand(1,numel(D))*1);

% crypted string
crypted = bitxor(D,E);

% Decrypted string
decrypted = char(sum(reshape(bitxor(crypted,E),8,[]).*(2.^(0:7)).'))
% decrypted = 'Very secret string'

The last line do the following things:

  1. decrypt the crypted binary code with bitxor
  2. binary to decimal
  3. ascii to char array

Answered By – obchardon

Answer Checked By – David Marino (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.