Secret Message
This program uses the Caesar cipher to encode or decode a message. To encode a message you shift the letters in the alphabet by a certain number of positions, or key, and substitute the original letter with the new one. To decode, you subtract the original key from 26 to get a new key.
For example, in a key of 3, you would shift the original alphabet by 3 positions. So,
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
becomes
D E F G H I J K L M N O P Q R S T U V W X Y Z A B C
You then substitute the letters of your message with the new letters in the same position. So, A is substituted by D, B by E, and so on. "Hello World" becomes "Khoor Zruog."
To decode, you subtract the original key from 26 to get your new key. In this case 26 - 3 = 23.
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
becomes
X Y Z A B C D E F G H I J K L M N O P Q R S T U V W
With this new key, "Khoor Zruog" becomes "Hello World" again.
Lines 20 - 60 asks the user to input if they are encoding or decoding a message, in what key, and the message. If the user is decoding, line 60 subtracts the key from 26 to get the new key.
Lines 70 - 140 uses a for loop to go through the message a letter at a time. It stores the ASCII value of the letter in A. The ASCII value is then checked to see if it is upper or lower case.
Lines 100 and 120 uses modular arithmetic to shift the value according to the key. First, it subtracts the ASCII value from 65 if it is uppercase or 97 if it is lowercase to get the position in the alphabet. For example, 65(A) would be position 0 in the alphabet. The key is then added to the original position.
(N - (26 * INT(N / 26))) is used to calculate N MOD 26. This gives the new position in the alphabet. 65 or 97 is added to the new position to get the corresponding ASCII value and this is now stored in A.
For example, if you were encoding the letter "B" with a key of 3, you would get:
N = A - 65 + K
N = 66 - 65 + 3 = 4
A = N MOD 26 + 65
A = 4 MOD 26 + 65
A = 4 + 65
A = 69
69 is the ASCII value of "E"
or if you were encoding the letter "j" with a key of 20:
N = A - 97 + K
N = 106 - 97 + 20 = 29
A = N MOD 26 + 97
A = 29 MOD 26 + 97
A = 3 + 97
A = 100
100 is the ASCII value of "d"
Line 130 adds the new character to N$, as well as any punctuation.
Lines 150 to 180 print the new message 18 characters at a time. Without this, the message was being printed on the VTech too fast to be read if the message was longer than 18 characters.
Code: Select all
5 CLEAR 200
10 PRINT "SECRET MESSAGE"
20 INPUT "1-ENCODE 2-DECODE"; C
30 INPUT "KEY(1-25)"; K
40 PRINT "ENTER MESSAGE"
50 INPUT M$
60 IF C = 2 THEN K = 26 - K
70 FOR I = 1 TO LEN(M$)
80 A = ASC(MID$(M$, I, 1))
90 IF A < 65 OR A > 90 THEN 110
100 N = A - 65 + K: A = (N - (26 * INT(N / 26))) + 65
110 IF A < 97 OR A > 122 THEN 130
120 N = A - 97 + K: A = (N - (26 * INT(N / 26))) + 97
130 N$ = N$ + CHR$(A)
140 NEXT I
150 IF LEN(N$) < 19 THEN PRINT N$: END
160 FOR I = 1 TO LEN(N$) STEP 18
170 PRINT MID$(N$, I, 18)
180 NEXT I