Talk:Substitution cipher: Difference between revisions

Content added Content deleted
m (Thundergnat moved page Talk:Substitution Cipher to Talk:Substitution cipher: Follow normal task title capitalization policy)
Line 7: Line 7:


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

== Task: substitute cipher in 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