RosettaCodeData/Task/ADFGVX-cipher/11l/adfgvx-cipher.11l
2023-07-01 13:44:08 -04:00

67 lines
1.6 KiB
Text
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

V adfgvx = ADFGVX
F encrypt(plainText, polybius, key)
V s =
L(ch) plainText
L(r) 6
L(c) 6
I polybius[r][c] == ch
s = :adfgvx[r]:adfgvx[c]
DefaultDict[Char, String] cols
L(ch) s
cols[key[L.index % key.len]] = ch
V result =
L(k) sorted(cols.keys())
I !result.empty
result =
result = cols[k]
R result
F decrypt(cipherText, polybius, key)
V skey = sorted(key)
V cols = [] * key.len
V idx = 0
L(col) cipherText.split( )
cols[key.findi(skey[idx])] = col
idx++
V s =
L(i) 0 .< key.len
L(col) cols
I i < col.len
s = col[i]
V result =
L(i) (0 .< s.len - 1).step(2)
V r = :adfgvx.findi(s[i])
V c = :adfgvx.findi(s[i + 1])
result = polybius[r][c]
R result
V polybius = [[Char("\0")] * 6] * 6
V alphabet = ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
random:shuffle(&alphabet)
L(r) 6
L(c) 6
polybius[r][c] = alphabet[6 * r + c]
print("6 x 6 Polybius square:\n")
print( | A D F G V X)
print(---------------)
L(row) polybius
print(adfgvx[L.index] | row.join( ))
V words = File(unixdict.txt).read().split("\n").filter(w -> w.len == 9 & w.len == Set(Array(w)).len)
V key = random:choice(words).uppercase()
print("\nThe key is "key)
V PlainText = ATTACKAT1200AM
print("\nPlaintext : "PlainText)
V cipherText = encrypt(PlainText, polybius, key)
print("\nEncrypted : "cipherText)
V plainText = decrypt(cipherText, polybius, key)
print("\nDecrypted : "plainText)