Bifid cipher: Difference between revisions

Line 1,523:
Encrypted : NGiw3okfXj4XoVE_NjWcLK4Sy28EivKo3aeNiti3N3z6HCHno6Fkf
Decrypted : The_invasion_will_start_on_the_first_of_January_2023.</pre>
=={{header|Ruby}}==
<syntaxhighlight lang="ruby" line>
def cleanMsg(msg, square)
msg.upcase!
msg.delete!(' ')
msg.delete!('J') if square.index(' ') == nil
end
 
def encrypt(msg, square)
cleanMsg msg, square
sq_size = (square.length ** 0.5).to_i
rows = [0] * msg.length
cols = [0] * msg.length
(0...msg.length).each do |i|
p = square.index(msg[i])
rows[i], cols[i] = p / sq_size, p % sq_size
end
result = ""
rows.concat(cols).each_slice(2) do |coord|
result += square[coord[0]*sq_size + coord[1]]
end
return result
end
 
def decrypt(msg, square)
msg.upcase!; square.upcase!
sq_size = (square.length ** 0.5).to_i
coords = []
result = ""
(0...msg.length).each do |i|
p = square.index(msg[i])
coords << p / sq_size
coords << p % sq_size
end
for i in (0...coords.length/2) do
row, col = coords[i], coords[i+coords.length/2]
result += square[row*sq_size + col]
end
return result
end
 
def printSquare(square)
sq_size = (square.length ** 0.5).to_i
(0..square.length).step(sq_size).each do |i|
print square[i...(i+sq_size)], "\n"
end
end
 
tests = [["ATTACKATDAWN" , "ABCDEFGHIKLMNOPQRSTUVWXYZ"],
["FLEEATONCE" , "BGWKZQPNDSIOAXEFCLUMTHYVR"],
["ATTACKATDAWN" , "ABCDEFGHIKLMNOPQRSTUVWXYZ"],
["the invasion will start on the first of january", "BGWKZQPNDSIOAXEFCLUMTHYVR"],
["THIS MESSAGE HAS NUMBERS 2023", "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"],
]
 
for test in tests
message = test[0]; square = test[1];
encrypted = encrypt(message, square)
decrypted = decrypt(encrypted, square)
 
puts "using the polybius:"
printSquare(square)
puts "the plain message:", message
puts "encrypted:", encrypted
puts "decrypted:", decrypted
puts "===================================================="
end</syntaxhighlight>
{{out}}
<pre>using the polybius:
ABCDE
FGHIK
LMNOP
QRSTU
VWXYZ
 
the plain message:
ATTACKATDAWN
encrypted:
DQBDAXDQPDQH
decrypted:
ATTACKATDAWN
====================================================
using the polybius:
BGWKZ
QPNDS
IOAXE
FCLUM
THYVR
 
the plain message:
FLEEATONCE
encrypted:
UAEOLWRINS
decrypted:
FLEEATONCE
====================================================
using the polybius:
ABCDE
FGHIK
LMNOP
QRSTU
VWXYZ
 
the plain message:
ATTACKATDAWN
encrypted:
DQBDAXDQPDQH
decrypted:
ATTACKATDAWN
====================================================
using the polybius:
BGWKZ
QPNDS
IOAXE
FCLUM
THYVR
 
the plain message:
THEINVASIONWILLSTARTONTHEFIRSTOFANUARY
encrypted:
RASOAQXCYRORXESXOLRGTXEGAWEWTNGTZTQALY
decrypted:
THEINVASIONWILLSTARTONTHEFIRSTOFANUARY
====================================================
using the polybius:
ABCDEF
GHIJKL
MNOPQR
STUVWX
YZ1234
567890
 
the plain message:
THISMESSAGEHASNUMBERS2023
encrypted:
TJMVBBDPMCW9ZIAYAEGBMK5XW
decrypted:
THISMESSAGEHASNUMBERS2023
====================================================
</pre>
 
=={{header|Wren}}==
One way of enabling all 26 letters to be encrypted uniquely would be to use a 6 x 6 Polybius square including the 10 digits. We could then encrypt text using numerals as well.
2

edits