TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
xor_cipher.cpp File Reference

Implementation of XOR cipher algorithm. More...

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

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 ()
 

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

Definition in file xor_cipher.cpp.

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

Definition at line 61 of file xor_cipher.cpp.

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

Definition at line 47 of file xor_cipher.cpp.

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

Definition at line 95 of file xor_cipher.cpp.

95 {
96 // Testing
97 test();
98 return 0;
99}
void test()

◆ test()

void test ( )

Function to test above algorithm

Definition at line 75 of file xor_cipher.cpp.

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}
std::string decrypt(const std::string &text, const int &key)
std::string encrypt(const std::string &text, const int &key)