TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
|
Implementation of XOR cipher algorithm. More...
#include <iostream>
#include <string>
#include <cassert>
Go to the source code of this file.
Namespaces | |
namespace | ciphers |
Algorithms for encryption and decryption. | |
namespace | XOR |
Functions for XOR cipher algorithm. | |
Functions | |
std::string | ciphers::XOR::encrypt (const std::string &text, const int &key) |
std::string | ciphers::XOR::decrypt (const std::string &text, const int &key) |
void | test () |
int | main () |
Implementation of XOR cipher algorithm.
In cryptography, the simple XOR cipher is a type of additive cipher, an encryption algorithm that operates according to the principles:
where \(\oplus\) 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.
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.
Definition in file xor_cipher.cpp.
std::string ciphers::XOR::decrypt | ( | const std::string & | text, |
const int & | key ) |
Decrypt given text using XOR cipher.
text | text to be encrypted |
key | to be used for decryption |
Definition at line 61 of file xor_cipher.cpp.
std::string ciphers::XOR::encrypt | ( | const std::string & | text, |
const int & | key ) |
Encrypt given text using XOR cipher.
text | text to be encrypted |
key | to be used for encyption |
Definition at line 47 of file xor_cipher.cpp.
int main | ( | void | ) |
Driver Code
Definition at line 95 of file xor_cipher.cpp.
void test | ( | ) |
Function to test above algorithm
Definition at line 75 of file xor_cipher.cpp.