Encryption/Decryption Program

This is a discussion on Encryption/Decryption Program within the C Programming forums, part of the General Programming Boards category; I am trying to build an encryption/decryption program for fun. I know it won't be strong, I know people would ...

  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 10: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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,439
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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 01: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, 05: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


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21