diff --git a/ciphers/a1z26_cipher.cpp b/ciphers/a1z26_cipher.cpp index 82a3d3bb4..7aa28a02e 100644 --- a/ciphers/a1z26_cipher.cpp +++ b/ciphers/a1z26_cipher.cpp @@ -9,14 +9,16 @@ * @author [Focusucof](https://github.com/Focusucof) */ -#include /// for std::transform and std::replace -#include /// for assert -#include /// for uint8_t -#include /// for IO operations -#include /// for std::map -#include /// for std::stringstream -#include /// for std::string -#include /// for std::vector +#include // for tolower, toupper +#include // for replace, transform +#include // for assert +#include // for uint8_t +#include // for basic_ostream, operator<<, basic_istream, cout +#include // for map +#include // for basic_stringstream +#include // for basic_string, char_traits, operator<<, string +#include // for vector +#include // for pair /** * @namespace ciphers diff --git a/ciphers/atbash_cipher.cpp b/ciphers/atbash_cipher.cpp index 4f0d793f2..11b2f3dc2 100644 --- a/ciphers/atbash_cipher.cpp +++ b/ciphers/atbash_cipher.cpp @@ -12,10 +12,11 @@ * * @author [Focusucof](https://github.com/Focusucof) */ -#include /// for assert -#include /// for IO operations -#include /// for std::map -#include /// for std::string +#include // for assert +#include // for basic_ostream, operator<<, cout, endl +#include // for map +#include // for basic_string, char_traits, allocator, operator<< +#include // for pair /** \namespace ciphers * \brief Algorithms for encryption and decryption diff --git a/ciphers/base64_encoding.cpp b/ciphers/base64_encoding.cpp index 52bce90ff..d4090a596 100644 --- a/ciphers/base64_encoding.cpp +++ b/ciphers/base64_encoding.cpp @@ -11,10 +11,9 @@ * digits. * @author [Ashish Daulatabad](https://github.com/AshishYUO) */ -#include /// for `std::array` -#include /// for `assert` operations -#include /// for integral typedefs -#include /// for IO operations +#include // for assert +#include // for uint32_t, uint8_t +#include // for basic_string, allocator, string, char_traits, ope... /** * @namespace ciphers diff --git a/ciphers/elliptic_curve_key_exchange.cpp b/ciphers/elliptic_curve_key_exchange.cpp index 0a9ce3cd0..7955466ae 100644 --- a/ciphers/elliptic_curve_key_exchange.cpp +++ b/ciphers/elliptic_curve_key_exchange.cpp @@ -21,10 +21,12 @@ * alicePubKey * bobPrivKey = bobPubKey * alicePrivKey = secret * @author [Ashish Daulatabad](https://github.com/AshishYUO) */ -#include /// for assert -#include /// for IO operations +#include // for assert +#include // for basic_ostream, operator<<, char_traits, cout +#include // for basic_string -#include "uint256_t.hpp" /// for 256-bit integer +#include "uint256_t.hpp" // for uint256_t, operator<< +#include "uint128_t.hpp" // for uint128_t /** * @namespace ciphers diff --git a/ciphers/morse_code.cpp b/ciphers/morse_code.cpp index f8ff51c5c..f446a8ec5 100644 --- a/ciphers/morse_code.cpp +++ b/ciphers/morse_code.cpp @@ -11,10 +11,11 @@ * and dashes or dits and dahs. Morse code is named after Samuel Morse, an * inventor of the telegraph. */ -#include -#include -#include -#include +#include // for assert +#include // for basic_ostream, operator<<, endl, cout, cerr +#include // for basic_string, operator==, char_traits, string +#include // for vector +#include // for exit, size_t /** \namespace ciphers * \brief Algorithms for encryption and decryption diff --git a/ciphers/vigenere_cipher.cpp b/ciphers/vigenere_cipher.cpp index 4efd56c00..17e1e3c55 100644 --- a/ciphers/vigenere_cipher.cpp +++ b/ciphers/vigenere_cipher.cpp @@ -1,109 +1,126 @@ /** * @file vigenere_cipher.cpp - * @brief Implementation of [Vigenère cipher](https://en.wikipedia.org/wiki/Vigen%C3%A8re_cipher) algorithm. + * @brief Implementation of [Vigenère + * cipher](https://en.wikipedia.org/wiki/Vigen%C3%A8re_cipher) algorithm. * * @details - * The Vigenère cipher is a method of encrypting alphabetic text by using a series of interwoven vigenere - * ciphers, based on the letters of a keyword. It employs a form of polyalphabetic substitution. + * The Vigenère cipher is a method of encrypting alphabetic text by using a + * series of interwoven vigenere ciphers, based on the letters of a keyword. It + * employs a form of polyalphabetic substitution. * * ### Algorithm - * The encryption can also be represented using modular arithmetic by first transforming - * the letters into numbers, according to the scheme, A → 0, B → 1, ..., Z → 25. - * Encryption of \f$i^{th}\f$ character in Message M by key K can be described mathematically as, - * + * The encryption can also be represented using modular arithmetic by first + * transforming the letters into numbers, according to the scheme, A → 0, B → 1, + * ..., Z → 25. Encryption of \f$i^{th}\f$ character in Message M by key K can + * be described mathematically as, + * * \f[ E_{K}(M_{i}) = (M_{i} + K_{i})\;\mbox{mod}\; 26\f] - * - * while decryption of \f$i^{th}\f$ character in Cipher C by key K can be described mathematically as, + * + * while decryption of \f$i^{th}\f$ character in Cipher C by key K can be + * described mathematically as, * * \f[ D_{k}(C_{i}) = (C_{i} - K_{i} + 26)\;\mbox{mod}\; 26\f] - * - * Where \f$K_{i}\f$ denotes corresponding character in key. If \f$|key| < |text|\f$ than - * same key is repeated untill their lengths are equal. - * + * + * Where \f$K_{i}\f$ denotes corresponding character in key. If \f$|key| < + * |text|\f$ than same key is repeated untill their lengths are equal. + * * For Example, * If M = "ATTACKATDAWN" and K = "LEMON" than K becomes "LEMONLEMONLE". - * - * \note Rather than creating new key of equal length this program does this by using modular index for key - * (i.e. \f$(j + 1) \;\mbox{mod}\; |\mbox{key}|\f$) - * - * \note This program implements Vigenère cipher for only uppercase English alphabet characters (i.e. A-Z). - * + * + * \note Rather than creating new key of equal length this program does this by + * using modular index for key (i.e. \f$(j + 1) \;\mbox{mod}\; |\mbox{key}|\f$) + * + * \note This program implements Vigenère cipher for only uppercase English + * alphabet characters (i.e. A-Z). + * * @author [Deep Raval](https://github.com/imdeep2905) */ -#include -#include -#include +#include // for assert +#include // for size_t +#include // for basic_ostream, operator<<, cout, endl +#include // for basic_string, char_traits, string, operator<< /** \namespace ciphers * \brief Algorithms for encryption and decryption */ namespace ciphers { - /** \namespace vigenere - * \brief Functions for [vigenère cipher](https://en.wikipedia.org/wiki/Vigen%C3%A8re_cipher) algorithm. - */ - namespace vigenere { - namespace { - /** - * This function finds character for given value (i.e.A-Z) - * @param x value for which we want character - * @return corresponding character for perticular value - */ - inline char get_char(const int x) { - // By adding 65 we are scaling 0-25 to 65-90. - // Which are in fact ASCII values of A-Z. - return char(x + 65); - } - /** - * This function finds value for given character (i.e.0-25) - * @param c character for which we want value - * @return returns corresponding value for perticular character - */ - inline int get_value(const char c) { - // A-Z have ASCII values in range 65-90. - // Hence subtracting 65 will scale them to 0-25. - return int(c - 65); - } - } // Unnamed namespace - /** - * Encrypt given text using vigenere cipher. - * @param text text to be encrypted - * @param key to be used for encryption - * @return new encrypted text - */ - std::string encrypt (const std::string &text, const std::string &key) { - std::string encrypted_text = ""; // Empty string to store encrypted text - // Going through each character of text and key - // Note that key is visited in circular way hence j = (j + 1) % |key| - for(size_t i = 0, j = 0; i < text.length(); i++, j = (j + 1) % key.length()) { - int place_value_text = get_value(text[i]); // Getting value of character in text - int place_value_key = get_value(key[j]); // Getting value of character in key - place_value_text = (place_value_text + place_value_key) % 26; // Applying encryption - char encrypted_char = get_char(place_value_text); // Getting new character from encrypted value - encrypted_text += encrypted_char; // Appending encrypted character - } - return encrypted_text; // Returning encrypted text - } - /** - * Decrypt given text using vigenere cipher. - * @param text text to be decrypted - * @param key key to be used for decryption - * @return new decrypted text - */ - std::string decrypt (const std::string &text, const std::string &key) { - // Going through each character of text and key - // Note that key is visited in circular way hence j = (j + 1) % |key| - std::string decrypted_text = ""; // Empty string to store decrypted text - for(size_t i = 0, j = 0; i < text.length(); i++, j = (j + 1) % key.length()) { - int place_value_text = get_value(text[i]); // Getting value of character in text - int place_value_key = get_value(key[j]); // Getting value of character in key - place_value_text = (place_value_text - place_value_key + 26) % 26; // Applying decryption - char decrypted_char = get_char(place_value_text); // Getting new character from decrypted value - decrypted_text += decrypted_char; // Appending decrypted character - } - return decrypted_text; // Returning decrypted text - } - } // namespace vigenere -} // namespace ciphers +/** \namespace vigenere + * \brief Functions for [vigenère + * cipher](https://en.wikipedia.org/wiki/Vigen%C3%A8re_cipher) algorithm. + */ +namespace vigenere { +namespace { +/** + * This function finds character for given value (i.e.A-Z) + * @param x value for which we want character + * @return corresponding character for perticular value + */ +inline char get_char(const int x) { + // By adding 65 we are scaling 0-25 to 65-90. + // Which are in fact ASCII values of A-Z. + return char(x + 65); +} +/** + * This function finds value for given character (i.e.0-25) + * @param c character for which we want value + * @return returns corresponding value for perticular character + */ +inline int get_value(const char c) { + // A-Z have ASCII values in range 65-90. + // Hence subtracting 65 will scale them to 0-25. + return int(c - 65); +} +} // Unnamed namespace +/** + * Encrypt given text using vigenere cipher. + * @param text text to be encrypted + * @param key to be used for encryption + * @return new encrypted text + */ +std::string encrypt(const std::string &text, const std::string &key) { + std::string encrypted_text = ""; // Empty string to store encrypted text + // Going through each character of text and key + // Note that key is visited in circular way hence j = (j + 1) % |key| + for (size_t i = 0, j = 0; i < text.length(); + i++, j = (j + 1) % key.length()) { + int place_value_text = + get_value(text[i]); // Getting value of character in text + int place_value_key = + get_value(key[j]); // Getting value of character in key + place_value_text = + (place_value_text + place_value_key) % 26; // Applying encryption + char encrypted_char = get_char( + place_value_text); // Getting new character from encrypted value + encrypted_text += encrypted_char; // Appending encrypted character + } + return encrypted_text; // Returning encrypted text +} +/** + * Decrypt given text using vigenere cipher. + * @param text text to be decrypted + * @param key key to be used for decryption + * @return new decrypted text + */ +std::string decrypt(const std::string &text, const std::string &key) { + // Going through each character of text and key + // Note that key is visited in circular way hence j = (j + 1) % |key| + std::string decrypted_text = ""; // Empty string to store decrypted text + for (size_t i = 0, j = 0; i < text.length(); + i++, j = (j + 1) % key.length()) { + int place_value_text = + get_value(text[i]); // Getting value of character in text + int place_value_key = + get_value(key[j]); // Getting value of character in key + place_value_text = (place_value_text - place_value_key + 26) % + 26; // Applying decryption + char decrypted_char = get_char( + place_value_text); // Getting new character from decrypted value + decrypted_text += decrypted_char; // Appending decrypted character + } + return decrypted_text; // Returning decrypted text +} +} // namespace vigenere +} // namespace ciphers /** * Function to test above algorithm @@ -116,7 +133,7 @@ void test() { assert(text1 == decrypted1); std::cout << "Original text : " << text1; std::cout << " , Encrypted text (with key = TESLA) : " << encrypted1; - std::cout << " , Decrypted text : "<< decrypted1 << std::endl; + std::cout << " , Decrypted text : " << decrypted1 << std::endl; // Test 2 std::string text2 = "GOOGLEIT"; std::string encrypted2 = ciphers::vigenere::encrypt(text2, "REALLY"); @@ -124,7 +141,7 @@ void test() { assert(text2 == decrypted2); std::cout << "Original text : " << text2; std::cout << " , Encrypted text (with key = REALLY) : " << encrypted2; - std::cout << " , Decrypted text : "<< decrypted2 << std::endl; + std::cout << " , Decrypted text : " << decrypted2 << std::endl; } /** Driver Code */