ADFGVX cipher: Difference between revisions

Added 11l
(Realize in F#)
(Added 11l)
Line 24:
As it's unclear from the Wikipedia article how to handle a final row with fewer elements than the number of characters in the key, either of the methods mentioned in [https://en.wikipedia.org/wiki/Transposition_cipher#Columnar_transposition Columnar transposition] may be used. In the case of the second method, it is also acceptable to fill any gaps after shuffling by moving elements to the left which makes decipherment harder.
<br><br>
 
=={{header|11l}}==
{{trans|Nim}}
 
<lang 11l>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]
 
V cols = DefaultDict[Char, String]()
L(ch) s
V i = L.index
cols[key[i % key.len]] ‘’= String(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
V i = L.index
print(adfgvx[i]‘ | ’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)</lang>
 
{{out}}
<pre>
6 x 6 Polybius square:
 
| A D F G V X
---------------
A | X O F P D 6
D | V H C 4 0 Z
F | J M K R U 5
G | I A 9 Y B W
V | 3 L 2 1 N G
X | Q T E 7 8 S
 
The key is EXCURSION
 
Plaintext : ATTACKAT1200AM
 
Encrypted : XFD GFVD GDG DGF DVD XDD DXV DGV DFF
 
Decrypted : ATTACKAT1200AM
</pre>
 
=={{header|F_Sharp|F#}}==
Line 58 ⟶ 153:
ATTACKAT1200AM encrypted is AFGFVDGVAAGVXFFAAGVADVDVVGXG which decrypted is ATTACKAT1200AM
</pre>
 
=={{header|Go}}==
{{trans|Wren}}
1,481

edits