You are on page 1of 2

XOR encryption

XOR encryption (or Exclusive-OR encryption) is a common method of encrypting text into a format that
cannot be trivially cracked by the average person. XOR encryption is great for storing things like game
save data, and other data types that are stored locally on a users computer, that while not a big deal if
they are tampered with, you would like to deter people from doing so. XOR encryption is also used often
as a part of more complex encryption algorithms

In cryptography, the simple XOR cipher is a type of additive cipher[1]an encryption algorithm
that operates according to the principles:

0 XOR 0 = 0
0 XOR 1 = 1
1 XOR 0 = 1
1 XOR 1 = 0

01101001 in 8-bit ASCII) can be encrypted with the repeating key 11110011 as follows:

01010111 01101001 01101011 01101001


11110011 11110011 11110011 11110011
= 10100100 10011010 10011000 10011010

And conversely, for decryption:

10100100 10011010 10011000 10011010


11110011 11110011 11110011 11110011
= 01010111 01101001 01101011 01101001

#include <iostream>

using namespace std;

string encryptDecrypt(string toEncrypt) {


char key = 'K'; //Any char will work
string output = toEncrypt;

for (int i = 0; i < toEncrypt.size(); i++)


output[i] = toEncrypt[i] ^ key;

return output;
}

int main(int argc, const char * argv[])


{
string encrypted = encryptDecrypt("kylewbanks.com");
cout << "Encrypted:" << encrypted << "\n";

string decrypted = encryptDecrypt(encrypted);


cout << "Decrypted:" << decrypted << "\n";

return 0;
}

Class Activity 1: Re-write above program to allow users to input string variable

You might also like