Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to write a simple Encryption program?

Status
Not open for further replies.

sujincls

Programmer
Jun 14, 2002
6
IN
Could be helpful if anyone tell me how to write a simple encryption program in C(unix/windows).

SJ.
 
How simple?
______________________________________________________________________
Perfection in engineering does not happen when there is nothing more to add.
Rather it happens when there is nothing more to take away.
 
Hi:

This is about as simple as encryption gets. I've implemented Bruce Schneier's xor algorithm from his book Applied Crytography.

Schneier rates this algorithm as an "embarrassment" in the amount of time a trained cryptographer needs to break this algorithm.

Regards,

Ed

#include <stdio.h>

int main(int argc, char *argv[])
{
char *key=&quot;theduke&quot;;
char *password=&quot;whopper&quot;;
char buffer[80];
int len;

/* encode the password*/
len=strlen(password);
strcpy(buffer, password);

xor(buffer, key, len);
printf(&quot;encoded password: %s \n&quot;, buffer);

/* decode the password*/
xor(buffer, key, len);

/* it works if both are the same */
printf(&quot;%s|%s|\n&quot;, password, buffer);

exit(0);
}

/*
* encode/decode 'str' with 'key' using the XOR
* algorithm. This algorithm is from Applied Cryptography
# by Bruce Schneier.
*/
int xor(str, key, len)
char *str;
char *key;
int len;
{
char *keys;
int str_len;

keys=key;
str_len=len;
while(len)
{
if(!*keys)
keys=key;
*(str++) ^= *(keys++);
len--;
}
str[str_len+1]='\0';
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top