19 October 2013

XOR encryption for alphabets.

What is XOR encryption ?
Mathematically, XOR encryption/cipher is additive cipher, an encryption algorithm that operates according to following principles:

A\oplus0=A
A\oplusA=0

where \oplus is the xor operator just like AND(*) and OR(+) operator
To decrypt the cipher we just need to  XOR the cipher with the key to regain the original text .
The XOR operator is extremely common component in complex encryption Algorithms.
Such a encryption can easily be broken by using a constant repeating key and  using frequency analysis .
 But we change the key after each encryption breaking such encryption is computationally very hard
 such a cipher is called a stream cipher in which every next bit is encrypted using a different pseudo-random key , such a kind of encryption was used by Germans in theirs Lorentz cipher  .

 By using a truly random* stream of key the cipher is theoretically unbreakable hence unusable
In this program (written in c++) which you can compile and build on your machine uses a 3 bit key encryption it is not very safe but more than enough for a normal usage , it encrypts every bit in the message with the entered key it can work with spaces , full stops and next lines i have not included numbers so presently digits cant be encoded but that's just some more coding work which i suggest you should do.
I Will present a better version of this code when i steal some more time from my classes.
SEE INSTRUCTIONS TO USE BELOW

Download  the installer/build of this script from   here  https://docs.google.com/file/d/0B59INvcwdrAqU1o3b1VxT2ZrNEU/edit?usp=sharing 

/* please don't brag about ugly looking code i was in a terrible hurry 
while writing this code 
XOR ENCRYPTOR  : http://programmingconsole.blogspot.com/
for the code to work make sure to write the correct address of your .txt file 
with extension in the name preferably you should copy your file to the 
location you run this build from then you just have to write <filename>.txt 
then write any 3 digit code to secure the file with when you reopen the file the 
contents will be replaced with random looking numbers to decrypt run the same build again
build and write file name and password to decrypt  
   */

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<fstream>

using namespace std;

class key1{
    private:
    int a;
    int b;
    int c;
    public:
    void set(int new_a,int new_b);
    int get_a();
    int get_b();
    int get_Line();
    int current_Line_Key();
    int get_ending();
};


int power(int x,int y)
{
        if(y==0)
        return 1;
        return x*power(x,y-1);
}

int tobinary(int p)
{
    int out=0,i=0;
    while(p!=0)
    {
        out=out+((int)power(10,i++))*(p%2);
        p=p/2;
    }

    return out;
}

int tointeger(int inp)
{
    int ans=0,i=0;
    while(inp!=0)
    {
        ans=ans+((inp%10)*power(2,i++));
        inp=inp/10;
    }
    return ans;
}

int xorval(int a, int b)
{

      int tp1,tp2,i=0,ans=0;
      while((a!=0)||(b!=0))
      {
          tp1=a%10;tp2=b%10;
          if(((tp1==1)&&(tp2==1))||((tp1==0)&&(tp2==0)))
          {
              i++;
          }
          else{
          ans=ans+power(10,i);
          i++;
          }
          a=a/10;
          b=b/10;
      }
      return ans;
}

int binconv_charToBin(char x)
{
    return(tobinary((int)x-(int)'A'+1));

}

char binToChar(int x)
{
    return((char)tointeger(x));
}
int limit(int x,int a,int b)
{
    if((x>=a)&&(x<=b))
    return x;

}

int XOR_ENCRYPT(char x,int scramble)
{

    int bin1=binconv_charToBin(x);
    int bin2=tobinary((scramble));
    return(tointeger(xorval(bin1,bin2)));


}
char XOR_DECRYPT(int x,int scramble)
{
    int bin1=tobinary(x);
    int bin2=tobinary((scramble));
    return(binToChar(tobinary((int)'A')+(xorval(bin1,bin2))-1)); //by :namit sinha 
}
int main(int argc,char* argv[])
{

    key1 key;
    string filename;
    int keyinp=878;
    if(argc>1){
    filename=argv[1];
    }
    else{
    printf("enter the filename: ");
    cin>>filename;
    }
    printf("\n enter the password > 3 numerals,");
    printf("To decrypt enter the password you encrypted the file with:\n");
    scanf("%d",&keyinp);
    int a1=(keyinp%10);
    keyinp=keyinp/10;
    int b1=(keyinp%10);
    key.set(a1,b1);
    int scramble;
    char inpstr[100000];

    string line;
    ifstream targetfile(filename);
    if(targetfile.is_open())
    {
        while(getline(targetfile,line))
        {
                int xs=line.size();
    char hg[100];
    for(int a=0;a<=xs;a++){
        hg[a]=line[a];
    }
    strcat(inpstr,hg);
    strcat(inpstr,"%");
        }
    }
    targetfile.close();
if(inpstr[0]!='$')
{

    int leng=strlen(inpstr);
    char outstr[leng];
    int messageinp[leng];
    int cntr=0;
    ofstream targetfile1(filename);
    targetfile1<<"$";
    for(int i=0;i<leng;i++)
    {
        scramble=key.get_a();
        if((inpstr[i]!='%')&&(inpstr[i]!='.')){
        {

        messageinp[i]=XOR_ENCRYPT(inpstr[i],scramble);
        cout<<messageinp[i]<<" ";
        targetfile1<<messageinp[i];
        }
        }
        else{
            if(inpstr[i]=='.')
            {
              targetfile1<<key.get_ending();

            }

            else{
            messageinp[i]=key.get_Line();
            cout<<messageinp[i]<<" ";
            targetfile1<<messageinp[i];
            }
            }

            targetfile1<<" ";
            }
    targetfile1.close();
    cout<<"\n";
}
else
{


    ofstream targetfile1(filename);
    key.set(a1,b1);
    int l=1,tmp=0,cnt=2,leng=strlen(inpstr);
    while(cnt<leng)
    {
        cnt++;
        if((inpstr[l]=='%')||(inpstr[l]==' '))
        {
        scramble=(key.get_a());
        if(tmp!=key.current_Line_Key()){
         if(tmp==(key.get_ending()))
         {
             targetfile1<<".";
         }
         else
        {

        char s=XOR_DECRYPT(tmp,scramble);
        s!='@'?cout<<s:cout<<" ";
        s!='@'?targetfile1<<s:targetfile1<<" ";
        }

        }
        else{

        int x1=key.get_Line();
        cout<<"\n";
        targetfile1<<"\n";
        }
        tmp=0;
        }

        else{
           int df=(int)inpstr[l] -(int)'0';
           tmp=tmp*10+df;
        }
        l++;

    }


    targetfile1.close();

}
}



void key1::set(int new_a,int new_b)
{
    a=new_a;
    b=new_b;
}
int key1::get_Line()
{

    int x;
    x=a*b-(b+a+a/b);
    return (x%100);
}
int key1::get_ending()
{
    int x7;
    x7=a*b-(b+a+a/b)+5;
    return (x7%100);
}

int key1::get_a()
{
    int tmp=a+9;
    a=b;
    b=tmp;
    return a%1000;
}
int key1::current_Line_Key(){
     return((a*b-(a+b+a/b))%100);
}
int key1::get_b()
{
    int tmp;
    a>=b?tmp=a-b+9:tmp=b-a+9;
    b=c;
    c=tmp;
    return b%1000;
}
//by :namit sinha 

for the code to work make sure to write the correct address  of your .txt file with extension in the 'file name' preferably you  should copy your file to the location you run this build from 
then you just have to write <filename>.txt enter then write any 3 digit code to secure the file with when you reopen the file the contents will be replaced with random looking numbers to  decrypt run the same build again build and write file name and  password to decrypyt.


you may download the build for windows 7 and higher machine from here**  or build   from source code 



for a better explanation about XOR encryption you may want to see http://cryptocellar.web.cern.ch/cryptocellar/tutte.pdf   professor Bill Tutte's (whom i consider to be a co-inventor of computer without whom britishers may never have broken the nazi's  Lorenz cipher which britishers called 'the fish'   )  lecture on encryption

*truly-random here means that we can-not use hardware random number generators , because without knowing the original pseudo-random stream decryption would be impossible . And mind you  there is no such thing as truly random we just don't know how certain things work yet  :) .

**If you are that kind of person who is suspicious about this .exe download file  you can cross-check the integrity by SHA-256, MD-5 HASH  of the file you downloaded
MD5=D17655A5B5B009C07823EB3CA9ED29F1
SHA-1=67FC197F9A931534881405CCC4A388D1F9DF4B54
SHA256=8BFC549113CA5DD939029D656E45011B3C15147227ABE89E76770FF5F05800FB


No comments:

Post a Comment

What do you think ?