Home arrow Security arrow Page 2 - A Sequel to Cryptography

Using XOR on an Example - Security

This is the second part of a series covering cryptography algorithms. If by any chance you have missed its first part, I urge you to check it out right now. It is called "An Introduction to Cryptography." In order to understand this article, it is crucial to grasp the concepts explained in that part.

  1. A Sequel to Cryptography
  2. Using XOR on an Example
  3. Breaking XOR Encryption
  4. Conclusions
By: Barzan "Tony" Antal
Rating: starstarstarstarstar / 7
July 24, 2007

print this article



Let's XOR our example text file with the password "longpassword123."

%'pl$=#-m:f49ne%!+#*"<#8(=w%#)#<8<s#>+pbml<(nz#8nf"'!zm@DF +u)!!s)?nP$(*-l+p8anw)>:/l9+p8cnp(+=g*>*ebm1y{5{uw

Check out the full C program:

#include <fstream>

#include <stdio.h>

#include <string.h>

using namespace std;

char srcfile[30], dstfile[30], key[255];

void main()


   printf("Source file: "); scanf("%s",srcfile);

   printf("nDestination file: "); scanf("%s",dstfile);

   printf("nPassword (up to 255 chars): ");

   //the start of the key preparation process

   for (int i=0; i<255; i++)


   scanf("%s",key); int temp=0; char key_backup[255];

   for (i=0;i<255;i++)


        if (key[i]=='')




   strcpy(key_backup, key);

   for (i=temp; i>=0; i--)

       key[temp-i]=key[temp-i] & (~key_backup[i]);

   //key preparation process is done; we're ready to XOR!

   ifstream s(srcfile, ios::binary);

   ofstream d(dstfile, ios::binary);

   if (!s || !d)

       printf("nERROR: Couldn't open one of the files!");

   i=0; char c;

   while ((s.read((char*)&c, sizeof(c))))


       if (key[i]=='')


       c=key[i] ^ c;

       d.write((char*)&c, sizeof(c));



   s.close();  d.close();

   printf("nAll done. ");


Once again, the download button below leads to an archive that contains the ready-to-run executable and source code of the above algorithm. Enjoy!

>>> More Security Articles          >>> More By Barzan "Tony" Antal

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Secure Your Business for Data Privacy Day
- Google Testing Security Fob Password Alterna...
- Security News Highlights Concerns
- Going to Extremes for Data Security
- Skipfish Website Vulnerability Scanner
- Critical Microsoft Visual Studio Security Pa...
- US Faces Tech Security Expert Deficit
- LAN Reconnaissance
- An Epilogue to Cryptography
- A Sequel to Cryptography
- An Introduction to Cryptography
- Security Overview
- Network Security Assessment
- Firewalls
- What’s behind the curtain? Part II

Developer Shed Affiliates


Dev Shed Tutorial Topics: