From 7f92f54bdea33fef4da6e2c01b7d61122d5893cf Mon Sep 17 00:00:00 2001 From: Deep Raval Date: Mon, 20 Jul 2020 02:15:45 +0530 Subject: [PATCH] feat: Add XOR Cipher (#961) * Added XOR Cipher * Remoced repetition of c ^ key in encrypt * Added explicit type conversion * Applied Suggested Changes * Added bullet points --- ciphers/xor_cipher.cpp | 99 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 ciphers/xor_cipher.cpp diff --git a/ciphers/xor_cipher.cpp b/ciphers/xor_cipher.cpp new file mode 100644 index 000000000..18cbdd61f --- /dev/null +++ b/ciphers/xor_cipher.cpp @@ -0,0 +1,99 @@ +/** + * @file xor_cipher.cpp + * @brief Implementation of [XOR cipher](https://en.wikipedia.org/wiki/XOR_cipher) algorithm. + * + * @details + * In cryptography, the simple XOR cipher is a type of additive cipher, an encryption + * algorithm that operates according to the principles: + * + * * \f$A {\oplus} 0 = A\f$ + * * \f$A {\oplus} A = 0\f$ + * * \f$ (A {\oplus} B) {\oplus} C = A {\oplus} (B {\oplus} C)\f$ + * * \f$ (B {\oplus} A) {\oplus} B = B {\oplus} 0 = B \f$ + * + * + * where \f$\oplus\f$ symbol denotes the exclusive disjunction (XOR) operation. + * This operation is sometimes called modulus 2 addition (or subtraction, which is identical). + * With this logic, a string of text can be encrypted by applying the bitwise XOR operator to + * every character using a given key. To decrypt the output, merely reapplying the XOR function + * with the key will remove the cipher. + * + * ### Algorithm + * Choose the key for encryption and apply XOR operation to each character of a string. + * Reapplying XOR operation to each character of encrypted string will give original string back. + * + * \note This program implements XOR Cipher for string with ASCII characters. + * + * @author [Deep Raval](https://github.com/imdeep2905) + */ +#include +#include +#include + +/** \namespace ciphers + * \brief Algorithms for encryption and decryption + */ +namespace ciphers { + /** \namespace XOR + * \brief Functions for [XOR cipher](https://en.wikipedia.org/wiki/XOR_cipher) algorithm. + */ + namespace XOR { + /** + * Encrypt given text using XOR cipher. + * @param text text to be encrypted + * @param key to be used for encyption + * @return new encrypted text + */ + std::string encrypt (const std::string &text, const int &key) { + std::string encrypted_text = ""; // Empty string to store encrypted text + for (auto &c: text) { // Going through each character + char encrypted_char = char(c ^ key); // Applying encyption + encrypted_text += encrypted_char; // Appending encrypted character + } + return encrypted_text; // Returning encrypted text + } + /** + * Decrypt given text using XOR cipher. + * @param text text to be encrypted + * @param key to be used for decryption + * @return new decrypted text + */ + std::string decrypt (const std::string &text, const int &key) { + std::string decrypted_text = ""; // Empty string to store decrypted text + for (auto &c : text) { // Going through each character + char decrypted_char = char(c ^ key); // Applying decryption + decrypted_text += decrypted_char; // Appending decrypted character + } + return decrypted_text; // Returning decrypted text + } + } // namespace XOR +} // namespace ciphers + +/** + * Function to test above algorithm + */ +void test() { + // Test 1 + std::string text1 = "Whipalsh! : Do watch this movie..."; + std::string encrypted1 = ciphers::XOR::encrypt(text1, 17); + std::string decrypted1 = ciphers::XOR::decrypt(encrypted1, 17); + assert(text1 == decrypted1); + std::cout << "Original text : " << text1; + std::cout << " , Encrypted text (with key = 17) : " << encrypted1; + std::cout << " , Decrypted text : "<< decrypted1 << std::endl; + // Test 2 + std::string text2 = "->Valar M0rghulis<-"; + std::string encrypted2 = ciphers::XOR::encrypt(text2, 29); + std::string decrypted2 = ciphers::XOR::decrypt(encrypted2, 29); + assert(text2 == decrypted2); + std::cout << "Original text : " << text2; + std::cout << " , Encrypted text (with key = 29) : " << encrypted2; + std::cout << " , Decrypted text : "<< decrypted2 << std::endl; +} + +/** Driver Code */ +int main() { + // Testing + test(); + return 0; +}