Hi,
I would like to get the code for encryption & decryption of a file
Thanks
Hi,
I would like to get the code for encryption & decryption of a file
Thanks
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]
>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
with an encryption function of your own that works better:Code:fputc ( ~ch, out );
Code:fputc ( cipher ( ch ), out );-PreludeCode:/* ** 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; }
My best code is written with the delete key.
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:
The code has been simplified to make it easily readable, so you get the basic idea.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; }
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.