xor_cipher.cpp File Reference

Implementation of XOR cipher algorithm. More...

`#include <iostream>`
`#include <string>`
`#include <cassert>`
Include dependency graph for xor_cipher.cpp:

## Namespaces

| ciphers
| Algorithms for encryption and decryption.
| 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 ()

## Detailed Description

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:

- \(A {\oplus} 0 = A\)
- \(A {\oplus} A = 0\)
- \( (A {\oplus} B) {\oplus} C = A {\oplus} (B {\oplus} C)\)
- \( (B {\oplus} A) {\oplus} B = B {\oplus} 0 = B \)

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.

### 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

## Function Documentation

## decrypt()

std::string ciphers::XOR::decrypt | ( | const std::string & | text,
|  | const int & | key
| ) |  |

Decrypt given text using XOR cipher.

Parameters

  text | text to be encrypted
  key | to be used for decryption

Returns
  new decrypted text

{
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
}
std::string

## encrypt()

std::string ciphers::XOR::encrypt | ( | const std::string & | text,
|  | const int & | key
| ) |  |

Encrypt given text using XOR cipher.

Parameters

  text | text to be encrypted
  key | to be used for encyption

Returns
  new encrypted text

{
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
}

## main()

int main | ( | void |  | ) |

Driver Code

{
// Testing
test();
return 0;
}
test
void test()
Definition: xor_cipher.cpp:75
Here is the call graph for this function:

## test()

void test | ( |  | ) |

Function to test above algorithm

{
// 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;
}
std::cout
std::endl
T endl(T... args)
ciphers::XOR::decrypt
std::string decrypt(const std::string &text, const int &key)
Definition: xor_cipher.cpp:61
ciphers::XOR::encrypt
std::string encrypt(const std::string &text, const int &key)
Definition: xor_cipher.cpp:47
Here is the call graph for this function: