1. Home
  2. Computing & Technology
  3. C / C++ / C#

C Programming Tutorial - Low Level Operations

By , About.com Guide

7 of 10

An example using Simple Xor Encryption

Simple Encryption

Example 4 uses this technique in a simple example. The function hideandshow(char *,char *) processes the text in one buffer into another using 8 values for the xor. I picked 8 values from the output of example 3 with 5 or more bits set. With a higher number of bits set there is a greater overall transformation. Using only 8 values though means the range of transformations is lower.

As this is a light weight scheme, you should realize that it is not suitable for anything stronger than hiding text. Every developer thinks that their encryption scheme is uncrackable but that protection usually relies on hiding how the data was encrypted, this is known as "security through obscurity" and is not regarded every highly.

The strongest encryption schemes have their algorithms published and it is the strength of those algorithms that protects the data. If you write your own scheme, it is quite difficult to prevent your code being reverse engineered and if the prize is high enough, it will be.

The 8 values are indexed by the variable eindex which is repeatedly incremented but constrained within the range 0..7 by the line

eindex % = 8;
This uses the built in C modulus operator. This line is the same as
if (eindex>=8)
   eindex=0;
As can be seen from the output, the text displayed by the last printf is identical to the text supplied to the first call of hideandshow() in the output listing below.
The hidden text is
89
1a
e3
15
c8
93
cc
d0
b3
13
eb
57
The unhidden text is Hello world.
Download Example 4.

On the next page All about Unions.

Explore C / C++ / C#
About.com Special Features

The Best Web Trends of the Decade

A look back at the best innovations, ideas and technologies over the last 10 years, More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. C / C++ / C#
  4. C
  5. C Tutorials
  6. An example using Simple Xor Encryption

©2010 About.com, a part of The New York Times Company.

All rights reserved.