Vigenère cipher: Difference between revisions
Content added Content deleted
m (→[[Vigenère_Cipher#ALGOL 68]]: Translation of this Vigenère_Cipher#C++ version) |
m (→[[Vigenère_Cipher#ALGOL 68]]: Translation of C version into ALGOL 68) |
||
Line 6: | Line 6: | ||
* [[Vigenère Cipher/Cryptanalysis]] |
* [[Vigenère Cipher/Cryptanalysis]] |
||
=={{header| |
=={{header|ALGOL 68}}== |
||
{{trans|C++}} Note: This specimen retains the original [[Vigenère_Cipher#C++|C++]] coding style. |
|||
<lang cpp>#include <iostream> |
|||
{{works with|ALGOL 68|Revision 1 - no extensions to language used.}} |
|||
#include <string> |
|||
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-1.18.0/algol68g-1.18.0-9h.tiny.el5.centos.fc11.i386.rpm/download 1.18.0-9h.tiny].}} |
|||
using namespace std; |
|||
{{works with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d].}} |
|||
<lang algol68>STRING key := ""; |
|||
PROC vigenere cipher = (REF STRING key)VOID: |
|||
class Vigenere |
|||
( |
|||
{ |
|||
FOR i FROM LWB key TO UPB key DO |
|||
public: |
|||
⚫ | |||
string key; |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
PROC encrypt = (STRING text)STRING: |
|||
Vigenere(string key) |
|||
( |
|||
⚫ | |||
STRING out := ""; |
|||
for(int i = 0; i < key.size(); ++i) |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
} |
|||
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 |
|||
⚫ | |||
j := j MOD UPB key + 1 |
|||
⚫ | |||
c += 'A' - 'a'; |
|||
OD; |
|||
else if(c < 'A' || c > 'Z') |
|||
continue; |
|||
out |
|||
⚫ | |||
); |
|||
j = (j + 1) % key.length(); |
|||
} |
|||
PROC decrypt = (STRING text)STRING: |
|||
⚫ | |||
( |
|||
} |
|||
⚫ | |||
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 |
|||
⚫ | |||
j := j MOD UPB key + 1 |
|||
⚫ | |||
c += 'A' - 'a'; |
|||
OD; |
|||
else if(c < 'A' || c > 'Z') |
|||
continue; |
|||
out |
|||
⚫ | |||
); |
|||
j = (j + 1) % key.length(); |
|||
} |
|||
main: |
|||
return out; |
|||
( |
|||
} |
|||
⚫ | |||
⚫ | |||
⚫ | |||
int main() |
|||
⚫ | |||
{ |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
Output: |
Output: |
||
<pre> |
<pre> |
Revision as of 06:35, 31 May 2011
Vigenère cipher is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
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
Note: This specimen retains the original C++ coding style.
<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++
<lang cpp>#include <iostream>
- 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