Substitution Cipher Implementation - File Encryption/Decryption



Task

Encrypt a input/source file by replacing every upper/lower case alphabets of the source file with another predetermined upper/lower case alphabets or symbols and save it into another output/encrypted file and then again convert that output/encrypted file into original/decrypted file.

This type of Encryption/Decryption scheme is often called a Substitution Cipher.



Related tasks

 Caesar cipher
 Rot-13
 Vigenère Cipher/Cryptanalysis


See also

 Wikipedia: Substitution cipher


100 PROGRAM "SuChiper.bas"
110 STRING ST$(1 TO 2)*52,K$*1
120 LET ST$(1)="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
130 LET ST$(2)="VsciBjedgrzyHalvXZKtUPumGfIwJxqOCFRApnDhQWobLkESYMTN"
140 CLEAR SCREEN:PRINT "1 - encode, 2 - decode"
150 DO
160   LET K$=INKEY$
170 LOOP UNTIL K$="1" OR K$="2"
180 IF K$="1" THEN
190   INPUT PROMPT "File name: ":NAME$
200   IF OPENFILE(NAME$) THEN CALL CHIPER(1)
210 ELSE
220   IF OPENFILE("Encrypte.txt") THEN CALL CHIPER(2)
230 END IF
240 DEF OPENFILE(N$)
250   LET OPENFILE=0
260   WHEN EXCEPTION USE OPENERROR
270     OPEN #1:N$
280   END WHEN 
290   LET OPENFILE=-1
300 END DEF
310 DEF CHIPER(FUNC)
320   LET EOF=0
330   WHEN EXCEPTION USE OPENERROR
340     IF FUNC=1 THEN
350       OPEN #2:"Encrypte.txt" ACCESS OUTPUT
360       LET OUTP=2
370     ELSE 
380       OPEN #2:"Decrypte.txt" ACCESS OUTPUT
390       LET OUTP=1
400     END IF 
410   END WHEN 
420   WHEN EXCEPTION USE IOERROR
430     DO 
440       GET #1:K$
450       IF UCASE$(K$)>="A" AND UCASE$(K$)<="Z" THEN
460         PRINT #2:ST$(OUTP)(POS(ST$(FUNC),K$));
470       ELSE 
480         PRINT #2:K$;
490       END IF 
500     LOOP UNTIL EOF
510   END WHEN 
520   HANDLER IOERROR
530     IF EXTYPE<>9228 THEN PRINT EXSTRING$(EXTYPE)
540     CLOSE #2
550     CLOSE #1
560     LET EOF=1
570   END HANDLER 
580 END DEF 
590 HANDLER OPENERROR
600   PRINT EXSTRING$(EXTYPE)
610   END
620 END HANDLER