我正在研究一個加密字串并將其嵌入到影像中的專案,我所做的就是將其設為二進制。當我使用rand()函式生成隨機資料時,我的程式完全作業,但是當我嘗試真正的字串時,它不起作用,我如何知道它不起作用是通過比較加密、嵌入和解密之前和之后的資料,提煉。我用來生成隨機資料的代碼如下
%% generate binary secret data
num = 1000;
rand('seed',0); % set the seed
D = round(rand(1,num)*1); % Generate stable random numbers
我用于加密的代碼是
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));
end
我用來替換隨機生成資料的代碼(上面的第一個代碼)
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]);
但這不起作用,我想知道為什么,有人可以幫助我嗎?
uj5u.com熱心網友回復:
要使用 bitxor 加密和解密字串,您可以執行以下操作:
% 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'
最后一行執行以下操作:
- 使用 bitxor 解密加密的二進制代碼
- 二進制轉十進制
- ascii 到 char 陣列
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/466505.html
