Talk:Substitution cipher: Difference between revisions

From Rosetta Code
Content added Content deleted
m (added a section header to the first topic to properly place the table-of-contents (TOC) --- (this happens more often than one would think).)
 
(3 intermediate revisions by 3 users not shown)
Line 1: Line 1:
== Old Things may be woken! ==
== Old Things may be woken! ==
I daren't read the REXX cypher-text out loud. Old Things may be woken! --[[User:Tim-brown|Tim-brown]] ([[User talk:Tim-brown|talk]]) 07:07, 15 October 2015 (UTC)
I daren't read the REXX cypher-text out loud. Old Things may be woken! --[[User:Tim-brown|Tim-brown]] ([[User talk:Tim-brown|talk]]) 07:07, 15 October 2015 (UTC)

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


== Task needs to specify some examples ==
== Task needs to specify some examples ==


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 ==
=={{header|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>

Latest revision as of 09:35, 9 March 2021

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>