Thread: Encryption/Decryption Program

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    13

    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;
    }
    Last edited by adohertyd; 12-21-2011 at 11:54 AM. Reason: inserted code

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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;

    Using a password
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    13
    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?

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    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...
    ¾à à  NTZL L^Hftbj D^J JLHftbj LrD\bZLœ – à à  L^Hftbj D^J JLHjtbj lhT^P HRDfÂ Ø Â XLtÂ Ö Â æ  œ – È T^HZlJL ú hjJT`Þ Rþ œ – È T^HZlJL ú hjJZTFÞ Rþ œ – œ – T^j \DT^Â Ò T^j DfPHÚ Â HRDfÂ Ö DfPn8<Ô œ –   x T^j Hftbjø   à à  L^HftbjT`^ XLtœ –     T^j NnDZø    à à  NTZL nDZlLhœ –      Â Ö T^NTZLÚ Â Ö `ljNTZLø œ –   œ –     TNÒ DfPH ú  ê Ô œ –       x bfT^jNÒ Æ TZL L^Hftbj`fà JLHjtbj`fÞ Þ Þ :^Æ Ô ø œ –         bfT^jNÒ Æ ^Hftbj `f JLHftbj D NTZL FDhLJ FDhLJ `^ D hLHfLj \LjR`J:^:^Æ Ô ø œ –         bfT^jNÒ Æ ,hDPL ö  :^Æ Ô ø œ –         bfT^jNÒ Æ Â Â Â Â Â Hftbj ú XLtþ  ú h`lfHLNTZLþ  ú `ljbljNTZLþ :^:^Æ Ô ø œ –         bfT^jNÒ Æ Lt ö :^Æ Ô ø œ –         bfT^jNÒ Æ Â Â Â *` L^Hftbj L^jLf D hT^PZL HRDfDHjLf Dh t`lf L^HftbjT`^ XLt:^Æ Ô ø œ –         bfT^jNÒ Æ Â Â Â *` JLHftbj L^jLfÂ Ö Â Dh jRL XLt:^:^Æ Ô ø œ –         bfT^jNÒ Æ rD\bZLh ö :^Æ Ô ø œ –         bfT^jNÒ Æ Â Â Â Â Â Hftbj r \tp`fXÞ jrj hDNLp`fXÞ jrj:^Æ Ô ø œ –         bfT^jNÒ Æ Â Â Â Â Â HftbjÂ Ö Â hDNLp`fXÞ jrj R`\Lp`fXÞ jrj:^:^Æ Ô ø œ –         LrTjÒ æ ì ì Ô ø  |œ – œ –     à à  `bL^ jRL NTZLhœ –     T^NTZL ü  N`bL^Ò DfPn8æ <Ú Æ fÆ Ô ø œ –     TNÂ Ò Ä Â T^NTZLÔ œ –       x bfT^jNÒ Æ ,^DFZL j` `bL^ T^blj NTZL:^:^Æ Ô ø œ –         LrTjÒ æ ì ê Ô ø  |œ – œ –     `ljNTZL ü  N`bL^Ò DfPn8è <Ú Æ pÆ Ô ø œ –     TNÂ Ò Ä Â `ljNTZLÔ œ –       x bfT^jNÒ Æ ,^DFZL j` `bL^ `ljblj NTZL:^:^Æ Ô ø œ –         LrTjÒ æ ì è Ô ø  |œ – œ –     à à  DHjTnL L^Hftbj`f     œ –     TNÂ Ò Ò DfPn8ä <8â < ü ü Â Ð Ö Ð Ô Ô œ –       x bfT^jNÒ Æ
    LHftbjT^PÞ Þ Þ :^:^Æ Ô ø œ –         à à  NTfhj nDZlL T^ jRL NTZL Th jRL XLtœ –         TNÂ Ò Â NhHD^NÒ T^NTZLÚ Æ Ì JÆ Ú Î HftbjÔ Â þ  â Ô œ –           pRTZLÒ NhHD^NÒ T^NTZLÚ Æ Ì JÆ Ú Î NnDZÔ Â þ  â Ô œ –             NbljHÒ Ò NnDZà æ Ô Â Ü Â HftbjÚ `ljNTZLÔ ø   |œ –     LZhLœ –       x bfT^jNÒ Æ ^HftbjT^PÞ Þ Þ :^:^Æ Ô ø œ –         à à  hLj XLt Dh NTfhj nDZlL T^ NTZL|    œ –         NbfT^jNÒ `ljNTZLÚ Æ Ì JÂ Æ Ú DfPn8ä <8â <Ô ø œ –         pRTZLÒ Â Ò NnDZ ü  NPLjHÒ T^NTZLÔ Ô Â þ  â Â Ô œ –           NbfT^jNÒ `ljNTZLÚ Æ Ì JÂ Æ Ú Â Ò NnDZÂ Ø Â DfPn8ä <8â <Ô Â Ö Â æ Ô ø  |œ –         œ –     NHZ`hLÒ T^NTZLÔ ø œ –     NHZ`hLÒ `ljNTZLÔ ø œ – œ –     bfT^jNÒ Æ
    `^LÞ Þ Þ :^:^:^Æ Ô ø     œ –     fLjlf^ â ø  |œ – œ – œ –

  6. #6
    Registered User
    Join Date
    Dec 2011
    Posts
    13
    That's perfect Tater thank you very much

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by adohertyd View Post
    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.
    Last edited by CommonTater; 12-21-2011 at 02:02 PM.

  8. #8
    Registered User
    Join Date
    Dec 2011
    Posts
    13
    Quote Originally Posted by CommonTater View Post
    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

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by adohertyd View Post
    No I'm not submitting this. I just wanted to see something like that in operation is all thanks very much
    You're welcome.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. encryption / decryption
    By ghatoz in forum C++ Programming
    Replies: 2
    Last Post: 11-18-2010, 06:45 AM
  2. encryption / decryption program
    By epidemic in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2008, 06:40 AM
  3. Encryption/Decryption Help
    By coolage in forum C++ Programming
    Replies: 9
    Last Post: 04-25-2008, 01:53 AM
  4. Encryption and Decryption Program in C++ using a class
    By goron350 in forum C++ Programming
    Replies: 7
    Last Post: 06-05-2005, 09:29 PM
  5. Ask about Encryption and Decryption
    By ooosawaddee3 in forum C Programming
    Replies: 3
    Last Post: 07-19-2002, 12:55 AM

Tags for this Thread