Advertisement
dipto181

simple_rsa_decryption

Jul 18th, 2021
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.21 KB | None | 0 0
  1. #include "rsa/config.h"
  2. #include "rsa/aes.h"
  3. #include "rsa/bignum.h"
  4. #include "rsa/rsa.h"
  5. #include <sys/wait.h>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #include <fcntl.h>
  9. #include <errno.h>
  10. #include <stdlib.h>
  11. #include <sys/resource.h>
  12. //#include <pthread.h>
  13. #include <time.h>
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <unistd.h>
  17. #include "rsa/key.h"
  18.  
  19. #include "rsa/memory_buffer_alloc.h"
  20. #include "rsa/memory.h"
  21. #include "rsa/platform.h"
  22. #include "rsa/threading.h"
  23.  
  24.  
  25. #define NUM_OF_THREAD 2
  26.  
  27.  
  28. void decryption(){
  29.  
  30. unsigned char alloc_buf[10000];
  31. memory_buffer_alloc_init( &alloc_buf, sizeof(alloc_buf) );
  32.  
  33. unsigned char private_encrypt[KEY_BUFFER_SIZE];
  34. int total_dec=1000;
  35. unsigned char * buffer = 0;
  36. long length;
  37. unsigned char msg_decrypted[KEY_LEN];
  38.  
  39.  
  40. FILE * fp2 = fopen ("msg.enc", "rb");
  41. int size1=KEY_BUFFER_SIZE;
  42. if(fp2){
  43. while(size1>0){
  44. fread(private_encrypt,1,sizeof (private_encrypt),fp2);
  45. size1=size1-1;
  46. }
  47. }
  48. fclose(fp2);
  49.  
  50. FILE * fp = fopen ("rsa_priv.txt", "rb");
  51.  
  52. if (fp){
  53. fseek (fp, 0, SEEK_END);
  54. length = ftell (fp);
  55. fseek (fp, 0, SEEK_SET);
  56. buffer = calloc (1,length+1);
  57. if (buffer){
  58. fread (buffer, 1, length, fp);
  59. }
  60. fclose (fp);
  61. }
  62.  
  63. // initialize rsaContext
  64. rsa_context rsaContext;
  65. rsa_init(&rsaContext,RSA_PKCS_V15, 0);
  66. rsaContext.len=KEY_LEN;
  67.  
  68. // spliting keys and load into rsa context
  69. const char s[3] = "= ";
  70. char *token;
  71. int k=0, size;
  72. char *rest=buffer;
  73.  
  74. // get the first token
  75. token = strtok_r(rest,s,&rest);
  76.  
  77. // walk through other tokens
  78. while( token != NULL ) {
  79. size = strlen(token);
  80.  
  81. switch (k) {
  82. case 1:
  83. token[size-1]='\0';
  84. mpi_read_string(&rsaContext.N, 16, token);
  85. break;
  86.  
  87. case 3:
  88. token[size-1]='\0';
  89. mpi_read_string(&rsaContext.E, 16, token);
  90. break;
  91.  
  92. case 5:
  93. token[size-1]='\0';
  94. mpi_read_string(&rsaContext.D, 16, token);
  95. break;
  96.  
  97. case 7:
  98. token[size-1]='\0';
  99. mpi_read_string(&rsaContext.P, 16, token);
  100. break;
  101.  
  102. case 9:
  103. token[size-1]='\0';
  104. mpi_read_string(&rsaContext.Q, 16, token);
  105. break;
  106.  
  107. case 11:
  108. token[size-1]='\0';
  109. mpi_read_string(&rsaContext.DP, 16, token);
  110. break;
  111.  
  112. case 13:
  113. token[size-1]='\0';
  114. mpi_read_string(&rsaContext.DQ, 16, token);
  115. break;
  116.  
  117. case 15:
  118. token[size-1]='\0';
  119. mpi_read_string(&rsaContext.QP, 16, token);
  120. break;
  121. }
  122. k=k+1;
  123. //token = strtok(NULL, "= \n");
  124. token = strtok_r(rest, "= \n", &rest);
  125. }
  126.  
  127. if( rsa_private(&rsaContext,private_encrypt, msg_decrypted) != 0 ) {
  128. printf( "Decryption failed! %d\n", rsa_private(&rsaContext,private_encrypt, msg_decrypted));
  129. }else{
  130. printf("%d Decrypted plaintext-----> %s\n",total_dec, msg_decrypted );
  131. }
  132.  
  133.  
  134. }
  135.  
  136. void thread_function(void * input){
  137.  
  138. printf("Test thread\n");
  139.  
  140. int total_loop=1000;
  141. while(total_loop>0){
  142. decryption();
  143. total_loop--;
  144. }
  145.  
  146. }
  147.  
  148.  
  149. int main(){
  150. int i;
  151. clock_t t;
  152.  
  153. // total number of thread
  154. pthread_t ths[NUM_OF_THREAD];
  155.  
  156. // start time count
  157. t = clock();
  158.  
  159. for (i = 0; i < NUM_OF_THREAD; i++) {
  160. pthread_create(&ths[i], NULL, thread_function, NULL);
  161. }
  162.  
  163. for (i = 0; i < NUM_OF_THREAD; i++) {
  164. void* res;
  165. pthread_join(ths[i], &res);
  166. }
  167.  
  168. // end time count
  169. t = clock() - t;
  170. double time_taken = ((double)t)/CLOCKS_PER_SEC; // in seconds
  171. printf("Took %f seconds to execute \n", time_taken);
  172. printf("Total Cycle %f \n", t);
  173. return 0;
  174. }
  175.  
  176.  
  177.  
  178.  
  179. output:
  180. $./multiThread
  181. Test thread
  182. Test thread
  183. Decryption failed! -17168
  184. Decryption failed! -17168
  185.  
  186.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement