XOR Cipher in C++

XOR cipher or XOR encryption is a data encryption method that cannot be cracked by brute-force method.

Brute-force method is a method of random encryption key generation and matching them with the correct one.

To implement this encryption method, we will define an encryption key(random character) and perform XOR of all characters of the string with the encryption key. This will encrypt all characters of the string.

Program to show the implementation of encryption −


 Live Demo

using namespace std;
void XORChiper(char orignalString[]) {
   char xorKey = 'T';
   int len = strlen(orignalString);
   for (int i = 0; i < len; i++){
      orignalString[i] = orignalString[i] ^ xorKey;
int main(){
   char sampleString[] = "Hello!";
   cout<<"The string is: "<<sampleString<<endl;
   cout<<"Encrypted String: ";
   return 0;


The string is: Hello!
Encrypted String: 188;u

Updated on: 17-Apr-2020

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started