Addons/convert/misc/vig

Vigenère Cipher

As an [JSvnAddons]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.

«vig.ijs»=
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.

«charsets»=
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.

«basic examples»=
   (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.

«symmetry examples»=
   '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.

«encoded»=
NB. base64 encoded
vig64=: 1 : 0
:
  require '~system\packages\misc\base64.ijs'
  mv=. m vigascii
  x (_64 ]\ tobase64@mv)`(mv frombase64@(, -. CRLF,' '"_))@.m y
)

NB. ascii85 encoded
vig85=: 1 : 0
:
  require '~system\packages\misc\ascii85.ijs'
  mv=. m vigascii
  x (toascii85@mv)`(mv fromascii85)@.m y
)

For example,

«encoded examples»=
   '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


CategoryLiterate

last edited 2007-03-25 12:17:51 by OlegKobchenko