## 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:

A0=A
AA=0

where  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

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278  /* 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 .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 #include #include #include 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=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  :) .

MD5=D17655A5B5B009C07823EB3CA9ED29F1
SHA-1=67FC197F9A931534881405CCC4A388D1F9DF4B54
SHA256=8BFC549113CA5DD939029D656E45011B3C15147227ABE89E76770FF5F05800FB