Vigenère cipher: Difference between revisions

From Rosetta Code
Content added Content deleted
m (→‎{{header|D}}: added const for key)
(Added CoffeeScript)
Line 272: Line 272:
Decrypted: BEWARETHEJABBERWOCKMYSONTHEJAWSTHATBITETHECLAWSTHATCATCH
Decrypted: BEWARETHEJABBERWOCKMYSONTHEJAWSTHATBITETHECLAWSTHATCATCH
</pre>
</pre>

=={{header|CoffeeScript}}==
{{trans|D}}
<lang coffeescript># Simple helper since charCodeAt is quite long to write.
code = (char) -> char.charCodeAt()

encrypt = (text, key) ->
res = []
j = 0
for c in text.toUpperCase()
continue if c < 'A' or c > 'Z'
res.push ((code c) + (code key[j]) - 130) % 26 + 65
j = ++j % key.length
String.fromCharCode res...

decrypt = (text, key) ->
res = []
j = 0
for c in text.toUpperCase()
continue if c < 'A' or c > 'Z'
res.push ((code c) - (code key[j]) + 26) % 26 + 65
j = ++j % key.length
String.fromCharCode res...

# Trying it out
key = "VIGENERECIPHER"
original = "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!"
encrypted = encrypt original, key

console.log "Original : #{original}"
console.log "Encrypted : #{encrypted}"
console.log "Decrypted : #{decrypt encrypted, key}"</lang>
<pre>Original : Beware the Jabberwock, my son! The jaws that bite, the claws that catch!
Encrypted : WMCEEIKLGRPIFVMEUGXQPWQVIOIAVEYXUEKFKBTALVXTGAFXYEVKPAGY
Decrypted : BEWARETHEJABBERWOCKMYSONTHEJAWSTHATBITETHECLAWSTHATCATCH</pre>


=={{header|D}}==
=={{header|D}}==

Revision as of 01:46, 4 June 2011

Task
Vigenère cipher
You are encouraged to solve this task according to the task description, using any language you may know.

Implement a Vigenère cypher, both encryption and decryption. The program should handle keys and text of unequal length, and should capitalize everything and discard non-alphabetic characters. (If your program handles non-alphabetic characters in another way, make a note of it.)

See also:

ALGOL 68

Translation of: C++

Note: This specimen retains the original C++ coding style.

Works with: ALGOL 68 version Revision 1 - no extensions to language used.
Works with: ALGOL 68G version Any - tested with release 1.18.0-9h.tiny.
Works with: ELLA ALGOL 68 version Any (with appropriate job cards) - tested with release 1.8-8d.

<lang algol68>STRING key := "";

PROC vigenere cipher = (REF STRING key)VOID: (

 FOR i FROM LWB key TO UPB key DO
   IF key[i] >= "A" AND key[i] <= "Z" THEN
     key +:= key[i] FI;
   IF key[i] >= "a" AND key[i] <= "z" THEN
     key +:= REPR(ABS key[i] + ABS"A" - ABS"a") FI
 OD

);

PROC encrypt = (STRING text)STRING: (

 STRING out := "";
 INT j := LWB text;
 FOR i FROM LWB text TO UPB text DO
   CHAR c := text[i];
   IF c >= "a" AND c <= "z" THEN
     c := REPR(ABS c + (ABS"A" - ABS"a")) FI;
   IF c >= "A" AND c <= "Z" THEN
     out +:= REPR((ABS c + ABS key[j] - 2*ABS"A") MOD 26 + ABS"A");
     j := j MOD UPB key + 1
   FI
 OD;
 out

);

PROC decrypt = (STRING text)STRING: (

 STRING out;
 INT j := LWB text;
 FOR i FROM LWB text TO UPB text DO
   CHAR c := text[i];
   IF c >= "a" AND c <= "z" THEN
     c := REPR(ABS c + (ABS"A" - ABS"a")) FI;
   IF c >= "A" AND c <= "Z" THEN
     out +:= REPR((ABS c - ABS key[j] + 26) MOD 26 + ABS"A");
     j := j MOD UPB key + 1
   FI
 OD;
 out

);

main: (

 vigenere cipher(key:="VIGENERECIPHER");
 STRING original := "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!";
 STRING encrypted := encrypt(original);
 STRING decrypted := decrypt(encrypted);
 print((original, new line));
 print(("Encrypted: ", encrypted, new line));
 print(("Decrypted: ", decrypted, new line))

)</lang> Output:

Beware the Jabberwock, my son! The jaws that bite, the claws that catch!
Encrypted: WMCEEIKLGRPIFVMEUGXQPWQVIOIAVEYXUEKFKBTALVXTGAFXYEVKPAGY
Decrypted: BEWARETHEJABBERWOCKMYSONTHEJAWSTHATBITETHECLAWSTHATCATCH

C

Translation of: C++

<lang c>#include <stdio.h>

  1. include <stdlib.h>
  2. include <string.h>
  3. include <ctype.h>

typedef const char *vigenere_t;

// for -std=c99

  1. if !defined(_BSD_SOURCE) && !defined(_SVID_SOURCE) && !defined(_XOPEN_SOURCE)
  2. define isascii(V) ((V) >= 32 && (V) <= 127 ? 1 : 0)
  3. endif

vigenere_t vigenere(char *key) {

 int i, j = 0;
 const size_t l = strlen(key);
 char *r = malloc(l);
 if (r != NULL)
 {
   for(i = 0; i < l; i++)
   {
     if ( isalpha(toupper(key[i])) && isascii(key[i]) )
     {

r[j] = toupper(key[i]); j++;

     }
   }
   r[j] = 0;
 }
 return (vigenere_t)r;

}

void vigenere_delete(vigenere_t v) {

 free((void *)v);

}

char *vigenere_encrypt(vigenere_t v, const char *s) {

 const char *k = (const char *)v;
 int i, j, ri = 0;
 const size_t l = strlen(s);
 const size_t kl = strlen(k);
 char *r = malloc(l);
 
 if (r != NULL)
 {
   for(i = 0, j = 0; i < l; i++)
   {
     char c = toupper(s[i]);
     if (!isascii(s[i]) || !isalpha(s[i])) continue;
     r[ri] = (c + k[j] - 2*'A') % 26 + 'A';
     j = (j + 1) % kl;
     ri++;
   }
   r[ri] = 0;
 }
 return r;

}

char *vigenere_descrypt(vigenere_t v, const char *s) {

 const char *k = (const char *)v;
 int i, j, ri = 0;
 const size_t l = strlen(s);
 const size_t kl = strlen(k);
 char *r = malloc(l);
 if (r != NULL)
 {
   for(i = 0, j = 0; i < l; i++)
   {
     char c = toupper(s[i]);
     if (!isascii(s[i])) continue;
     r[ri] = (c - k[j] + 26) % 26 + 'A';
     j = (j + 1) % kl;
     ri++;      
   }
 }
 return r;

}

// testing

const static char *original = "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!";

int main() {

 vigenere_t v = vigenere("VIGENERECIPHER");
 char *enc, *dec;
 enc = vigenere_encrypt(v, original);
 dec = vigenere_descrypt(v, enc);
 printf("%s\n"
        "Encrypted: %s\n"
        "Decrypted: %s\n", original, enc, dec);
 free(enc); free(dec);
 vigenere_delete(v);
 return 0;

}</lang>

C++

<lang cpp>#include <iostream>

  1. include <string>

using namespace std;

class Vigenere { public:

 string key;
 Vigenere(string key)
 {
   for(int i = 0; i < key.size(); ++i)
   {
     if(key[i] >= 'A' && key[i] <= 'Z')
       this->key += key[i];
     else if(key[i] >= 'a' && key[i] <= 'z')
       this->key += key[i] + 'A' - 'a';
   }
 }
 string encrypt(string text)
 {
   string out;
   for(int i = 0, j = 0; i < text.length(); ++i)
   {
     char c = text[i];
     
     if(c >= 'a' && c <= 'z')
       c += 'A' - 'a';
     else if(c < 'A' || c > 'Z')
       continue;
     out += (c + key[j] - 2*'A') % 26 + 'A'; 
     j = (j + 1) % key.length();
   }
   return out;
 }
 string decrypt(string text)
 {
   string out;
   for(int i = 0, j = 0; i < text.length(); ++i)
   {
     char c = text[i];
     
     if(c >= 'a' && c <= 'z')
       c += 'A' - 'a';
     else if(c < 'A' || c > 'Z')
       continue;
     out += (c - key[j] + 26) % 26 + 'A'; 
     j = (j + 1) % key.length();
   }
   return out;
 }

};

int main() {

 Vigenere cipher("VIGENERECIPHER");
 string original = "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!";
 string encrypted = cipher.encrypt(original);
 string decrypted = cipher.decrypt(encrypted);
 cout << original << endl;
 cout << "Encrypted: " << encrypted << endl;
 cout << "Decrypted: " << decrypted << endl;

}</lang>

Output:

Beware the Jabberwock, my son! The jaws that bite, the claws that catch!
Encrypted: WMCEEIKLGRPIFVMEUGXQPWQVIOIAVEYXUEKFKBTALVXTGAFXYEVKPAGY
Decrypted: BEWARETHEJABBERWOCKMYSONTHEJAWSTHATBITETHECLAWSTHATCATCH

CoffeeScript

Translation of: D

<lang coffeescript># Simple helper since charCodeAt is quite long to write. code = (char) -> char.charCodeAt()

encrypt = (text, key) -> res = [] j = 0

for c in text.toUpperCase() continue if c < 'A' or c > 'Z'

res.push ((code c) + (code key[j]) - 130) % 26 + 65 j = ++j % key.length

String.fromCharCode res...

decrypt = (text, key) -> res = [] j = 0

for c in text.toUpperCase() continue if c < 'A' or c > 'Z'

res.push ((code c) - (code key[j]) + 26) % 26 + 65 j = ++j % key.length

String.fromCharCode res...

  1. Trying it out

key = "VIGENERECIPHER" original = "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!" encrypted = encrypt original, key

console.log "Original  : #{original}" console.log "Encrypted : #{encrypted}" console.log "Decrypted : #{decrypt encrypted, key}"</lang>

Original  : Beware the Jabberwock, my son! The jaws that bite, the claws that catch!
Encrypted : WMCEEIKLGRPIFVMEUGXQPWQVIOIAVEYXUEKFKBTALVXTGAFXYEVKPAGY
Decrypted : BEWARETHEJABBERWOCKMYSONTHEJAWSTHATBITETHECLAWSTHATCATCH

D

Translation of: C++

<lang d>import std.stdio, std.string;

string encrypt(string text, in string key) {

   string res = "";
   text = text.toupper;
   for (int i = 0, j = 0; i < text.length; i++) {
       char c = text[i];
       if(c < 'A' || c > 'Z') continue;
       res ~= (c + key[j] - 2 * 'A') % 26 + 'A'; 
       j = ++j % key.length;
   }
   return res;

}

string decrypt(string text, in string key) {

   string res = "";
   text = text.toupper;
   for (int i = 0, j = 0; i < text.length; i++) {
       char c = text[i];
       if(c < 'A' || c > 'Z') continue;
       res ~= (c - key[j] + 26) % 26 + 'A'; 
       j = ++j % key.length;
   }
   return res;

}

void main() {

   auto key = "VIGENERECIPHER"; 
   auto original = "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!";
   auto encrypted = encrypt(original, key);
   writeln(encrypted);
   writeln(decrypt(encrypted, key));

}</lang>

WMCEEIKLGRPIFVMEUGXQPWQVIOIAVEYXUEKFKBTALVXTGAFXYEVKPAGY
BEWARETHEJABBERWOCKMYSONTHEJAWSTHATBITETHECLAWSTHATCATCH

Icon and Unicon

<lang Icon>procedure main()

  ptext := "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!"
  write("Key        = ",ekey := "VIGENERECIPHER")
  write("Plain Text = ",ptext)
  write("Normalized = ",GFormat(ptext := NormalizeText(ptext)))
  write("Enciphered = ",GFormat(ctext := Vignere("e",ekey,ptext)))
  write("Deciphered = ",GFormat(ptext := Vignere("d",ekey,ctext)))

end

procedure Vignere(mode,ekey,ptext,alpha) #: Vignere cipher

  /alpha := &ucase                                              # default 
  if *alpha ~= *cset(alpha) then runerr(205,alpha)              # no dups
  alpha ||:= alpha                                              # unobstructed
  every ctext:="" & p:=ptext[i := 1 to *ptext] & k:=ekey[(i-1)%*ekey+1] do 
     case mode of {
        "e"|"encrypt": 
           ctext||:=map(p,alpha[1+:*alpha/2],alpha[find(k,alpha)+:(*alpha/2)])
        "d"|"decrypt": 
           ctext||:=map(p,alpha[find(k,alpha)+:(*alpha/2)],alpha[1+:*alpha/2])
        default: runerr(205,mode)
     }

return ctext end</lang>

The following helper procedures will be of general use with classical cryptography tasks. <lang Icon> link strings

procedure NormalizeText(ptext,alpha) #: text/case classical crypto helper

  /alpha := &ucase                                              # default 
  if &lcase === (alpha := cset(alpha)) then ptext := map(ptext) # lower
  if &ucase === alpha  then ptext := map(ptext,&lcase,&ucase)   # upper
  return deletec(ptext,&cset--alpha)                            # only alphas

end

procedure GFormat(text) #: 5 letter group formatting helper

  text ?  (s := "", until pos(0) do s ||:= " " || move(5)|tab(0))
  return s[2:0]

end</lang>

strings.icn provides deletec

Output:

Key        = VIGENERECIPHER
Plain Text = Beware the Jabberwock, my son! The jaws that bite, the claws that catch!
Normalized = BEWAR ETHEJ ABBER WOCKM YSONT HEJAW STHAT BITET HECLA WSTHA TCATCH
Enciphered = WMCEE IKLGR PIFVM EUGXQ PWQVI OIAVE YXUEK FKBTA LVXTG AFXYE VKPAGY
Deciphered = BEWAR ETHEJ ABBER WOCKM YSONT HEJAW STHAT BITET HECLA WSTHA TCATCH

J

Solution:
Using vig from the convert/misc/vig addon: <lang j>NB.*vig c Vigenère cipher NB. cipher=. key 0 vig charset plain NB. plain=. key 1 vig charset cipher vig=: conjunction define

 r=. (#y) $ n i.x
 n {~ (#n) | (r*_1^m) + n i.y

)

ALPHA=: (65,:26) ];.0 a. NB. Character Set preprocess=: (#~ e.&ALPHA)@toupper NB. force uppercase and discard non-alpha chars vigEncryptRC=: 0 vig ALPHA preprocess vigDecryptRC=: 1 vig ALPHA preprocess</lang>

Example Use: <lang j> 'VIGENERECIPHER' vigEncryptRC 'Beware the Jabberwock, my son! The jaws that bite, the claws that catch!' WMCEEIKLGRPIFVMEUGXQPWQVIOIAVEYXUEKFKBTALVXTGAFXYEVKPAGY

  'VIGENERECIPHER' vigDecryptRC 'WMCEEIKLGRPIFVMEUGXQPWQVIOIAVEYXUEKFKBTALVXTGAFXYEVKPAGY'

BEWARETHEJABBERWOCKMYSONTHEJAWSTHATBITETHECLAWSTHATCATCH</lang>

PicoLisp

<lang PicoLisp>(de vigenereKey (Str)

  (extract
     '((C)
        (when (>= "Z" (uppc C) "A")
           (- (char (uppc C)) 65) ) )
     (chop Str) ) )

(de vigenereEncrypt (Str Key)

  (pack
     (mapcar
        '((C K)
           (char (+ 65 (% (+ C K) 26))) )
        (vigenereKey Str)
        (apply circ (vigenereKey Key)) ) ) )

(de vigenereDecrypt (Str Key)

  (pack
     (mapcar
        '((C K)
           (char (+ 65 (% (+ 26 (- C K)) 26))) )
        (vigenereKey Str)
        (apply circ (vigenereKey Key)) ) ) )</lang>

Test:

: (vigenereEncrypt
   "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!"
   "VIGENERECIPHER" )
-> "WMCEEIKLGRPIFVMEUGXQPWQVIOIAVEYXUEKFKBTALVXTGAFXYEVKPAGY"

: (vigenereDecrypt @ "VIGENERECIPHER")
-> "BEWARETHEJABBERWOCKMYSONTHEJAWSTHATBITETHECLAWSTHATCATCH"

Tcl

Translation of: C++

<lang tcl>package require Tcl 8.6

oo::class create Vigenere {

   variable key
   constructor {protoKey} {

foreach c [split $protoKey ""] { if {[regexp {[A-Za-z]} $c]} { lappend key [scan [string toupper $c] %c] } }

   }
   method encrypt {text} {

set out "" set j 0 foreach c [split $text ""] { if {[regexp {[^a-zA-Z]} $c]} continue scan [string toupper $c] %c c append out [format %c [expr {($c+[lindex $key $j]-130)%26+65}]] set j [expr {($j+1) % [llength $key]}] } return $out

   }
   method decrypt {text} {

set out "" set j 0 foreach c [split $text ""] { if {[regexp {[^A-Z]} $c]} continue scan $c %c c append out [format %c [expr {($c-[lindex $key $j]+26)%26+65}]] set j [expr {($j+1) % [llength $key]}] } return $out

   }

}</lang> Demonstrating: <lang tcl>set cypher [Vigenere new "Vigenere Cipher"] set original "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!" set encrypted [$cypher encrypt $original] set decrypted [$cypher decrypt $encrypted] puts $original puts "Encrypted: $encrypted" puts "Decrypted: $decrypted"</lang> Output:

Beware the Jabberwock, my son! The jaws that bite, the claws that catch!
Encrypted: WMCEEIKLGRPIFVMEUGXQPWQVIOIAVEYXUEKFKBTALVXTGAFXYEVKPAGY
Decrypted: BEWARETHEJABBERWOCKMYSONTHEJAWSTHATBITETHECLAWSTHATCATCH