Vigenère Cipher
As an addon, load with
load'convert/misc/vig'
The Vigenère cipher is a method of encryption that uses a series of different Caesar ciphers based on the letters of a keyword.
NB.*vig c Vigenère cipher
NB. cipher=. key 0 vig charset plain
NB. plain=. key 1 vig charset cipher
vig=: 2 : 0
:
r=. (#y) $ n i.x
n {~ (#n) | (r*_1^m) + n i.y
)
«charsets»
«encoded»
Note 'Examples'
«basic examples»
«symmetry examples»
«encoded examples»
)
Different character sets can be used for different types of text.
NB. printable ASCII vigprn=: vig (TAB,CRLF,(32,:95) ];.0 a.) NB. alphabetic vigalpha=: vig ( (97,:26) ];.0 a. ) NB. acsii vigascii=: vig a.
Compare with the Wikipedia example.
(97,:26) ];.0 a. NB. alpha abcdefghijklmnopqrstuvwxyz 'lemon' 0 vigalpha 'attackatdawn' lxfopvefrnhr 'lemon' 1 vigalpha 'lemon' 0 vigalpha 'attackatdawn' attackatdawn
Symmetry
Vigenère cipher is a symmetric-key algorithm using the same key to encode and decode.
In addition, the encoding and decoding are interchangeable.
'lemon' 1 vigalpha 'attackatdawn' pphmpzwhpnlj 'lemon' 0 vigalpha 'lemon' 1 vigalpha 'attackatdawn' attackatdawn
Encodings
To work with the full range of 256 ascii symbols, it is convenient to represent the result of cypher in one of printable encodings. The encoded result is wrapped into several lines.
NB. base64 encoded vig64=: 1 : 0 : require 'convert/misc/base64' mv=. m vigascii x (_64 ]\ tobase64@mv)`(mv frombase64@(, -. CRLF,' '"_))@.m y ) NB. ascii85 encoded vig85=: 1 : 0 : require 'convert/misc/ascii85' mv=. m vigascii x (toascii85@mv)`(mv fromascii85)@.m y )
For example,
'lemon' 0 vig85 a. Ch@U+EcGl:GB\4... a. -: 'lemon' 1 vig85 'lemon' 0 vig85 a. 1 'lemon' 0 vig64 a. bGZvcnJxa3R3d3... a. -: 'lemon' 1 vig64 'lemon' 0 vig64 a. 1
See Also
Vigenère cipher at Wikipedia
Essays/Under for Caesar cipher
