Jump to content

Talk:Substitution cipher

From Rosetta Code
(Redirected from Talk:Substitution Cipher)

Old Things may be woken!

I daren't read the REXX cypher-text out loud. Old Things may be woken! --Tim-brown (talk) 07:07, 15 October 2015 (UTC)

Yes, it does sound like some strange incantation,   like a record being played backward.     -- Gerard Schildberger (talk) 15:06, 14 January 2020 (UTC)

Task needs to specify some examples

This task should really specify some examples to be tried by each implementation. --Rdm (talk) 14:32, 15 October 2015 (UTC)

Task: substitute cipher in PowerBASIC

PowerBASIC

<lang PowerBASIC> 'PBCC 6.03 version

' uses same alphabet and key as Ada language example $string1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" $string2 = "VsciBjedgrzyHalvXZKtUPumGfIwJxqOCFRApnDhQWobLkESYMTN"

%true = -1 %false = 0


FUNCTION PBMAIN() AS LONG

 PROCESS1 "plain.txt", "encrypted.txt", %true
 PROCESS1 "encrypted.txt", "decrypted.txt",%false
 PRINT
 PRINT "Press any key to quit"
 WAITKEY$

END FUNCTION


SUB process1(inputFile AS STRING, outputFile AS STRING, encrypt AS INTEGER)

 DIM i AS INTEGER
 OPEN inputFile FOR INPUT AS #1
 IF ERR > 0 THEN
   PRINT "Unable to open input file"
   WAITKEY$
   END
 END IF
 DIM alpha AS STRING, key1 AS STRING
 IF encrypt THEN
   alpha = $string1 : key1 = $string2
 ELSE
   alpha = $string2 : key1 = $string1
 END IF
 OPEN outputFile FOR OUTPUT AS #2
 DIM s AS STRING
 DIM p AS INTEGER
 DIM currentChar AS INTEGER
 WHILE NOT EOF(1)
   LINE INPUT #1, s
   FOR i = 1 TO LEN(s)
     currentChar = ASC(MID$(s, i, 1))
     IF (currentChar >= 65 AND currentChar <= 90) OR (currentChar >= 97 AND currentChar <= 122) THEN
       p = INSTR(alpha, MID$(s, i, 1))
       MID$(s, i, 1) = MID$(key1, p, 1)
     END IF
   NEXT
   PRINT #2, s
 WEND
 CLOSE #1 : CLOSE #2

END SUB </lang>

Cookies help us deliver our services. By using our services, you agree to our use of cookies.