How to xor key with the element by element in an array?

11 次查看(过去 30 天)
hi, Currently i'am doing image encryption for my project. Basically what i want to do is.. XOR key with the first element(done). The answer would XOR with the 2nd element. Next answer will be XOR with the 3rd element and so on. (It is similar to cumsum but i want to do it using XOR)
This is my code..
key = bitxor(bin2dec(strAscii), bitt(1,1))
for r = 1:rw
for c = 1:cl
xored = bitxor(key,bitt(r:-1:c))
end
end
and the output is xored =
1×0 empty uint8 row vector
I hope somebody could share with me how to do this. because i have been stuck for few days to figure out this.
  1 个评论
Stephen23
Stephen23 2018-4-24

@Farah Izzati: this is not twitter, so I removed the ugly # symbol from your tags, and split them correctly into separate tags. Next time you can do this yourself: simply enter each tag as words, and place a comma to separate the tags. Simple.

请先登录,再进行评论。

回答(1 个)

Shounak Shastri
Shounak Shastri 2018-4-24
Here, try this
I = randi([1,10],5); %Input matrix
[m, n] = size(I) %Size of the input matrix for looping
key = randi([1,10]); %Key
I = uint8(I); %Making sure the key and the input matrix have same number of bits
key = uint8(key);
for ii = 1 : m
for jj= 1 : n
c(ii, jj) = bitxor(key, I(ii,jj));
key = c(ii, jj); %Storing the encrypted value as the new key
end
end
This is just a suggestion. This code could probably be optimized.
Best of Luck.

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by