Jump to content

Check out our Community Blogs

Register and join over 40,000 other developers!

Recent Status Updates

View All Updates

- - - - -

[C/C++] Beginner's Xor Encryption


  • Please log in to reply
1 reply to this topic

#1 tpPacino


    CC Regular

  • Member
  • PipPipPip
  • 29 posts
  • Location:Sarajevo, Bosnia and Herzegovina
  • Programming Language:C, Java, C++, PHP, (Visual) Basic, PL/SQL, Pascal, Others
  • Learning:Objective-C, C#, JavaScript, Ruby, Haskell, Others

Posted 28 June 2012 - 06:20 AM

Beginner's XOR encryption/decryption

This is a part of a series about encryption which I'll make with time. This first part will contain the simplest of all encryptions.
Before we start, you should know only one thing about computers and of course some C/C++ basics.
The one thing you should know is how the computer works, i.e. how it stores data. Every computer stores data in binary, that is in form of bytes ( 1 or 0 ). 1 byte is 8 bits and 8 bits is a set of 8 zeros and ones.
This is where the tutorial starts.
Binary data can be encrypted using a key, which is based on a bool operator called XOR (Exclusive OR ).

Posted Image
With this you can encrypt strings, integers, etc.
For example let's pretend we want to encrypt a char. Let's use the char 'c'. It's ASCII value is 99 in decimal. We need to convert it to binary, so the binary value of 99 = 1100011.
The next step is getting a key to use for the encryption and later on, the decryption. Let's take the number 12. It's binary value is 0001100.
What we need to do is the following:
---------- // using XOR
1101111 // XOR encrypted char
The decimal value of the encrypted char is 111. That the char 'o'. So we encrypted the char 'c' to char 'o' using XOR encryption with the key 12 ( 0001100 ).
Now we need to decrypt it. We will be using the key as following:
---------- // XOR decryption
1100011 // ---- we get the first char back
If we want to encrypt/decrypt a string, we would do the logic on every character individually.

The code

The implementation will be shown using C++ programming language.

#include <iostream>
#include <string>
using namespace std;
string XOREncryption(string str, int key)
    string enc("");
for (unsigned int i(0); i < str.length(); i++) // iterates through the string to encrypt
        enc += str[i] ^ key; // ^ - XOR operator in C++
    return enc;
string XORDecryption(string str, int key)
    string dec("");
for (unsigned int i(0); i < str.length(); i++)// iterates through the string to decrypt
        dec += str[i] ^ key; // ^ - XOR operator in C++
    return dec;
int main() {

    string str, encrypted, decrypted;
    cin >> str;
    encrypted = XOREncryption(str, 12); // storing the encrypted string
    decrypted = XORDecryption(encrypted, 12); // storing the decrypted string
    cout << "Encrypted string: " << encrypted;
    cout << "\nDecrypted string: " << decrypted;
    return 0;

There are other, more secure methods, but this is a beginner's guide, so we won't complicate stuff too much...
If you got any further questions, please share them with us :)
  • 1

#2 skrtbhtngr


    CC Lurker

  • Just Joined
  • Pip
  • 2 posts
  • Programming Language:C, C++
  • Learning:C, C++

Posted 29 June 2012 - 11:39 AM

Good work and a nice Encryption Program for beginners!!
  • 0

Also tagged with one or more of these keywords: encryption

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download