Vigenère cipher: Difference between revisions

Content added Content deleted
(Added Quackery.)
(Updated to work with Nim 1.4: added missing parameter types; replaced "toUpper" with "toUpperAscii". Also, some other modifications.)
Line 1,947: Line 1,947:
<lang nim>import strutils
<lang nim>import strutils


proc encrypt(msg, key: string): string =
proc isAlpha(c): bool = c in 'a'..'z' or c in 'A'..'Z'

proc encrypt(msg, key): string =
result = ""
var pos = 0
var pos = 0
for c in msg:
for c in msg:
if isAlpha c:
if c in Letters:
result.add chr(((ord(key[pos]) + ord(toUpper c)) mod 26) + ord('A'))
result.add chr(((ord(key[pos]) + ord(c.toUpperAscii)) mod 26) + ord('A'))
pos = (pos + 1) mod key.len
pos = (pos + 1) mod key.len


proc decrypt(msg, key): string =
proc decrypt(msg, key: string): string =
result = ""
var pos = 0
var pos = 0
for c in msg:
for c in msg:
Line 1,973: Line 1,969:
echo encr
echo encr
echo decr</lang>
echo decr</lang>

{{out}}
{{out}}
<pre>Beware the Jabberwock, my son! The jaws that bite, the claws that catch!
<pre>Beware the Jabberwock, my son! The jaws that bite, the claws that catch!