Cant Use Reshape Function with Randi or Randperm
显示 更早的评论
Hi,
I am trying to generate a random number vector and use it as shown in below:
plaintextsamples = randi(65535,1,1000);
for h = 1:1000
%a = plaintextsamples(i);
plaintexttemp = dec2bin(plaintextsamples(h),16);
for k = 1:16
temp = plaintexttemp(k);
plaintext(k) = str2double(temp);
end
ciphertext = encrypt_func(plaintext);
In the 9th line, I call a function and this function contents another function with reshape operation:
function permboxout = permbox1func(permboxin_temp)
%%%%Block Cipher 1 Permutation
permutation1 = [1,5,9,13,2,6,10,14,3,7,11,15,4,8,12,16];
permboxin_temp = dec2bin(permboxin_temp);
permboxin = reshape(permboxin_temp',[1,16]);
for i = 1:16
permboxout(permutation1(i)) = permboxin(i);
end
Basically, I am using the plaintext value in the first code as the input of the permbox1func and plaintext is a random value of the plaintextsamples which is created with an randi or randperm function.
When I run it an error message is shown:
Error using reshape
To RESHAPE the number of elements must not change.
Error in permbox1func (line 5)
permboxin = reshape(permboxin_temp',[1,16]);
Error in encrypt_func (line 21)
permboxround1 = permbox1func(sboxround1); %permutation 2
Error in decrypt (line 9)
ciphertext = encrypt_func(plaintext);
When I disable the randi function and using the loop value itself:
for h = 1:1000
%a = plaintextsamples(i);
plaintexttemp = dec2bin(h,16);
for k = 1:16
temp = plaintexttemp(k);
plaintext(k) = str2double(temp);
end
ciphertext = encrypt_func(plaintext);
It works if h = 1:540 but the same error pop up if max limit of h is bigger than 550 or 560. I am really confused. Why the limit of a loop or randi function matters for reshape function? The shape should not change each time because I already declared that plaintext is a double vector with 16 elements.
Thanks for your help.
采纳的回答
更多回答(1 个)
James Tursa
2020-4-29
I'm guessing you need to tell this dec2bin call that it always needs to produce 16 digits also:
permboxin_temp = dec2bin(permboxin_temp,16);
2 个评论
James Tursa
2020-4-29
编辑:James Tursa
2020-4-29
Use the debugger. Type this in at the command line
dbstop if error
and then run your code. When the error occurs, the code will pause with all variables intact. Examine permboxin_temp to see what it is, and then backtrack in your code to figure out why it isn't what you expected.
类别
在 帮助中心 和 File Exchange 中查找有关 Encryption / Cryptography 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!