· 7 years ago · Mar 22, 2018, 12:24 PM
1function encoder
2 const_lowerBound = 'A';
3 const_upperBound = 'Z';
4 secretKey = input('Input secret key: ', 's');
5 secretKey = upper(secretKey);
6
7 initialText = input('Input the text you desire to encrypt: ', 's');
8 initialText = upper(initialText);
9
10 fprintf(1, 'Secret: %s\n', secretKey);
11 fprintf(1, 'Initial text: %s\n', initialText);
12
13 keyCodes = filter(initialText, @(c) ~isspace(c));
14
15 encrypted = '';
16
17 for i = 1:length(keyCodes)
18 charToEncode = keyCodes(i);
19
20 if charToEncode > const_upperBound || charToEncode < const_lowerBound
21 continue
22 end
23
24 index = mod(i - 1, length(secretKey));
25 disp(index);
26 encodedChar = charToEncode + secretKey(index + 1);
27
28 encodedChar = mod(encodedChar - const_lowerBound, const_upperBound - const_lowerBound + 1);
29 encodedChar = encodedChar + const_lowerBound;
30
31 encrypted = [encrypted encodedChar];
32 end
33
34 fprintf(1, 'Encrypted: %s\n', encrypted);
35end
36
37function filtered = filter(array, filterFunction)
38 result = [];
39
40 for i = 1:length(array)
41 if filterFunction(array(i))
42 result = [result array(i)];
43 end
44 end
45
46 filtered = result;
47end