Guest User

Untitled

a guest
Dec 17th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. using System;
  2. using Org.BouncyCastle.Math;
  3.  
  4. public BigInteger CalculateRSA(BigInteger p, BigInteger q, BigInteger e)
  5. {
  6. // n = p*q - for illustration
  7. BigInteger n = p.Multiply(q);
  8.  
  9. // phi / r = (p-1)*(q-1)
  10. BigInteger phi = p.Subtract(BigInteger.One).Multiply(q.Subtract(BigInteger.One));
  11.  
  12. // d = e^−1 (mod λ(n))
  13. BigInteger d = e.ModInverse(phi);
  14.  
  15. // Return d
  16. return d;
  17. }
  18.  
  19. public void Main(string[] args){
  20. // Example usage - primes would have to be factored from n of public key
  21. string pText = "CC9EEF4A31452B93A2C3658566841BA7DFCD10B847D6749A91A32271DAAC6EFB5A45150AE684D632EDF8EAC03AA3F65EAA2648738E1B86137F66BE238B96D4AB",
  22. qText = "C364D7C501E30FC2076E5CFA57DFD4D8970AF695D754DD41A5CA13E760E2C5A1A447A1B7B8985225C25D0966C8CD8DB198860C37F3CE6CB190FB2B1FCDE11E61",
  23. eText = "65537";
  24.  
  25. // Load p and q as hex, e as decimal
  26. BigInteger p = new BigInteger(pText, 16),
  27. q = new BigInteger(qText, 16),
  28. e = new BigInteger(eText, 10);
  29.  
  30. // Calculate d - the private key
  31. BigInteger d = CalculateRSA(p, q, e);
  32.  
  33. // Output as hex
  34. Console.WriteLine(d.ToString(16));
  35. }
Add Comment
Please, Sign In to add comment