|
| 1 | +/** |
| 2 | + * @file xor_cipher.cpp |
| 3 | + * @brief Implementation of [XOR cipher](https://en.wikipedia.org/wiki/XOR_cipher) algorithm. |
| 4 | + * |
| 5 | + * @details |
| 6 | + * In cryptography, the simple XOR cipher is a type of additive cipher, an encryption |
| 7 | + * algorithm that operates according to the principles: |
| 8 | + * |
| 9 | + * * \f$A {\oplus} 0 = A\f$ |
| 10 | + * * \f$A {\oplus} A = 0\f$ |
| 11 | + * * \f$ (A {\oplus} B) {\oplus} C = A {\oplus} (B {\oplus} C)\f$ |
| 12 | + * * \f$ (B {\oplus} A) {\oplus} B = B {\oplus} 0 = B \f$ |
| 13 | + * |
| 14 | + * |
| 15 | + * where \f$\oplus\f$ symbol denotes the exclusive disjunction (XOR) operation. |
| 16 | + * This operation is sometimes called modulus 2 addition (or subtraction, which is identical). |
| 17 | + * With this logic, a string of text can be encrypted by applying the bitwise XOR operator to |
| 18 | + * every character using a given key. To decrypt the output, merely reapplying the XOR function |
| 19 | + * with the key will remove the cipher. |
| 20 | + * |
| 21 | + * ### Algorithm |
| 22 | + * Choose the key for encryption and apply XOR operation to each character of a string. |
| 23 | + * Reapplying XOR operation to each character of encrypted string will give original string back. |
| 24 | + * |
| 25 | + * \note This program implements XOR Cipher for string with ASCII characters. |
| 26 | + * |
| 27 | + * @author [Deep Raval](https://github.com/imdeep2905) |
| 28 | + */ |
| 29 | +#include <iostream> |
| 30 | +#include <string> |
| 31 | +#include <cassert> |
| 32 | + |
| 33 | +/** \namespace ciphers |
| 34 | + * \brief Algorithms for encryption and decryption |
| 35 | + */ |
| 36 | +namespace ciphers { |
| 37 | + /** \namespace XOR |
| 38 | + * \brief Functions for [XOR cipher](https://en.wikipedia.org/wiki/XOR_cipher) algorithm. |
| 39 | + */ |
| 40 | + namespace XOR { |
| 41 | + /** |
| 42 | + * Encrypt given text using XOR cipher. |
| 43 | + * @param text text to be encrypted |
| 44 | + * @param key to be used for encyption |
| 45 | + * @return new encrypted text |
| 46 | + */ |
| 47 | + std::string encrypt (const std::string &text, const int &key) { |
| 48 | + std::string encrypted_text = ""; // Empty string to store encrypted text |
| 49 | + for (auto &c: text) { // Going through each character |
| 50 | + char encrypted_char = char(c ^ key); // Applying encyption |
| 51 | + encrypted_text += encrypted_char; // Appending encrypted character |
| 52 | + } |
| 53 | + return encrypted_text; // Returning encrypted text |
| 54 | + } |
| 55 | + /** |
| 56 | + * Decrypt given text using XOR cipher. |
| 57 | + * @param text text to be encrypted |
| 58 | + * @param key to be used for decryption |
| 59 | + * @return new decrypted text |
| 60 | + */ |
| 61 | + std::string decrypt (const std::string &text, const int &key) { |
| 62 | + std::string decrypted_text = ""; // Empty string to store decrypted text |
| 63 | + for (auto &c : text) { // Going through each character |
| 64 | + char decrypted_char = char(c ^ key); // Applying decryption |
| 65 | + decrypted_text += decrypted_char; // Appending decrypted character |
| 66 | + } |
| 67 | + return decrypted_text; // Returning decrypted text |
| 68 | + } |
| 69 | + } // namespace XOR |
| 70 | +} // namespace ciphers |
| 71 | + |
| 72 | +/** |
| 73 | + * Function to test above algorithm |
| 74 | + */ |
| 75 | +void test() { |
| 76 | + // Test 1 |
| 77 | + std::string text1 = "Whipalsh! : Do watch this movie..."; |
| 78 | + std::string encrypted1 = ciphers::XOR::encrypt(text1, 17); |
| 79 | + std::string decrypted1 = ciphers::XOR::decrypt(encrypted1, 17); |
| 80 | + assert(text1 == decrypted1); |
| 81 | + std::cout << "Original text : " << text1; |
| 82 | + std::cout << " , Encrypted text (with key = 17) : " << encrypted1; |
| 83 | + std::cout << " , Decrypted text : "<< decrypted1 << std::endl; |
| 84 | + // Test 2 |
| 85 | + std::string text2 = "->Valar M0rghulis<-"; |
| 86 | + std::string encrypted2 = ciphers::XOR::encrypt(text2, 29); |
| 87 | + std::string decrypted2 = ciphers::XOR::decrypt(encrypted2, 29); |
| 88 | + assert(text2 == decrypted2); |
| 89 | + std::cout << "Original text : " << text2; |
| 90 | + std::cout << " , Encrypted text (with key = 29) : " << encrypted2; |
| 91 | + std::cout << " , Decrypted text : "<< decrypted2 << std::endl; |
| 92 | +} |
| 93 | + |
| 94 | +/** Driver Code */ |
| 95 | +int main() { |
| 96 | + // Testing |
| 97 | + test(); |
| 98 | + return 0; |
| 99 | +} |
0 commit comments