20097 total geeks with 3178 solutions
Recent challengers:
 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
Domuk
No, not an issue with the PHP - I was responding to "AJAX not being cross site is annoying"
MaxMouse
Really? i thought that would only be important if the user had some kind of control over where the XML came from, if you hard code it (As in a PHP file) wouldn't that eliminate XSS attacks?
Domuk
Yes, but very, very necessary. AJAX requests run in the context of the browser, there'd be no security if it was cross-domain .
MaxMouse
AJAX not being cross site is annoying, all other scripts can be used in that way, having to resort to PHP to patch it is a shame.
SAJChurchey
thx MaxMouse

Donate
Donate and help us fund new challenges
Donate!
Due Date: Nov 30
November Goal: $40.00
Gross: $0.00
Net Balance: $0.00
Left to go: $40.00
Contributors


News Feeds
The Register
SQL Server 2008 -
from
semi-relational to
sublime
Blogger outs
back-end Google
tech
FDA takes aim at
illegal net
pharmacies
Oman cuffs 212 for
selling VoIP calls
IBM chase HP (and
Sun) with tiny mem
prices
Hackers free Snow
Leopard from
Jobsian cage
MySpace makes peace
with Indies
Nvidia previews
next-gen Fermi GPUs
Potty-mouths
charged for Comcast
hijack
Microsoft
Silverlight - now
with hidden Windows
bias
Slashdot
iPhone Game Piracy
"the Rule Rather
Than the Exception"
New Microsoft
Silverlight
Features Have
Windows Bias
How Heavy Is the
Internet?
Anti-Smoking
Vaccine Is Nearing
the Market
iPhone Owners
Demand To See Apple
Source Code
Proton Beams Sent
Around the LHC
Microsoft"s Lack of
Nightly Builds For
IE
Some Claim Android
App Store Worse
Than iPhone"s
Climatic Research
Unit Hacked, Files
Leaked
Aging Nuclear
Stockpile Good For
Decades To Come
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 129475 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
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.

Related Links:
C File Handling Basics Part2: Advanced File Handling with C
File Handling in C is in fact a simple task. But this does not mean youre limited to do simple things...
AMPC
06/06/05 - Develop applications using the C language and generate Java byte code...
Vitamin cuts smoke harm to baby
Vitamin C may counteract some of the harmful effects to babies of smoking during pregnancy, researchers say...
Free Software on a Cheap Computer
Shell writes Is this the solution to free software on a cheap computer? NetBSD and Yellow Dog Linux have both begun to support the Mac Mini. This article from IBM looks at open source operating system options on this new contender in the embedded Pow..
18th International Obfuscated C Code Contest Opens
chongo writes The 18th International Obfuscated C Code Contest, the Internets longest running contest, is now open. The goals, rules, and guidelines are available. Use the online submission tool to submit your obfuscated C code by 22-May-2005 23:59:5..
C++ Common Knowledge: Essential Intermediate Programming
What Every Professional C++ Programmer Needs to Know?Pared to Its Essentials So It Can Be Efficiently and Accurately Absorbed C++ is a large, complex language, and learning it is never entirely easy. But some concepts and techniques must be thoroughl..
Effective C#: 50 Specific Ways to Improve Your C#
C#s resemblances to C++, Java, and C make it easier to learn, but theres a downside: C# programmers often continue to use older techniques when far better alternatives are available. In Effective C#, respected .NET expert Bill Wagner identifies fifty..
Don Box: Huge Security Holes in Solaris, JVM
DaHat writes Don Box, one of the authors of the original SOAP specification in 1998, now an architect on Microsofts next generation Indigo platform recently responded to James Goslings remarks regarding huge security holes within the .NET Common Lang..
Gosling: Huge security hole in .Net
James Gosling says Microsofts use of C and C++ in its .Net platform has left a security hole large enough to drive many, many trucks through...
Gosling Claims Huge Security Hole in .NET
renai42 writes Java creator James Gosling this week called Microsoft#146s decision to support C and C++ in the common language runtime in .NET one of the biggest and most offensive mistakes that they could have made. Gosling further commented that by..
[român?] Limbajul de programare C
[?????] Introduction To Basic C++ Programming
The C Programming Language


     
Your Ad Here
 
Copyright Open Source Institute, 2006