以二进制形式加密字符串并嵌入图像matlab中的问题

发布于 2025-01-23 22:02:48 字数 1058 浏览 1 评论 0原文

我正在研究一个将字符串加密并嵌入图像中的项目,通过使其制作二进制文件。 当我使用 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]);

,但这是不起作用的,我想知道为什么,为什么,谁能帮我吗?

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));
end

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

娇柔作态 2025-01-30 22:02:48

要使用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'

最后一行执行以下操作:

  1. 解密带有bitxor二进制的隐式二进制代码
  2. 到十进制
  3. ASCII到char Array

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文