Evilerus

RSACracker

Nov 6th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.26 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdbool.h>
  3. #include <stdlib.h>
  4. #include <inttypes.h>
  5. #include <limits.h>
  6. #include <time.h>
  7. #include <pthread.h>
  8.  
  9. bool isComprime(uintmax_t p, uintmax_t q, uintmax_t expectedResult);
  10. uintmax_t gcd(uintmax_t p, uintmax_t q);
  11. void generateErathostenesSieve(uintmax_t number, bool* erathostenesSieve);
  12. uintmax_t zsqrt(uintmax_t number);
  13. bool isPrime(uintmax_t number, bool* erathostenesSieve);
  14. int main(int argc, char **argv);
  15.  
  16. struct arg_struct {
  17.     uintmax_t from;
  18.     uintmax_t to;
  19.     uintmax_t searchedNumber;
  20.     unsigned int threadNumber;
  21.     bool* erathostenesSieve;
  22.     bool* isComplete;
  23. };
  24.  
  25. void generateErathostenesSieve(uintmax_t number, bool* erathostenesSieve) {
  26.     for (uintmax_t i=0; i<number; i++) {
  27.         erathostenesSieve[i] = true;
  28.     }
  29.     for (uintmax_t i=2; i<zsqrt(number); i++) {
  30.         if (erathostenesSieve[i] == true) {
  31.             for (uintmax_t j=i*i; j<number; j=j+i) {
  32.                 erathostenesSieve[j] = false;
  33.             }
  34.         }
  35.     }
  36. }
  37.  
  38. uintmax_t zsqrt(uintmax_t number)
  39. {
  40.     if(number==0) return 0;
  41.     uintmax_t nextEstimate = number;
  42.     uintmax_t result = 0;
  43.     uintmax_t prevEstimate;
  44.     do{
  45.         prevEstimate = result;
  46.         result = nextEstimate;
  47.         nextEstimate = (result + number/result) >> 1;
  48.     }while(nextEstimate != prevEstimate);
  49.     return result;
  50. }
  51.  
  52. bool isPrime(uintmax_t number, bool erathostenesSieve[]) {
  53.     if (number <= 1) {
  54.         return false;
  55.     } else if (number == 2) {
  56.         return true;
  57.     } else {
  58.         if (erathostenesSieve[number] == true) {
  59.             return true;
  60.         } else {
  61.             return false;
  62.         }
  63.     }
  64. }
  65.  
  66. void *searchForComprimesThread(void *vargp) {
  67.     struct arg_struct *args = vargp;
  68.  
  69.     printf("Thread %d started its job! (searching from %"PRIuMAX" to %"PRIuMAX")\n", args->threadNumber, args->from, args->to);
  70.  
  71.     for (uintmax_t i = args->from; i<=args->to; i++) {
  72.         if (*(args->isComplete) == true)
  73.             break;
  74.         if (isPrime(i, args->erathostenesSieve) == true) {
  75.             for (uintmax_t j = i; j<=args->to; j++) {
  76.                 if (i*j > args->searchedNumber)
  77.                     break;
  78.                 if ((isPrime(j, args->erathostenesSieve) == true) && (j != i)) {
  79.                         if (i*j == args->searchedNumber) {
  80.                             printf("[Thread %d] Found combination for %"PRIuMAX"! p = %"PRIuMAX", q = %"PRIuMAX"\n", args->threadNumber, args->searchedNumber, i, j);
  81.                             *(args->isComplete) = true;
  82.                             break;
  83.                         }
  84.                 }
  85.             }
  86.         }
  87.     }
  88.  
  89.     printf("Thread %d finished its job!\n", args->threadNumber);
  90.  
  91.     return NULL;
  92. }
  93.  
  94. int main(int argc, char **argv) {
  95.     char* endPointer;
  96.     if (argc != 3) {
  97.         printf("usage: %s [number to crack] [threads to be used]", argv[0]);
  98.         return 1;
  99.     } else {
  100.         if (strtol(argv[2], NULL, 10) <= 0) {
  101.             printf("Number of threads to be used must be greater than zero!\n");
  102.             return 1;
  103.         }
  104.  
  105.         int threadCount = (int)strtol(argv[2], NULL, 10);
  106.         time_t beginning = time(NULL);
  107.         uintmax_t searchedNumber = strtoumax(argv[1], &endPointer, 10);
  108.         struct arg_struct args[threadCount];
  109.         pthread_t searchingThreads[threadCount];
  110.         bool *erathostenesSieve = malloc(searchedNumber*sizeof(bool));
  111.         bool isComplete = false;
  112.         printf("Searching for combinations for number %"PRIuMAX"\n", searchedNumber);
  113.         printf("Generating Erathostenes' Sieve...\n");
  114.         generateErathostenesSieve(searchedNumber, erathostenesSieve);
  115.         printf("Creating arguments for threads...\n");
  116.  
  117.         for (int i = 0; i<threadCount; i++) {
  118.             if (i == 0) {
  119.                 args[i].from = 2;
  120.             } else {
  121.                 args[i].from = (searchedNumber/threadCount)*i;
  122.             }
  123.             if (i == threadCount-1) {
  124.                 args[i].to = searchedNumber;
  125.             } else {
  126.                 args[i].to = (searchedNumber/threadCount)*(i+1);
  127.             }
  128.             args[i].searchedNumber = searchedNumber;
  129.             args[i].erathostenesSieve = erathostenesSieve;
  130.             args[i].threadNumber  = i+1;
  131.             args[i].isComplete = &isComplete;
  132.         }
  133.  
  134.         printf("Starting threads...\n");
  135.  
  136.         for (int i = 0; i<threadCount; i++) {
  137.             printf("Starting thread %d...\n", i+1);
  138.             pthread_create(&searchingThreads[i], NULL, searchForComprimesThread, (void *)&args[i]);
  139.         }
  140.  
  141.         printf("Waiting for threads to finish their job...\n");
  142.  
  143.         for (int i = 0; i<threadCount; i++) {
  144.             printf("Waiting for thread %d...\n", i+1);
  145.             pthread_join(searchingThreads[i], NULL);
  146.         }
  147.  
  148.         free(erathostenesSieve);
  149.  
  150.         time_t end = time(NULL);
  151.  
  152.  
  153.         if (isComplete) {
  154.             printf("Successfully found a combination for number %"PRIuMAX" in %ld seconds\n", searchedNumber, (long)(end-beginning));
  155.             return 0;
  156.         } else {
  157.             printf("Didn't find any combinations for number %"PRIuMAX" in %ld seconds\n", searchedNumber, (long)(end-beginning));
  158.             return 1;
  159.         }
  160.     }
  161. }
Add Comment
Please, Sign In to add comment