Encoder and Decoder Problem
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Neshant Thiru
am 27 Apr. 2020
Beantwortet: Deepak Gupta
am 27 Apr. 2020
I'm currently doing a script that writes and encrypts whatever the user inputs into a text file. As well as read that text file and decode the encryption. I have somehow managed to get the encoding part of the script working.
This is the Script I have to use for encrpting the input data onto a text file
%%Encoder Script
function [cipher,key]= encoder(message)
message_dec=cast(message,'uint8');%covner message into dcimalh
key_decimal = randi([1 255],1);%generate cipher key automatically
key_binary=de2bi(key_decimal,8,'left-msb');
cipher =' ';
for m= 1: length(message)
message_binary=de2bi(message_dec(m),8,'left-msb');
cipher_binary=xor(message_binary,key_binary);
cipher_dec=bi2de(cipher_binary,'left-msb');
cipher(m) =cast(cipher_dec,'char');%return cipher from encoder function
end
key=key_binary;%return key from encoder
The second one is a Decoding Script that decodes and outputs the encrypted input
%%Decoder Script
function message= decoder(ciphertext,key)
n=length(ciphertext) ;
ciphertext=uint8(ciphertext);
bkey=de2bi(key,8,'left-msb');
textmessage =' ';
for m = 1: n
message_binary=xor(de2bi(ciphertext(m),8,'left-msb'),bkey);
message_dec=bi2de(message_binary,'left-msb');
textmessage(m)= message_dec; %%<--This line is the problem
end
message=textmessage;%return key from encoder
The third one is a call back, read and write script.
clear,clc
message=input('enter a message:', 's');
%call the encoder funtion h
[encoded_text,key]= encoder(message);
fprintf('the encrypted message is: %s\n',encoded_text);
decoded_text=decoder(message,key); %%<--This line is also a problem
% Write encoded message to a text file
fid = fopen('mycode.txt', 'w'); % Open the file for write access 'w'
fwrite(fid, message); % Write message to open file
fclose(fid); % important, must release file back to Operating System after we're done
clear message
% Read encoded message from a text file
fid = fopen('mycode.txt', 'r'); % Open the file for read access 'r'
message = fread(fid); % Read the data out of the file
fclose(fid); % important, must release file back to Operating System after we're done
fprintf('the decrypted message is: %s\n',decoded_text);
0 Kommentare
Akzeptierte Antwort
Deepak Gupta
am 27 Apr. 2020
Hi Neshant,
your code is almost correct except a couple of mistakes.
In read-write script you need to provide encripted message as input to the decoder function.
i.e.
decoded_text=decoder(encoded_text,key);
And in the decoder function, your key is already in binary format so you don't need to convert it again to binary, which is causing a mismatch in the vector sizes.
%%Decoder Script
function message= decoder(ciphertext,key)
n=length(ciphertext) ;
ciphertext=uint8(ciphertext);
%bkey=de2bi(key,8,'left-msb');
textmessage =' ';
for m = 1: n
message_binary=xor(de2bi(ciphertext(m),8,'left-msb'),key);
message_dec=bi2de(message_binary,'left-msb');
textmessage(m)= char(message_dec); %%<--This line is the problem
end
message=textmessage;%return key from encoder
Cheers.
Deepak
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Encryption / Cryptography finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!