Cryptography

This is a discussion on Cryptography within the C Programming forums, part of the General Programming Boards category; Hi, I would like to get the code for encryption & decryption of a file Thanks...

  1. #1
    Vamsianitha
    Guest

    Lightbulb Cryptography

    Hi,

    I would like to get the code for encryption & decryption of a file

    Thanks

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    So?

    Are you writing your own and having trouble with it, or are you just wanting to use someone elses code?

    There are plenty of example sources on the web, try looking
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I would like to get the code for encryption & decryption of a file
    This is the basic idea, though I don't suggest using it since the encryption method is worse than useless. Replace
    Code:
    fputc ( ~ch, out );
    with an encryption function of your own that works better:
    Code:
    fputc ( cipher ( ch ), out );
    Code:
    /*
    ** Encryption using files, very simple (to break).
    */
    #include <stdio.h>
    #include <stdlib.h>
    
    int main ( void )
    {
      int ch;
      FILE *in;
      FILE *out;
    
      if ( ( in = fopen ( "input.txt", "r" ) ) == NULL ) {
        perror ( "Input file not opened" );
        exit ( EXIT_FAILURE );
      }
    
      if ( ( out = fopen ( "output.txt", "w" ) ) == NULL ) {
        perror ( "Output file not opened" );
        exit ( EXIT_FAILURE );
      }
    
      while ( ( ch = fgetc ( in ) ) != EOF )
        fputc ( ~ch, out );
    
      return EXIT_SUCCESS;
    }
    -Prelude
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    8

    Lightbulb XOR encryption

    You may want to look into XOR encryption, it is very simple, yet highly effective.
    The basic idea is:
    char ^ key_val = encrypted_char

    Coincidently, I wrote a simple program that implements this the other day:

    Code:
    int main(int argc, char *argv[])
    {
    FILE *fp;
    int ch; /* For reading chars from file */
    size_t  length; /* Length of key */
    size_t offset = 0;  /* Used for address arithmetic when  looping though key chars. */
    
    if( argc != 3 )
    {
        fprintf(stderr,"Usage: %s [file] [key]\n",argv[0]);
        return 1;
    }
    if( (fp = fopen(argv[1],"rt")) == NULL )
    {
        perror(argv[1]);
        return 2;
    }
    
    length = strlen(argv[2]);
    
    /* Read each char from the file until EOF (or error) */
    while( (ch = fgetc(fp)) != EOF )
    {
        /* put XOR encrypted char to stdout */
        putchar(ch^(*(argv[2]+offset)));
        fflush(stdout);
        /* continuously loop through the key char array */
        if( offset++  >= length )
            offset = 0;
    }
    
    fclose(fp);
    return 0;
    }
    The code has been simplified to make it easily readable, so you get the basic idea.
    It is a good idea to zip the file before you encrypt it, as it makes it even harder to brute force. As long as you use a decent key (Eg. What a Unix system would call a decent password), the encryption should suffice.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Cryptography
    By freddyvorhees in forum C++ Programming
    Replies: 12
    Last Post: 08-15-2008, 10:03 PM
  2. Replies: 16
    Last Post: 11-23-2007, 01:48 PM
  3. What does this mean for the world of cryptography?
    By hk_mp5kpdw in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 10-08-2002, 12:55 AM
  4. cryptography
    By tsarena in forum C++ Programming
    Replies: 2
    Last Post: 06-05-2002, 05:14 PM

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