1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 # This program is public domain
4 # Task: Vigenère cipher
6 # See: <http://rosettacode.org/wiki/Vigenère_cipher>
9 fun encrypt
(src
, key
: String): String do
13 for i
in [0..src
.length
- 1] do
16 if c
>= 'a' and c
<= 'z' then
18 else if c
< 'A' or c
> 'Z' then
22 out
.add
(((c
.ascii
+ key
[j
].ascii
- 2u8
* 'A'.ascii
) % 26u8
+ 'A'.ascii
).ascii
)
23 j
= (j
+ 1) % key
.length
29 fun decrypt
(src
, key
: String): String do
33 for i
in [0..src
.length
- 1] do
36 if c
>= 'a' and c
<= 'z' then
38 else if c
< 'A' or c
> 'Z' then
42 out
.add
(((c
.ascii
- key
[j
].ascii
+ 26u8
) % 26u8
+ 'A'.ascii
).ascii
)
43 j
= (j
+ 1) % key
.length
46 return out
.write_to_string
50 var str
= "All your base are belong to us"
52 var code
= encrypt
(str
, key
)
53 var decode
= decrypt
(code
, key
)
58 print
"Back: " + decode