22858 total geeks with 3297 solutions
Recent challengers:
best bread maker
 Welcome, you are an anonymous user! [register] [login] Get a yourname@osix.net email address 

Articles

GEEK

User's box
Username:
Password:

Forgot password?
New account

Shoutbox
sefo
anilg, new comments are deleted automaticall y because of some abuse recently
anilg
this is plain wierd. I submitted comments twice to article 950, and they dont seem to be there. Something wrong with the comment code?
CodeX
shout-boxes in general are old + the staff thing happened to everyone after an issue 2 months ago
anilg
/me is no longer staff :(
anilg
Also, osix's shoutbox predated twitter. Heh.

Donate
Donate and help us fund new challenges
Donate!
Due Date: Jul 31
July Goal: $40.00
Gross: $0.00
Net Balance: $0.00
Left to go: $40.00
Contributors


News Feeds
The Register
T-Mobile UK pumps
out the iPhone 4
Polaroid 300
instant print
camera
NatWest dumps O2
Money
YouTube ups video
time limit
Alleged expenses
fiddlers to face
justice
Nude trampolinist
bounces free from
court
Nexus One phone
rockets to 28,000ft
UK.gov drops £6m on
Google
Fake Firefox update
used to sling
scareware
Happy Sysadmin Day!
Slashdot
British ISPs Favour
Well-Connected
Customers
"Bizarre"
Nanobubbles Found
In Strained
Graphene
1-in-1,000 Chance
of Asteroid Impact
In
...
2182?
2 Chinese ISPs
Serve 20% of World
Broadband Users
World"s Fastest
Hybrid OK"d For
Production
Sometimes It"s OK
To Steal My Games
Thermoelectrics
Could Let You Feel
the Heat In Games
KDE SC 4.7 May Use
OpenGL 3 For
Compositing
Perl 6, Early, With
Rakudo Star
Internal Costs Per
Gigabyte —
What Do You Pay?
Article viewer

Simple encryption using XOR in C/C++



Written by:dimport
Published by:Nightscript
Published on:2003-06-21 07:19:46
Topic:C
Search OSI about C.More articles by dimport.
 viewed 156015 times send this article printer friendly

Digg this!
    Rate this article :
Following in the footsteps of our other encryption articles in Perl, C# and Java. Here is an example of how to perform some simple encryption in C/C++ by using the XOR method, also shown is an example of extending the XOR method by using a BLOCK.



Think your a GEEK? Prove it on the Geek Challenges





! XOR

xor declared as a '^' in C/C++/C# and works like this:


If both on the same position is 1 set it to 0, if one is 1 return 1 else return 0.

Here is an example:


01001101 (a)

10011100 (b)

------------

11010001 (c)


The best thing with xor is that is you xor c with b you get a, so you can easy decrypt it by applying xor with the key again. Example:


10011100 (b)

11010001 (c)

------------

01001101 (a)


! BLOCK


a way to make it harder to decypher the encrypted data is to block the data and encrypt it. This is shown in the following example:


BLOCK_SIZE = 3


thi|s i|s a| te|st

k | e| y| k|e


You split the message into block of the size 3(just as an example) and encrypt that block only with that key.


an example in C/C++:

for(int i = 0, y = 0; i <= strlen(data); ) {
  for(int o = 0; o <= BLOCK_SIZE; o++) {
    if(data[i] != '') {
      data[i] ^= key[y]; }
    i++; }
  y++;
  if(key[y] == '') {
    y = 0; }
}




This article was originally written by shab

Did you like this article? There are hundreds more.

Comments:
loureiro97
2004-12-30 21:41:20
How did this simple article get 8626 views.
Not that it's bad or anything, just that there are better articles... :?
lonetech
2005-05-04 23:25:08
Simple - you have to view it to tell what it contains. In this case, not terribly much. I don't see the point of skipping spaces in the algorithm; you're likely to run into a fencepost error anyway, the moment someone uses the same character as key and data.
BhanuTeja
2005-05-25 16:15:18
Anyone can easily break this type of crypto-system.
Anonymous
2006-01-23 19:44:28
I think that's why its called a "Simple" encryption system. It is used to illustrate that crypto-systems need bijective functions (its inverse is also a function). This is important because once you apply the function you need to be able to take the encrypted value back.

-Al
anilg
2006-01-24 08:08:25
Yeah.. this is sort of a very young brother of OTP.. which is the only proven 100% secure cryptosystem
Anonymous
2006-01-24 22:47:06
otp unless implemented incorrectly is 100% secure
aidan
2006-01-25 09:27:36
There are inherent problems with the initial key distribution, and of course you also have to keep the pad secure. But it can be implemented correctly.
anilg
2006-01-26 05:41:16
The drawback is he key is as long as the message itself. So you'll have to first send across the "pad" to the recipent.

The +ve is that since the key is as long as the message itself.. the ciphertext, for ex:

a shd ajsdh jahdjakshdasjdh

could decrypt to:

"Hey we need more artillary"

or

"Whoa! send some girls over"
Anonymous
2006-04-16 11:52:15
xor in vc
xpi0t0s
2006-04-19 11:02:29
um, what about it? ^ as the xor operator works fine in Visual C++; I've used it myself on numerous occasions.
Anonymous
2006-07-18 19:51:15
I wrote a commandline based XOR encryption program that takes in as the argument the key.
The program works fine, however if there are too many characters in the file to be deciphered, the program will not properly decipher the file, instead I get half of the file deciphered, and the rest is just a bunch of useless ciphertext junk. What would cause the XOR encryption to behave like that, just because there are too many characters to decipher.

note: I wrote the program so that if the key is longer than the string to decipher, the program resets to the beginning of the key.

EX:
KEY: ABC
STRING: hello World!!!

h ^ A
e ^ B
l ^ C
l ^ A
o ^ B
' ' ^ C
etc . . .

my syntax diagram:

Usage: aenc Filename key
anilg
2006-07-19 05:10:29
The source code would help find the problem. Use the forum to post it.
Anonymous
2006-07-23 13:58:57
How can I become a member of this websites forum?
Anonymous
2006-07-23 17:45:50
go there:

http://www.osix.net/signup.php
Anonymous
2006-12-28 08:19:43
BTW, it's 100% secure only in a case when length of key is equal to message length. It seems to be so...
Anonymous
2007-01-10 19:33:11
Let me answer why this article is so popular, there is a site called http://www.ouverture-facile.com/ full of riddles to solve, a there is one that uses XOR encryption, it was recently digged, so many people are finding this article in the first page when they google XOR encryption
Anonymous
2007-01-18 03:38:10
encryption and decryption in c++
Anonymous
2007-03-08 09:37:30
A very simple encryption method, easily bypassed, and yet it does provide beginners a simple example of how it's done.
Anonymous
2007-03-25 19:02:19
why be so negative? this guy has taken the time to post his code. newbies may find it useful.
Anonymous
2007-04-12 10:40:20
Hey I found this useful! thanks for taking the time to show us simple and easy to understand code. Programming is like lifting weights, if you don't do it a lot you can't just go and lift 300lb. This example was an awesome starter! Please do more of these in the future.
Anonymous
2007-07-04 09:48:52
If you have a totally random key the same length as the plain-text, securely distributed beforehand to the receiving party, this simple XOR scheme is perfectly secure. This is called a one-time pad. Mathematics can't break it, so the NSA couldn't break it either.
Anonymous
2007-11-23 02:12:06
Nice routines and very well written keep it up, thank you for sharing SEO Expert it's easy to follow and understandable.
Anonymous
2009-04-11 13:24:17
NEVER USE STR.LENGTH IN ANY LOOP !!!!!!!!!!!!!!!!!
Anonymous
2009-04-28 12:41:34
can a simple encryption algorithm not possible using non-linear feedback shift register concept?...cause that would increase the security to a great level, as well as would be simple also..
Anonymous
2009-05-02 00:41:13
> A very simple encryption method,
> easily bypassed

Since its so easy... explain it in a few short sentences.
Anonymous
2009-11-12 13:11:20
Nothing wrong with str.length if you've got a compiler that doesn't suck
Anonymous
2009-12-11 12:30:16
can u explain how to encrypt an sqlite db file in c++ (plz mail to kiran.ramanadham@gmail.com)
Anonymously add a comment: (or register here)
(registration is really fast and we send you no spam)
BB Code is enabled.
Captcha Number:


Blogs: (People who have posted blogs on this subject..)
amisauv
Creating a Lexical Analyzer in C on Tue 9th Dec 11am
#include<stdio.h> #include<string.h> #include<conio.h> #include<ctype.h> /*************************************** ************************* Functions prototype. **************************************** *************************/ void Open_File(
amisauv
Controling digital circuit through computer on Tue 9th Dec 10am
this code access the lpt port.here only 4 of the total 8 pins are used but can be modified for full 8 pins.it has a complete GUI with mouse & keyboard interactive control panel.works well in win98, but not in winxp. #include<stdio.h> #include<conio.
amisauv
/* Computerised Electrical Equipment Control */ /* PC BASED DEVICE CONTROLLER * on Tue 9th Dec 10am
#include<stdio.h> #include<conio.h> #include<dos.h> void main() { void tone(void); int p=0x0378; char ex={"Created By Mrc"}; int j; char ex1={"For Further Details & Improvements"}; int k; char ex2={"Contact : E-mail : anbudan
amisauv
Calendar Program on Tue 9th Dec 10am
This program prints Weekdays of specified date. It even prints calendar of a given year too. /*Ccalendar library*/ #include<stdio.h> #include<string.h> #include<conio.h> int getNumberOfDays(int month,int year) { switch(month) { case
amisauv
Calculator: on Tue 9th Dec 10am
#include"graphics.h" #include"dos.h" #include"stdio.h" #include"math.h" union REGS i,o; char text={ "7","8","9","*","4","5","6","/","1","2", "3","+","0","00",".","-","M","M+", "M-","+/-","MR","MC","x^2","sr","OFF","A C","CE","="}; int s=0,k=0,pass
amisauv
INFECTED CODES WRITTEN IN C\C++ on Tue 9th Dec 10am
This is a simple code that changes system time and date. It is written using c/c++ but can be easily converted to java. #include "stdio.h" #include "process.h" #include "dos.h" int main(void) { struct date new_date; struct date old_date; s
amisauv
A C programme which can print the file name it is kept in on Tue 9th Dec 9am
#include<stdio.h> main(){ printf(”the source file name is %s\n”,__FILE__); } actually __FILE__ is a macro which stands for the file name the programme is kept in and the compiler does the rest .. for you ..
amisauv
BOOTSECTOR EDITOR: on Tue 9th Dec 9am
Code : /*program to save the partion table of your hard disk for future use. it will save your partition table in a file partition.dat */ #include<stdio.h> #include<bios.h> #include<conio.h> #include<stdlib.h> #include<ctype.h> void main () {
amisauv
BLINKING STAR : on Tue 9th Dec 9am
#include<conio.h> #include<graphics.h> #include<stdlib.h> #include<dos.h> void main() { int gdriver=DETECT,gmode; int i,x,y; initgraph(&gdriver,&gmode,"e: cgi"); while(!kbhit()) { x=random(640); y=random(480); setcolor
amisauv
// To print semicolons using C programming without using semicolons any where i on Tue 9th Dec 9am
// To print semicolons using C programming without using semicolons any where in the C code in program. // #include<stdio.h> #include<conio.h> void main() { char a; a=59; if(printf("%c",a)){} getch();

Test Yourself: (why not try testing your skill on this subject? Clicking the link will start the test.)
BSD sockets API by skrye

This is a test of your knowledge of the BSD socket interface
C Programming by keoki

This test is aimed at a C programmer that is at an intermediate level.


     
Your Ad Here
 
Copyright Open Source Institute, 2006