Advertisement
Guest User

Untitled

a guest
Jun 8th, 2014
516
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.36 KB | None | 0 0
  1. // g++ -Wall -Wextra -Wconversion -Wno-unused-label -g3 cryptopp-test.cpp -o cryptopp-test.exe -lcryptopp
  2.  
  3. #include <iostream>
  4. using std::cout;
  5. using std::cerr;
  6. using std::endl;
  7.  
  8. #include <string>
  9. using std::string;
  10.  
  11. #include <algorithm>
  12. using std::replace;
  13.  
  14. #include <stdexcept>
  15. using std::runtime_error;
  16.  
  17. #include <cryptopp/filters.h>
  18. using CryptoPP::StringSink;
  19. using CryptoPP::StringSource;
  20.  
  21. #include <cryptopp/hex.h>
  22. using CryptoPP::HexEncoder;
  23.  
  24. #include <cryptopp/base64.h>
  25. using CryptoPP::Base64Decoder;
  26.  
  27. #include <cryptopp/integer.h>
  28. using CryptoPP::Integer;
  29.  
  30. #include <cryptopp/nbtheory.h>
  31. using CryptoPP::VerifyPrime;
  32.  
  33. #include <cryptopp/osrng.h>
  34. using CryptoPP::AutoSeededRandomPool;
  35.  
  36. string nz = "ofgWCuLjybRlzo0tZWJjNiuSfb4p4fAkd_wWJcyQoTbji9k0l8W26mPddx"
  37.             "HmfHQp-Vaw-4qPCJrcS2mJPMEzP1Pt0Bm4d4QlL-yRT-SFd2lZS-pCgNMs"
  38.             "D1W_YpRPEwOWvG6b32690r2jZ47soMZo9wGzjb_7OMg0LOL-bSf63kpaSH"
  39.             "SXndS5z5rexMdbBYUsLA9e-KXBdQOS-UTo7WTBEMa2R2CapHg665xsmtdV"
  40.             "MTBQY4uDZlxvb3qCo5ZwKh9kG4LT6_I5IhlJH7aGhyxXFvUK-DWNmoudF8"
  41.             "NAco9_h9iaGNj8q2ethFkMLs91kzk2PAcDTW9gb54h4FRWyuXpoQ";
  42.  
  43. string ez = "AQAB";
  44.  
  45. string dz = "Eq5xpGnNCivDflJsRQBXHx1hdR1k6Ulwe2JZD50LpXyWPEAeP88vLNO97I"
  46.             "jlA7_GQ5sLKMgvfTeXZx9SE-7YwVol2NXOoAJe46sui395IW_GO-pWJ1O0"
  47.             "BkTGoVEn2bKVRUCgu-GjBVaYLU6f3l9kJfFNS3E0QbVdxzubSu3Mkqzjkn"
  48.             "439X0M_V51gfpRLI9JYanrC4D4qAdGcopV_0ZHHzQlBjudU2QvXt4ehNYT"
  49.             "CBr6XCLQUShb1juUO1ZdiYoFaFQT5Tw8bGUl_x_jTj3ccPDVZFD9pIuhLh"
  50.             "BOneufuBiB4cS98l2SR_RQyGWSeWjnczT0QU91p1DhOVRuOopznQ";
  51.  
  52. void RSA_solve(const Integer& n, const Integer& e, const Integer& d,
  53.         Integer& p, Integer& q,
  54.         Integer& dmodp1, Integer& dmodq1, Integer& invqmodp);
  55.  
  56. #define UNUSED(x) ((void)x)
  57.  
  58. int main(int argc, char* argv[])
  59. {
  60.     UNUSED(argc); UNUSED(argv);
  61.  
  62.     string nn, ee, dd;
  63.  
  64.     // First, convert Base64URL encoding to Base64
  65.     replace(nz.begin(), nz.end(), '-', '+');
  66.     replace(ez.begin(), ez.end(), '-', '+');
  67.     replace(dz.begin(), dz.end(), '-', '+');
  68.     replace(nz.begin(), nz.end(), '_', '/');
  69.     replace(ez.begin(), ez.end(), '_', '/');
  70.     replace(dz.begin(), dz.end(), '_', '/');
  71.  
  72.     StringSource ss1(nz, true, new Base64Decoder(new StringSink(nn)));
  73.     StringSource ss2(ez, true, new Base64Decoder(new StringSink(ee)));
  74.     StringSource ss3(dz, true, new Base64Decoder(new StringSink(dd)));
  75.  
  76.     Integer n((byte*)nn.data(), nn.size());
  77.     Integer e((byte*)ee.data(), ee.size());
  78.     Integer d((byte*)dd.data(), dd.size());
  79.  
  80.     cout << "N: " << endl << n << endl << endl;
  81.     cout << "E: " << endl << e << endl << endl;
  82.     cout << "D: " << endl << d << endl << endl;
  83.  
  84.     Integer p, q;
  85.     Integer dmodp1, dmodq1, invqmodp;
  86.  
  87.     RSA_solve(n, e, d, p, q, dmodp1, dmodq1, invqmodp);
  88.  
  89.     cout << "P: " << endl << p << endl << endl;
  90.     cout << "Q: " << endl << q << endl << endl;
  91.  
  92.     cout << "D mod P-1: " << endl << dmodp1 << endl << endl;
  93.     cout << "D mod Q-1: " << endl << dmodq1 << endl << endl;
  94.     cout << "Inv Q mod P: " << endl << invqmodp << endl << endl;
  95.  
  96.     return 0;
  97. }
  98.  
  99. /*
  100.     From http://www.di-mgt.com.au/rsa_factorize_n.html
  101.  
  102.     1. [Initialize] Set k←de−1.
  103.     2. [Try a random g] Choose g at random from {2,…,N−1} and set t←k.
  104.     3. [Next t] If t is divisible by 2, set t←t/2 and x←g^t mod N. Otherwise go to step 2.
  105.     4. [Finished?] If x>1 and y=gcd(x−1,N)>1 then set p←y and q←N/y, output (p,q) and
  106.        terminate the algorithm. Otherwise go to step 3.
  107. */
  108.  
  109. void RSA_solve(const Integer& n, const Integer& e, const Integer& d,
  110.         Integer& p, Integer& q,
  111.         Integer& dmodp1, Integer& dmodq1, Integer& invqmodp)
  112. {
  113.     AutoSeededRandomPool prng;
  114.     Integer g = 1;
  115.     unsigned int SAFETY = 0;
  116.  
  117. STEP_1:
  118.     const Integer k = e * d - 1;
  119.     if(!k.IsEven())
  120.         throw runtime_error("e * d - 1 is not even");
  121.  
  122. STEP_2:
  123.     // g = 3, 5, 7, 11, ...
  124.     g += 2; while(!VerifyPrime(prng, g)) g += 2;
  125.     Integer t = k;
  126.  
  127. STEP_3:
  128.     if(SAFETY++ > 128)
  129.         throw runtime_error("could not factor n");
  130.  
  131.     if(!t.IsEven())
  132.         goto STEP_2;
  133.  
  134.     t /= 2;
  135.     Integer x = a_exp_b_mod_c(g, t, n);
  136.  
  137. STEP_4:
  138.     if(!(x > 1))
  139.         goto STEP_3;
  140.  
  141.     Integer y = GCD(x-1, n);
  142.     if(!(y > 1))
  143.         goto STEP_3;
  144.  
  145.     p = std::max(y, n/y);
  146.     q = std::min(y, n/y);
  147.  
  148.     Integer check = p * q;
  149.     if(n != check)
  150.         throw runtime_error("n != p * q");
  151.  
  152.     dmodp1 = d.Modulo(p - 1);
  153.     dmodq1 = d.Modulo(q - 1);
  154.     invqmodp = q.InverseMod(p);
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement