RosettaCodeData/Task/Bifid-cipher/M2000-Interpreter/bifid-cipher.m2000
2026-04-30 12:34:36 -04:00

68 lines
1.9 KiB
Text

module Bifid_cipher (f, code$) {
n=sqrt(len(code$))
print #f," ";
for i=1 to n
print #f, i+" ";
next
print #f
for i=0 to n-1
Print #f, (i+1)+" "+STR$(mid$(code$,1+i*n, n),STRING$("@ ", 5))
next
tables=lambda (a$)->{
n=sqrt(len(a$))
a=list
for i=1 to len(a$)
append a, mid$(a$,i, 1):=((i-1) mod n+1, (i-1) div n+1 )
next
b=list
m=each(a)
while m
z=eval(m)
append b, z#val$(1)+z#val$(0):=eval$(m!)
end while
=a, b
}(code$)
encode= lambda (a, b)->{
=lambda a,b (mess as string) -> {
document code$
for n=1 to 0
for i=1 to len(mess)
q=mid$(mess, i,1)
if not exist(a, q) then
if q="J" then q="I" else q="A"
end if
code$=a(q)#val$(n)
next
next
document final$
for i=1 to len(code$) step 2
final$=b$(mid$(code$, i, 2))
next
= final$
}
}(!tables)
decode= lambda (a, b)->{
=lambda a,b (final as string) -> {
document code$, mess$
for i=1 to len(final)
q=a(mid$(final, i, 1))
code$=q#val$(1)+q#val$(0)
next
offset=len(code$) div 2
for i=1 to offset
mess$=b$(mid$(code$,i,1)+mid$(code$,i+offset,1))
next
= mess$
}
}(!tables)
Print #f, encode("ATTACKATDAWN")
Print #f, decode(encode("ATTACKATDAWN"))="ATTACKATDAWN"
Print #f, encode(ucase$(filter$("The invasion will start on the first of January", " ")))
Print #f, decode(encode(ucase$(filter$("The invasion will start on the first of January", " "))))
}
open "out.txt" for output as #a
Bifid_cipher a, "ABCDEFGHIKLMNOPQRSTUVWXYZ"
Bifid_cipher a, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
Bifid_cipher a, "BGWKZQPNDSIOAXEFCLUMTHYVR"
close #a
win "out.txt"