•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 402,506 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,841 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
This snippet is an example of using XOR encryption to encrypt/decrypt a file. It uses command-line input parameters as follows: Performing the reverse on the encrypted file "file.bin" will restore the original file contents into another output file "file.txt" using the same "key". If the code shown here was in "main.c", you should find that the second output "file.txt" matches "main.c".
argv[1] - name of input file to encrypt/decryptFor example, an input file called "main.c" is used to create an output file "file.bin" with the encryption key "key" by doing this:
argv[2] - name of output file
argv[3] - crypt key
•
•
•
•
H:\Daniweb>xorcrypt main.c file.bin key
•
•
•
•
H:\Daniweb>xorcrypt file.bin file.txt key
Last edited : Aug 4th, 2006.
#include <stdio.h> int main(int argc, char *const *argv) { if ( argc == 4 ) { FILE *input = fopen(argv[1], "rb"); FILE *output = fopen(argv[2], "wb"); if ( input != NULL && output != NULL ) { unsigned char buffer[BUFSIZ]; size_t count, i, j = 0; do { count = fread(buffer, sizeof *buffer, sizeof buffer, input); for ( i = 0; i < count; ++i ) { buffer[i] ^= argv[3][j++]; if ( argv[3][j] == '\0' ) { j = 0; /* restart at the beginning of the key */ } } fwrite(buffer, sizeof *buffer, count, output); } while ( count == sizeof buffer ); fclose(input); fclose(output); } } return 0; }
Comments (Newest First)
vonqi | Newbie Poster | Nov 8th, 2006
•
•
•
•
Hi
i aM new here . Like to know where can i get xorcrypt <-- this program. Thanks
i aM new here . Like to know where can i get xorcrypt <-- this program. Thanks
Post Comment
•
•
•
•
DaniWeb Marketplace (Sponsored Links)