Advertisement
Guest User

blum.cpp

a guest
Jan 22nd, 2020
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.61 KB | None | 0 0
  1. #include "Blum.hpp"
  2. #include "stdio.h"
  3. #include "stdlib.h"
  4.  
  5. using namespace std;
  6.  
  7. int losowe()                                                                    //Generator wartosci x0
  8. {
  9.     int a;
  10.     random_device device;
  11.     mt19937 generator(device());
  12.     uniform_int_distribution<int> distribution(65, 122);
  13.     a = distribution(generator);
  14.     if (a > 90 && a < 97) { a = losowe(); } // eliminuje tutaj znaki inne niż litery
  15.     return a;
  16. }
  17.  
  18. HugeInt modpow(HugeInt base, HugeInt exp, HugeInt modulus) {
  19.     base = base % modulus;
  20.     HugeInt result = 1;
  21.     while (exp > 0) {
  22.         if (exp % 2 == 1) result = (result * base) % modulus;
  23.         base = (base * base) % modulus;
  24.         exp /= 2;
  25.     }
  26.     return result;
  27. }
  28.  
  29. vector<bool> BlumMicali( int dlugosc)
  30. {
  31.     srand( time( NULL ) );
  32.     HugeInt a = HugeInt("12360923874510234561092345619023465019234561092345601234965123495619234659123465901234650923456019432560923456109234650059360928365127965012397561230489751209571627389519659185234750129875104928502");
  33.     HugeInt p = HugeInt("97512978125816728651326745927562561375169519415679951917625138884594562974219756961687292466758265957415675231498987423728564198147423285187561498415673281280265418907820596742182062749517488941522");
  34.  
  35.     a = HugeInt("509");
  36.     p = HugeInt("521");
  37.  
  38.     HugeInt x0 = losowe();
  39.     HugeInt x;
  40.  
  41.     vector<bool> klucz;
  42.  
  43.     for( int i=0; i < dlugosc * 8; i++)
  44.     {
  45.         x = modpow(a, x0, p);
  46.         //cout << x << '\t';
  47.         if( x > (p-1)/2 ) klucz.push_back(1);
  48.         else klucz.push_back(0);
  49.         x0 = x;
  50.     }
  51.     return klucz;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement