Question
Fix this code where stated! (only two lines to fix to make work 'as-is') Then turn into a function that can be called such that
Fix this code where stated! (only two lines to fix to make work 'as-is')
Then turn into a function that can be called such that
input = string message and a key
This will be used later -- you can then further modify closer to the end
% clear the command window and workspace
clear
clc
% produce a random 8 bit key in a logical array
bitarr = rand(1,8) > 0.3;
disp(bitarr);
% produce a bitstream with the message encoded
% bitstream contains strings of binary code
bitstream = {};
% the string to encode
str = 'This is the message to encode';
% the length of the string -- used throughout
l = length(str);
% work through the length of the string
for c = 1:l
ch = str(c);
asc = unicode2native(ch);
newbits = dec2bin(asc,8);
bitstream{c} = newbits;
end
%disp(bitstream);
% Convert a number to a binary logical array
% example, for 129, with 8 bits use:
% m = logical(dec2bin(129, 8) - '0')
% XOR encode here
%
cipher = {};
for c = 1:l
s = bitstream{c};
% convert string to logical array required by bitxor
snx = logical(s-'0');
% One line to fix here!
% ci = ?????????
% convert logical array to string
cis = char(ci + 48);
cipher{c} = cis;
end
%ci_sentence = strjoin(cipher,'');
%disp(ci_sentence);
% XOR decode here
% do it again to decode!
decipher = {};
for c = 1:l
s = cipher{c};
% convert string to logical array required by bitxor
snx = logical(s-'0');
% One line to fix here!
% ci = ????????
% convert logical array to string
% also works as - char(A + int8('0')); (48 is the code for '0')
cis = char(ci + 48);
decipher{c} = cis;
end
% de_sentence = strjoin(decipher,'');
%disp(de_sentence);
sentence = {};
for c = 1:l
s = decipher{c};
n = bin2dec(s);
ch = char(n);
sentence{c} = ch;
end
jsentence = strjoin(sentence,'');
disp(jsentence);
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started