Algorithms_in_C++  1.0.0
Set of algorithms implemented in C++.
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
texttext to be encrypted
keyto be used for decryption
Returns
new decrypted text
61  {
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  }

◆ encrypt()

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

Encrypt given text using XOR cipher.

Parameters
texttext to be encrypted
keyto be used for encyption
Returns
new encrypted text
47  {
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  }

◆ main()

int main ( void  )

Driver Code

95  {
96  // Testing
97  test();
98  return 0;
99 }
Here is the call graph for this function:

◆ test()

void test ( )

Function to test above algorithm

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