News:

Welcome to RetroCoders Community

Main Menu

Qbasic Substitute Cipher

Started by ron77, Aug 09, 2023, 12:54 PM

Previous topic - Next topic

ron77

this code is a simple substitute cipher to encrypt or decrypt files (text files or what ever) in Qbasic

DECLARE SUB process (inputFile AS STRING, outputFile AS STRING, encrypt AS ANY)

' uses same alphabet and key as Ada language example
CONST string1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
CONST string2 = "VsciBjedgrzyHalvXZKtUPumGfIwJxqOCFRApnDhQWobLkESYMTN"
 
process "test.txt", "test-ecp.txt", 1
'process "encrypted.txt", "decrypted.txt", 0
PRINT
PRINT "Database Encrypted Press any key to quit"
SLEEP

SUB process (inputFile AS STRING, outputFile AS STRING, encrypt AS LONG)
   OPEN inputFile FOR INPUT AS #1
   IF ERR > 0 THEN
     PRINT "Unable to open input file"
     SLEEP
     END
   END IF
   DIM alpha AS STRING, key2   AS STRING
   IF encrypt = 1 THEN
     alpha = string1: key2 = string2
   ELSE
     alpha = string2: key2 = string1
   END IF
   OPEN outputFile FOR OUTPUT AS #2
   DIM s AS STRING
   DIM p AS INTEGER
   WHILE NOT EOF(1)
     LINE INPUT #1, s
     FOR i = 1 TO LEN(s)
       sChar$ = MID$(s, i, 1)
       IF (sChar$ >= "A" AND sChar$ <= "Z") OR (sChar$ >= "a" AND sChar$ <= "z") THEN
         p = INSTR(alpha, MID$(s, i, 1)) 
		   MID$(s, i, 1) = MID$(key2, p, 1)
       END IF
     NEXT
     PRINT #2, s
   WEND
   CLOSE #1: CLOSE #2
END SUB