# XOR Cipher

XOR Encryption is an encryption method used to encrypt data and is hard to crack by brute-force method, i.e generating random encryption keys to match with the correct one.

Below is a simple implementation in C++. The concept of implementation is to first define a XOR – encryption key and then to perform XOR operation of the characters in the String with this key which you want to encrypt. To decrypt the encrypted characters we have to perform XOR operation again with the defined key. Here we are encrypting the entire String.

`// C++ program to implement XOR - Encryption ` `#include<bits/stdc++.h> ` ` ` `// The same function is used to encrypt and ` `// decrypt ` `void` `encryptDecrypt(` `char` `inpString[]) ` `{ ` ` ` `// Define XOR key ` ` ` `// Any character value will work ` ` ` `char` `xorKey = ` `'P'` `; ` ` ` ` ` `// calculate length of input string ` ` ` `int` `len = ` `strlen` `(inpString); ` ` ` ` ` `// perform XOR operation of key ` ` ` `// with every caracter in string ` ` ` `for` `(` `int` `i = 0; i < len; i++) ` ` ` `{ ` ` ` `inpString[i] = inpString[i] ^ xorKey; ` ` ` `printf` `(` `"%c"` `,inpString[i]); ` ` ` `} ` `} ` ` ` `// Driver program to test above function ` `int` `main() ` `{ ` ` ` `char` `sampleString[] = ` `"GeeksforGeeks"` `; ` ` ` ` ` `// Encrypt the string ` ` ` `printf` `(` `"Encrypted String: "` `); ` ` ` `encryptDecrypt(sampleString); ` ` ` `printf` `(` `"\n"` `); ` ` ` ` ` `// Decrypt the string ` ` ` `printf` `(` `"Decrypted String: "` `); ` ` ` `encryptDecrypt(sampleString); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

Output:

Encrypted String: 55;#6?"55;# Decrypted String: GeeksforGeeks

The basic idea behind XOR – encryption is, if you don’t know the XOR-encryption key before decrypting the encrypted data, it is impossible to decrypt the data. For example, if you XOR two unknown variables you cannot tell that what the output of those variables are. Consider the operation A XOR B, and this returns true. Now if the value of one of the variable is known we can tell the value of other variable. If A is True then B should be False or if A is False then B should be true according to the properties of the boolean XOR operation. Without knowing one of the value we can not decrypt the data and this idea is used in XOR – encryption.

**Related Articles:**

Vigenère Cipher

Caesar Cipher

This article is contributed by **Harsh Agarwal**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

## Recommended Posts:

- Null Cipher
- Hill Cipher
- Caesar Cipher
- ROT13 cipher
- Vigenère Cipher
- Baconian Cipher
- Keyword Cipher
- Polybius Square Cipher
- Implementation of Affine Cipher
- Latin alphabet cipher
- Columnar Transposition Cipher
- Implementing Atbash Cipher
- Rail Fence Cipher - Encryption and Decryption
- Longest substring of only 4's from the first N characters of the infinite string