# Encryption/Decryption Program

• 12-21-2011
Encryption/Decryption Program
I am trying to build an encryption/decryption program for fun. I know it won't be strong, I know people would crack it in seconds but I don't care. This is just to get to grips with it. I have code that works with the XOR encryption method. It needs a bit of work but its operating and that was my goal. What I want to do is design a very simple algorithm to replace XOR in my code. I assume that I should write a function and replace
Code:

`c^=*(key++);`
with my algorithm e.g.
Code:

`c = algo(c, *key++);`
Can someone tell me how I might write a function to say increment every letter by 5, i.e. a = (a+5), b = (b+5), etc?

I should know this but I'm blanking!

Code so far:

Code:

```#include <stdio.h> int main(int argc, char*argv[]) { FILE *fp1, *fp2; char* key; int c; key = argv[1];         if(*key != '\0')         {         fp1 = fopen(argv[2],"rb");                 if(fp1 != NULL)                 {                 fp2 = fopen(argv[3],"wb");                 if (fp2 != NULL)                 {                         while((c=getc(fp1))!=EOF)                         {                                 if(!*key)key = argv[1];                                 c ^=*(key++);                                         putc(c, fp2);                         }                 fclose(fp2);                 }         fclose(fp1);         }         } return 0; }```
• 12-21-2011
Download an ascii table from the net, showing the value in integers, of the various chars the computer uses.

What you want to do is called a Caesar Shift, since it was said that Julius Caesar used it. When any number + it's shift, is greater than 'z' (or 'Z'), then you need to subtract 'a' from it, and let it "wrap back around".

It's helpful if you use tolower() or toupper in the include file < ctype.h >, first, to make all the char's the same, before you do the shift. At least, while you're learning it.
• 12-21-2011
Salem
Almost all algorithms can be set to use xor.
The "strength" comes from how you derive the key.

Null encryption
char = char ^ 0;

Simple shift
char = char ^ constant;

char = char ^ *p++; if ( *p == '\0' ) p = password;

Use a RNG stream
char = char ^ rand();

Now if your "rand()" function is of cryptographic quality, you should be in pretty good shape.
• 12-21-2011
I don't want to use a preset encryption algorithm though, I would like to write my own basic one that stands alone. Even by adding 5 letters to the original letter then multiplying it's position by 2

i.e. I want to take each letter of the input file and add 5 to its ASCII value:

e.g a+5 = f (65+5)

When done, I want to do another loop that multiplies the position x 2

f x 2 = í(special I character) ( 70 x 2 is 140)

Therefore a becomes the 140th character in the ASCII table.

How would I write that into a funtion to replace the XOR in my original code?
• 12-21-2011
CommonTater
Here study this... it should give you some ideas...
Code:

```// file encrypt and decrypt example // encrypt and dectypt using char + key * 2 #include <stdio.h> #include <stdlib.h> int main (int argc, char *argv[])   { char  crypt;  // encryption key     short fval;  // file values     FILE  *infile, *outfile;       if(argc < 4)       { printf("File encryptor/dectyptor...\n");         printf("Encrypt or decrypt a file based based on a secret method\n\n");         printf("Usage : \n");         printf("    crypt <key> <sourcefile> <outputfile>\n\n");         printf("Key :\n");         printf("  To encrypt enter a single character as your encryption key\n");         printf("  To decrypt enter * as the key\n\n");         printf("Examples :\n");         printf("    crypt x mywork.txt safework.txt\n");         printf("    crypt * safework.txt homework.txt\n\n");         exit(255); }     // open the files     infile = fopen(argv[2],"rb");     if (! infile)       { printf("Unable to open input file\n\n");         exit(254); }     outfile = fopen(argv[3],"wb");     if (! outfile)       { printf("Unable to open output file\n\n");         exit(253); }     // active encryptor        if ((argv[1][0] == '*'))       { printf("Decrypting...\n\n");         // first value in the file is the key         if ( fread(&crypt, sizeof(char), 1, infile) )           { crypt = ~crypt;                            while( fread(&fval ,sizeof(short), 1, infile) )               fputc( (fval / 2) - crypt, outfile );  } }     else       { printf("Encrypting...\n\n");         // set key as first value in file}            crypt = ~argv[1][0];         fwrite(&crypt, sizeof(char), 1, outfile);         while( (fval = fgetc(infile)) > 0 )           { fval = (fval + argv[1][0]) * 2;           fwrite(&fval,sizeof(short),1,outfile); } }             fclose(infile);     fclose(outfile);     printf("Done...\n\n\n");        return 0; }```
and here it is encrypting it's own source code...
Quote:

¾à à Â NTZLÂ L^HftbjÂ D^JÂ JLHftbjÂ LrD\bZLœ – à à Â L^HftbjÂ D^JÂ JLHjtbjÂ lhT^PÂ HRDfÂ Ø Â XLtÂ Ö Â æ Â œ – È T^HZlJLÂ ú hjJT`Þ Rþ œ – È T^HZlJLÂ ú hjJZTFÞ Rþ œ – œ – T^jÂ \DT^Â Ò T^jÂ DfPHÚ Â HRDfÂ Ö DfPn8<Ô œ – Â Â xÂ T^jÂ Hftbjø Â Â à à Â L^HftbjT`^Â XLtœ – Â Â Â Â T^jÂ NnDZø Â Â Â à à Â NTZLÂ nDZlLhœ – Â Â Â Â  Â Ö T^NTZLÚ Â Ö `ljNTZLø œ – Â Â œ – Â Â Â Â TNÒ DfPHÂ ú Â ê Ô œ – Â Â Â Â Â Â xÂ bfT^jNÒ Æ TZLÂ L^Hftbj`fà JLHjtbj`fÞ Þ Þ :^Æ Ô ø œ – Â Â Â Â Â Â Â Â bfT^jNÒ Æ ^HftbjÂ `fÂ JLHftbjÂ DÂ NTZLÂ FDhLJÂ FDhLJÂ `^Â DÂ hLHfLjÂ \LjR`J:^:^Æ Ô ø œ – Â Â Â Â Â Â Â Â bfT^jNÒ Æ ,hDPLÂ ö Â :^Æ Ô ø œ – Â Â Â Â Â Â Â Â bfT^jNÒ Æ Â Â Â Â Â HftbjÂ ú XLtþ Â ú h`lfHLNTZLþ Â ú `ljbljNTZLþ :^:^Æ Ô ø œ – Â Â Â Â Â Â Â Â bfT^jNÒ Æ LtÂ ö :^Æ Ô ø œ – Â Â Â Â Â Â Â Â bfT^jNÒ Æ Â Â Â *`Â L^HftbjÂ L^jLfÂ DÂ hT^PZLÂ HRDfDHjLfÂ DhÂ t`lfÂ L^HftbjT`^Â XLt:^Æ Ô ø œ – Â Â Â Â Â Â Â Â bfT^jNÒ Æ Â Â Â *`Â JLHftbjÂ L^jLfÂ Ö Â DhÂ jRLÂ XLt:^:^Æ Ô ø œ – Â Â Â Â Â Â Â Â bfT^jNÒ Æ rD\bZLhÂ ö :^Æ Ô ø œ – Â Â Â Â Â Â Â Â bfT^jNÒ Æ Â Â Â Â Â HftbjÂ rÂ \tp`fXÞ jrjÂ hDNLp`fXÞ jrj:^Æ Ô ø œ – Â Â Â Â Â Â Â Â bfT^jNÒ Æ Â Â Â Â Â HftbjÂ Ö Â hDNLp`fXÞ jrjÂ R`\Lp`fXÞ jrj:^:^Æ Ô ø œ – Â Â Â Â Â Â Â Â LrTjÒ æ ì ì Ô ø Â |œ – œ – Â Â Â Â à à Â `bL^Â jRLÂ NTZLhœ – Â Â Â Â T^NTZLÂ ü Â N`bL^Ò DfPn8æ <Ú Æ fÆ Ô ø œ – Â Â Â Â TNÂ Ò Ä Â T^NTZLÔ œ – Â Â Â Â Â Â xÂ bfT^jNÒ Æ ,^DFZLÂ j`Â `bL^Â T^bljÂ NTZL:^:^Æ Ô ø œ – Â Â Â Â Â Â Â Â LrTjÒ æ ì ê Ô ø Â |œ – œ – Â Â Â Â `ljNTZLÂ ü Â N`bL^Ò DfPn8è <Ú Æ pÆ Ô ø œ – Â Â Â Â TNÂ Ò Ä Â `ljNTZLÔ œ – Â Â Â Â Â Â xÂ bfT^jNÒ Æ ,^DFZLÂ j`Â `bL^Â `ljbljÂ NTZL:^:^Æ Ô ø œ – Â Â Â Â Â Â Â Â LrTjÒ æ ì è Ô ø Â |œ – œ – Â Â Â Â à à Â DHjTnLÂ L^Hftbj`fÂ Â Â Â Â œ – Â Â Â Â TNÂ Ò Ò DfPn8ä <8â <Â ü ü Â Ð Ö Ð Ô Ô œ – Â Â Â Â Â Â xÂ bfT^jNÒ Æ
LHftbjT^PÞ Þ Þ :^:^Æ Ô ø œ – Â Â Â Â Â Â Â Â à à Â NTfhjÂ nDZlLÂ T^Â jRLÂ NTZLÂ ThÂ jRLÂ XLtœ – Â Â Â Â Â Â Â Â TNÂ Ò Â NhHD^NÒ T^NTZLÚ Æ Ì JÆ Ú Î HftbjÔ Â þ Â â Ô œ – Â Â Â Â Â Â Â Â Â Â pRTZLÒ NhHD^NÒ T^NTZLÚ Æ Ì JÆ Ú Î NnDZÔ Â þ Â â Ô œ – Â Â Â Â Â Â Â Â Â Â Â Â NbljHÒ Ò NnDZà æ Ô Â Ü Â HftbjÚ `ljNTZLÔ ø Â Â |œ – Â Â Â Â LZhLœ – Â Â Â Â Â Â xÂ bfT^jNÒ Æ ^HftbjT^PÞ Þ Þ :^:^Æ Ô ø œ – Â Â Â Â Â Â Â Â à à Â hLjÂ XLtÂ DhÂ NTfhjÂ nDZlLÂ T^Â NTZL|Â Â Â Â œ – Â Â Â Â Â Â Â Â NbfT^jNÒ `ljNTZLÚ Æ Ì JÂ Æ Ú DfPn8ä <8â <Ô ø œ – Â Â Â Â Â Â Â Â pRTZLÒ Â Ò NnDZÂ ü Â NPLjHÒ T^NTZLÔ Ô Â þ Â â Â Ô œ – Â Â Â Â Â Â Â Â Â Â NbfT^jNÒ `ljNTZLÚ Æ Ì JÂ Æ Ú Â Ò NnDZÂ Ø Â DfPn8ä <8â <Ô Â Ö Â æ Ô ø Â |œ – Â Â Â Â Â Â Â Â œ – Â Â Â Â NHZ`hLÒ T^NTZLÔ ø œ – Â Â Â Â NHZ`hLÒ `ljNTZLÔ ø œ – œ – Â Â Â Â bfT^jNÒ Æ
`^LÞ Þ Þ :^:^:^Æ Ô ø Â Â Â Â œ – Â Â Â Â fLjlf^Â â ø Â |œ – œ – œ –
• 12-21-2011
That's perfect Tater thank you very much
• 12-21-2011
CommonTater
Quote:

That's perfect Tater thank you very much

Now the "catch"... I wouldn't even think about turning that in as homework. You have to know your teachers check forums like this and will likely tag you for cheating... so treat it as the *example* that it is.
• 12-21-2011
Quote:

Originally Posted by CommonTater
Bow the "catch"... I wouldn't even think about turning that in as homework. You have to know your teachers check forums like this and will likely tag you for cheating... so treat it as the *example* that it is.

No I'm not submitting this. I just wanted to see something like that in operation is all thanks very much
• 12-21-2011
CommonTater
Quote: