Main Program % Secret service encryption system's driver program clear fprintf('Secret Service System\n\n') format compact choice = input('Enter 1 to encrypt, 2 to decrypt: '); while (choice ~= 1 & choice ~= 2) choice = input('Invalid input. Enter 1 to encrypt, 2 to decrypt: '); end % Get input file % In the input statement, using the extra parameter 's' means % the user does NOT enclose the input string in single quotes disp(' ') inFile = input('Enter input filename: ', 's'); message = readtext(inFile) % Get key sentence and construct the key disp(' ') sentence = input('Input key sentence: ', 's'); key = makeKey(sentence) % Encrypt or decrypt message disp(' ') if (choice == 1) newMessage = encode(key, message) else newMessage = decode(key, message) end % One should get rid of the evidence using the statement below, but % de-bugging is easier if the variables survive... % clear sentence key message newMessage inFile outFile Readtext function (you don’t need to understand the detail here) function result = readtext(filename) % Function readtext reads an ascii text file % Input: name of a text file with equal number of characters in each column % Output: a MATLAB array containing all characters in the input file fid = fopen(filename); if (fid < 0) message = sprintf('File %s cannot be opened.\nMake sure that file %s exists in your working directory.',... filename, filename) error(message) end i = 1; line = fgetl(fid); text = line; while (~feof(fid)) line = fgetl(fid); text = [text; line]; i=i+1; end result = text; MakeKey and helpers function res = makeKey(sent) %This function creates the key for the encryption algorithm. key = [' '] j=0; for k = 1:length(sent) %make sure we have a char and it is not already in the array if isletter(sent(k)) & ~inArray(key, sent(k)) key(floor(j/6) + 1, mod(j,6)+1) = upper(sent(k)); j = j +1; end end for k = 0:25 %make sure we have a char and it is not already in the array if ~inArray(key, char('A' + k)) key(floor(j/6) + 1, mod(j,6)+1) = 'A' + k; j = j +1; end end % add spaces to the array key(5,3:6) = ' ' res = key function res = inArray( key, symbol) %this function returns 1 if the char symbol is present in the array key res = sum(sum(key == upper(symbol))); Encode and helpers function res = encode(key, text) %Given a key and text in a nxm array, this function returns the encrypted text %in a nx2m array encr = [' '] [h,w] = size(text) for k = 1:h for j = 1:w if (isletter(text(k,j)) | text(k,j) == ' ') twoLetters = findEncr(key, text(k,j)); encr(k, 2*j - 1:2*j) = twoLetters; else encr(k, 2*j - 1:2*j) = text(k,j); end end end res = encr function res = findEncr(key, symbol) %given a key and a char, this function returns R and C, the characters first in the %corresponding %row and column, respectively of the key symbol [x, y] = size(key); [r, c] = find(key == upper(symbol)); res = [key(r(1),1), key(1,c(1))]; Decode and helpers function res = decode(key, text) %Given a key and text in a nxm array, this function returns the decrypted text % in a nxm/2 array decr = [' '] [h,w] = size(text) for k = 1:h for j = 1:(w/2) decr(k,j) = findDecr(key, text(k,2*j - 1:2*j)); end end res = decr function res = findDecr(key, symbols) %given a key and a char, this function returns R and C, the characters first %in the corresponding row and column, respectively of the key [x, y] = size(key); found = 0; k = 1; j = 1; %This part could be done with find as well %Done using max for illustrative purposes [max1, k] = max(key(1:x,1) == symbols(1)); [max2, j] = max(key(1,1:y) == symbols(2)); if (max1 == 0 | max2 == 0) res = symbols(1); else res = key(k,j); end 1. >> encrSystem Secret Service System Enter 1 to encrypt, 2 to decrypt: 1 Enter input filename: textline.txt message = Write & test each function independently! Input key sentence: Show variable values to help debug key = SHOWVA RIBLEU TPDGCF JKMNQX YZ newMessage = SWRSRHTSRVYO&&YOTSRVSSTSYORVSATVSHYOTARAJWTVTSRHSOJWYORHJWTORVTHRVJWTORVJWTSRWYS!! 2. >> encrSystem Secret Service System Enter 1 to encrypt, 2 to decrypt: 2 Enter input filename: codeline.txt message = SWRSRHTSRVYO&&YOTSRVSSTSYORVSATVSHYOTARAJWTVTSRHSOJWYORHJWTORVTHRVJWTORVJWTSRWYS!! Input key sentence: Show variable values to help debug key = SHOWVA RIBLEU TPDGCF JKMNQX YZ newMessage = WRITE & TEST EACH FUNCTION INDEPENDENTLY! 3. >> encrSystem Secret Service System Enter 1 to encrypt, 2 to decrypt: 2 Enter input filename: secret.txt message = JMVNVMSASSVMWMSSVMSNBMWMSMVMWMSASNWMSASSVYSOSNWMSMSAJNVYVOSNWMBMBSWMVOVOVO,,WMBMJNSAWSSNWMBAJNSOSMWMVYBYVMWMSSVM JNVSVOBOVM''SSWMBOSAJNWMJMSOSOVN WM[[BNWM77]]..WM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MSAVNVNSOWSSSWMSMVMWMVYSOWMBASOVNVNSOWSWMBYVOSMWMWSVOVY BYSOJYVYWMBMVOBABAVOBOJYVNVYSY.. Input key sentence: so many movies zo little time key = SOMANY VIEZLT BCDFGH JKPQRU WX newMessage = PLEASE SEND ME AN ASTON MARTIN DB III, DRAWN FROM THE SERVICE'S CAR POOL [G 7]. IT SHOULD BE BATTLESHIP GREY IN COLOR, AND STANDARD, BUT WITH CERTAIN EXTRAS INCLUDING: 1) SWITCHES TO ALTER THE TYPE AND COLOR OF FRONT AND REAR LIGHTS. 2) REINFORCED STEEL BUMPERS, FORE AND AFT, IN CASE I NEED TO RAM (FEW PEOPLE KNOW HOW TO RECOGNIZE REINFORCED STEEL.) 3) A RADIO PICK UP TUNED TO RECEIVE THE APPARATUS CALLED THE HOMER. THE LATTER IS THE MIDGET TRANSMITTER THAT YOU SENT ME WHICH, PRIVILY INSTALLED IN AN ENEMY'S CAR, ALLOWS ME TO FOLLOW HIM WITHOUT DIFFICULTY.