From J Wiki
Jump to: navigation, search

Vigenère Cipher

As an addon, load with

The Vigenère cipher is a method of encryption that uses a series of different Caesar ciphers based on the letters of a keyword. Download script: 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



Note 'Examples'
«basic examples»

«symmetry examples»

«encoded examples»

Different character sets can be used for different types of text. Download script: 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. Download script: basic examples

   (97,:26) ];.0 a.   NB. alpha
   'lemon' 0 vigalpha 'attackatdawn'
   'lemon' 1 vigalpha 'lemon' 0 vigalpha 'attackatdawn'


Vigenère cipher is a symmetric-key algorithm using the same key to encode and decode.

In addition, the encoding and decoding are interchangeable. Download script: symmetry examples

   'lemon' 1 vigalpha 'attackatdawn'
   'lemon' 0 vigalpha 'lemon' 1 vigalpha 'attackatdawn'


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. Download script: encoded

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, Download script: encoded examples

   'lemon' 0 vig85 a.
   a. -: 'lemon' 1 vig85 'lemon' 0 vig85 a.

   'lemon' 0 vig64 a.
   a. -: 'lemon' 1 vig64 'lemon' 0 vig64 a.

See Also