Anonymous user
Substitution cipher: Difference between revisions
→{{header|Nim}}
Alextretyak (talk | contribs) (Added 11l) |
|||
Line 1,286:
<pre>Rzs ho wft whxt bzv ykk nzzg xtr (yrg szxtr) wz jzxt wzntwftv.
Now is the time for all good men (and women) to come together.</pre>
=={{header|Nim}}==
<lang Nim>import sequtils, strutils
proc encrypt(key: seq[char]; msg: string): string =
result.setLen(msg.len)
for i, c in msg:
result[i] = key[ord(c) - 32]
proc decrypt(key: seq[char]; msg: string): string =
result.setLen(msg.len)
for i, c in msg:
result[i] = chr(key.find(c) + 32)
when isMainModule:
import random
randomize()
# Build a random key.
var key = toSeq(32..126).mapIt(chr(it)) # All printable characters.
key.shuffle()
const Message = "The quick brown fox jumps over the lazy dog, who barks VERY loudly!"
let encrypted = key.encrypt(Message)
let decrypted = key.decrypt(encrypted)
echo "Key = “$#”" % key.join()
echo "Message = “$#”" % Message
echo "Encrypted = “$#”" % encrypted
echo "Decrypted = “$#”" % decrypted</lang>
{{out}}
<pre>Key = “5`:S<UqJ& aCwQ?lA_bp"dEv*,'@c$;=FG}o8x\.s-MPrTu~L>[i1N(9Z^h/e#4Hjn]WBXYy7Dg3O+26fKIm|)zkt{R%!0V”
Message = “The quick brown fox jumps over the lazy dog, who barks VERY loudly!”
Encrypted = “17X5K)DW35]I6k25Y6t5g)+fm56zXI5|7X5OnR{5B6yw5k765]nI3m5(x[^5O6)BO{`”
Decrypted = “The quick brown fox jumps over the lazy dog, who barks VERY loudly!”</pre>
=={{header|Perl}}==
|