Advertisement
Guest User

Untitled

a guest
Dec 2nd, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. void encrypt(char* msg, size_t msg_size, char* key, size_t key_size)
  5. {
  6. size_t key_index = 0;
  7. for (size_t i = 0; i < msg_size; i++)
  8. {
  9. msg[i] ^= key[key_index];
  10.  
  11. ++key_index;
  12. if (key_index > key_size)
  13. key_index = 0;
  14. }
  15. }
  16.  
  17. void encrypt_file(char* in_name, char* out_name, char* key, size_t key_size)
  18. {
  19. char buf[1024];
  20. size_t nread;
  21.  
  22. FILE* in_file = fopen(in_name, "rb");
  23. FILE* out_file = fopen(out_name, "wb");
  24.  
  25. int count = 0;
  26. while ((nread = fread(buf, 1, sizeof(buf), in_file)) > 0)
  27. {
  28. count += nread;
  29. printf("Bytes read: %i\nTotal: %i\n\n", nread, count);
  30. encrypt(buf, nread, key, key_size);
  31. fwrite(buf, 1, nread, out_file);
  32. }
  33.  
  34. if (ferror(in_file) || ferror(out_file))
  35. {
  36. printf("Some error occured lol.\n");
  37. }
  38.  
  39. fclose(in_file);
  40. fclose(out_file);
  41. }
  42.  
  43. int main()
  44. {
  45. char key[] = "KsAdda2223132dfSDASDSSDASDWDsfsad3A";
  46. size_t key_size = strlen(key);
  47.  
  48. encrypt_file("lifesaver2.jpg", "encrypted.jpg", key, key_size);
  49. return 0;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement