Ciphers/Encryption trouble with 8bit binary xor encryption

4 views (last 30 days)
Im having trouble developing a this encryption. Im required to develope a cypher. Thats turns a message into its ascII then into its binary value (8 bit) then combine it with a randomly generated number using xor. I am given some code that will convert the message and save it in a text file.
I thought my code would work but it doesnt and im unsure where im going wrong
The error message im getting is
Attempt to execute SCRIPT encoder as a function:
C:\Users\mick\Documents\MATLAB\prac two\encoder.m
Also have a graph popping up and i have no clue why
the code ive devloped so far:
________________________________________________________________________________________________________________________________________
text = input('Enter what you want to code ', 's')
function [c_text, key] = encode(text)
N = length(text);
text = uint8(text);
key = randi([20 200], 1);
b_key = de2bi(key, 8, 'left-msb');
c_text = ''; %initialize an array for the coded message
for k = 1:N
b_c_text = xor(de2bi(text(k), 8, 'left-msb'), b_key);
d_c_text = bi2de(b_c_text, 'left-msb');
c_text(k) = d_c_text;
end
end
_____________
___________________________________________________________________________________________________________________________
The given code
______________________________________________________________________________________________________________________________
clear, clc
% Call my encoder function
[codedmessage, key] = encode(plaintext);
%Output coded message
fprintf('%c', codedmessage)
fprintf('\n')
%write encoded message to a text file
fid = fopen('group_text.txt', 'w'); %Open the file for write access 'w'
fwrite(fid, codedmessage); %Write message to open file
fclose(fid); % important, must release file back to Operating System after we're done
clear codedmessage
%Read encoded message from a text file
fid = fopen('group_text.txt', 'r'); %Open the file for read access'r'
codedmessage = fread(fid); % Read the data out of the file
fclose(fid); %important, must release file back to Oprating System after we're done
__________________________________________________________________________________________________________________
any help would be appricated

Accepted Answer

Walter Roberson
Walter Roberson on 27 Apr 2020
Remove the line
text = input('Enter what you want to code ', 's')
from encode.m
In your other file, before the line
% Call my encoder function
insert the line
plaintext = input('Enter what you want to code ', 's');
  1 Comment
MIchael Brennan
MIchael Brennan on 27 Apr 2020
Thanks Walter I didnt realise i had a varible also incorrectly labeled . Much appreciated

Sign in to comment.

More Answers (0)

Categories

Find more on Encryption / Cryptography in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!