Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
29
Task/Substitution-cipher/Nim/substitution-cipher.nim
Normal file
29
Task/Substitution-cipher/Nim/substitution-cipher.nim
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
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
|
||||
Loading…
Add table
Add a link
Reference in a new issue