Use for-loop on characters in a textfile

11 views (last 30 days)
I have a plain text file that is going to be encrypted - where I'll use double as part of the process. I read the text file
text = fileread('textfile.txt')
Then I need to get the letters into numbers
lettertoNumber = double(text)
Now comes the part I'm struggling with. I need to have a for-loop that goes through lettertoNumber, which also needs to take lower/captial letters into consideration.
for ii = 1:length(lettertoNumber)
?? = ???????
?? = ???????
end
Greatly appreciate any input.
  3 Comments
Josh Samnja
Josh Samnja on 18 Sep 2018
There is no restriction on what can be used. The script or the program is going to be a simplified enigma machine. Where the input (encrypt/decrypt) is given from a text file. So the implementation I've thought of is to change letters into numbers.
lettertoNumber = double(text)
But this is only for a single letter right? Or am I wrong and literally the entire text is already converted to numbers? There is also the case of handling case sensitivity when it goes from letters/numbers vice versa. The enigma machine contains 3 rotors and 1 reflector that is already given. After the letters are changed into numbers I will use something like
cipher = rotor1(rotor2(rotor3(reflector(rotor3(rotor2(rotor1(letter)))))));
Where cipher is again a value between 1 and 26, and that needs to be converted to a letter in the same way again, and of course make sure that capitals are turned into capital and lower case into lower case..
Walter Roberson
Walter Roberson on 18 Sep 2018
double(text) would apply double() to the entire vector of characters individually.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 18 Sep 2018
Use lookup tables. Like
MAP_INVALIDS_TO = -1;
mapping = zeros(1,255) + MAP_INVALIDS_TO;
mapping('a':'z') = 1:26;
mapping('A':'Z') = 1:26;
mapped = mapping(text); %valid to use characters as indices
mapped(mapped == MAP_INVALIDS_TO) = []; %delete characters that had no mapping
Modified versions of this can be used for situations such as encoding the other characters as pairs of values, or for using "shift-in" / "shift-out" to switch between encodings.

More Answers (0)

Categories

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

Products

Community Treasure Hunt

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

Start Hunting!