Advertisement
Guest User

Untitled

a guest
Nov 21st, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <gcrypt.h>
  3. #include <time.h>
  4. #include <sys/stat.h>
  5. //#define GCRY_CIPHER_AES256
  6. //#define GCRY_KDF_PBKDF2
  7. void intitializeLibrary();
  8.  
  9. int main(int argc, char **argv)
  10. {
  11. if(argc ==2){
  12. char *filename = argv[1];
  13. printf("The file you've requested to be encrypted is %s \n", filename);
  14. FILE *ifp, *ifpout;
  15. char *mode ="rb";
  16.  
  17. ifp = fopen(filename, mode);
  18. ifpout = fopen("output.txt", "w");
  19. if (ifp == NULL || ifpout == NULL){
  20. fprintf(stderr, "Can't open file! \n");
  21. exit(1);
  22. } else{
  23. intitializeLibrary();
  24. struct stat st;
  25. stat(filename, &st);
  26. int size = st.st_size;
  27.  
  28. char *encBuffer = malloc(32);
  29. char str[100];
  30. char keybuffer[32];
  31. char* salt = "CNT5410";
  32. char* iv = "assignment222222";
  33. printf("Enter password: ");
  34. gcry_cipher_hd_t handle;
  35. gets(str);
  36.  
  37. gpg_error_t err;
  38. err = gcry_kdf_derive(str, 100, GCRY_KDF_PBKDF2, GCRY_CIPHER_AES256, salt, 7, 3, 32, keybuffer);
  39. if(err){
  40. printf("Error in PBKDF2 \n");
  41. exit(1);
  42. }
  43.  
  44. puts(keybuffer);
  45.  
  46. err = gcry_cipher_open(&handle, GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_ECB, 0);
  47. if(err){
  48. printf("Error in cipheropen\n");
  49. exit(1);
  50. }
  51.  
  52. err = gcry_cipher_setkey(handle, keybuffer, 32);
  53. if(err){
  54. printf("Error in cipheropen\n");
  55. exit(1);
  56. }
  57.  
  58. err = gcry_cipher_setiv(handle, iv, 16);
  59. int bytes;
  60. while(!feof(ifp)){
  61. bytes = fread(encBuffer, 1, size, ifp);
  62. if(!bytes){
  63. break;
  64. }
  65. while(bytes < 32){
  66. encBuffer[bytes++] = 0x0;
  67. gcry_cipher_encrypt(handle, encBuffer, size, NULL,0);
  68. bytes = fwrite(encBuffer, size, 16, ifpout);
  69. }
  70. }
  71.  
  72. gcry_cipher_close(handle);
  73. fclose(ifp);
  74. fclose(ifpout);
  75.  
  76. struct stat st2;
  77. stat(filename, &st2);
  78. int size2 = st2.st_size;
  79.  
  80. err = gcry_cipher_open(&handle, GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_ECB, 0);
  81. if(err){
  82. printf("Error in cipheropen\n");
  83. exit(1);
  84. }
  85.  
  86. err = gcry_cipher_setkey(handle, keybuffer, 32);
  87. if(err){
  88. printf("Error in cipheropen\n");
  89. exit(1);
  90. }
  91.  
  92. err = gcry_cipher_setiv(handle, iv, 16);
  93. ifp = fopen("output.txt", mode);
  94. ifpout = fopen(filename, "w");
  95. while(!feof(ifp)){
  96. bytes = fread(encBuffer, 1, size2, ifp);
  97. if(!bytes){
  98. break;
  99. }
  100. while(bytes < 32){
  101. encBuffer[bytes++] = 0x0;
  102. gcry_cipher_encrypt(handle, encBuffer, size2, NULL,0);
  103. bytes = fwrite(encBuffer, 1, size2, ifpout);
  104. }
  105. }
  106.  
  107. free(encBuffer);
  108. encBuffer = NULL;
  109.  
  110.  
  111. }
  112. }else{
  113. puts("Please enter one file to encrypt");
  114. }
  115. return 0;
  116. }
  117.  
  118. void intitializeLibrary(){
  119.  
  120. /* Version check should be the very first call because it
  121. makes sure that important subsystems are intialized. */
  122. if (!gcry_check_version (GCRYPT_VERSION)){
  123. fputs ("libgcrypt version mismatch\n", stderr);
  124. exit (2);
  125. }
  126.  
  127. /* Disable secure memory. */
  128. gcry_control (GCRYCTL_DISABLE_SECMEM, 0);
  129.  
  130. /* ... If required, other initialization goes here. */
  131.  
  132. /* Tell Libgcrypt that initialization has completed. */
  133. gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
  134.  
  135. /* Disable secure memory. */
  136. gcry_control (GCRYCTL_DISABLE_SECMEM, 0);
  137.  
  138. /* ... If required, other initialization goes here. */
  139.  
  140. /* Tell Libgcrypt that initialization has completed. */
  141. gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement