Vigenère cipher: Difference between revisions
Content added Content deleted
mNo edit summary |
m (→[[Vigenère_Cipher#ALGOL 68]]: Translation of this Vigenère_Cipher#C++ version) |
||
Line 5: | Line 5: | ||
See also: |
See also: |
||
* [[Vigenère Cipher/Cryptanalysis]] |
* [[Vigenère Cipher/Cryptanalysis]] |
||
=={{header|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: |
|||
<pre> |
|||
Beware the Jabberwock, my son! The jaws that bite, the claws that catch! |
|||
Encrypted: WMCEEIKLGRPIFVMEUGXQPWQVIOIAVEYXUEKFKBTALVXTGAFXYEVKPAGY |
|||
Decrypted: BEWARETHEJABBERWOCKMYSONTHEJAWSTHATBITETHECLAWSTHATCATCH |
|||
</pre> |
|||
=={{header|C++}}== |
=={{header|C++}}== |
Revision as of 06:30, 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:
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
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