User Name Password Register
DaniWeb IT Discussion Community
All
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
Aug 4th, 2006
Views: 3,771
This snippet is an example of using XOR encryption to encrypt/decrypt a file. It uses command-line input parameters as follows:
argv[1] - name of input file to encrypt/decrypt
argv[2] - name of output file
argv[3] - crypt key
For example, an input file called "main.c" is used to create an output file "file.bin" with the encryption key "key" by doing this:
H:\Daniweb>xorcrypt main.c file.bin key
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".
H:\Daniweb>xorcrypt file.bin file.txt key
If the code shown here was in "main.c", you should find that the second output "file.txt" matches "main.c".
Last edited : Aug 4th, 2006.
c Syntax | 4 stars
  1. #include <stdio.h>
  2.  
  3. int main(int argc, char *const *argv)
  4. {
  5. if ( argc == 4 )
  6. {
  7. FILE *input = fopen(argv[1], "rb");
  8. FILE *output = fopen(argv[2], "wb");
  9. if ( input != NULL && output != NULL )
  10. {
  11. unsigned char buffer[BUFSIZ];
  12. size_t count, i, j = 0;
  13. do {
  14. count = fread(buffer, sizeof *buffer, sizeof buffer, input);
  15. for ( i = 0; i < count; ++i )
  16. {
  17. buffer[i] ^= argv[3][j++];
  18. if ( argv[3][j] == '\0' )
  19. {
  20. j = 0; /* restart at the beginning of the key */
  21. }
  22. }
  23. fwrite(buffer, sizeof *buffer, count, output);
  24. } while ( count == sizeof buffer );
  25. fclose(input);
  26. fclose(output);
  27. }
  28. }
  29. return 0;
  30. }
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
Post Comment

Only community members can submit or comment on code snippets. You must register or log in to contribute.

DaniWeb Marketplace (Sponsored Links)
All times are GMT -4. The time now is 5:49 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC