Advertisement
BurningBunny

RSA Key Generation Implementation in C#

Aug 3rd, 2013
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.95 KB | None | 0 0
  1. /*
  2.  *
  3.  * RSA Key Generation Implementation in C#
  4.  *
  5.  *  http://csharpcodewhisperer.blogspot.com
  6.  *
  7.  */
  8. BigInteger P = new BigInteger();
  9. BigInteger Q = new BigInteger();
  10.  
  11. BigInteger R    = new BigInteger();
  12. BigInteger phiR = new BigInteger();
  13.  
  14. BigInteger K = new BigInteger();
  15.  
  16. BigInteger E = new BigInteger();
  17. BigInteger D = new BigInteger();
  18.  
  19. void CalculateRandPhiR(string LargePrime1,string LargePrime2)
  20. {
  21.     BigInteger.TryParse(LargePrime1,out P);
  22.     BigInteger.TryParse(LargePrime2,out Q);
  23.    
  24.     R   = BigInteger.Multiply(P,Q);
  25.     phiR    = BigInteger.Multiply(P-1,Q-1);
  26.    
  27.     textBoxR.Text    = R.ToString();
  28.     textBoxPhiR.Text = phiR.ToString();
  29. }
  30.  
  31. bool IsCoprime(BigInteger value1, BigInteger value2)
  32. {
  33.     return ( BigInteger.GreatestCommonDivisor(value1,value2) == 1 );
  34. }
  35.  
  36. // Note: This method for finding candidates for K is extremely weak, and not cryptographically sound.
  37. // The cryptographically secure method would involve picking a random number greater than Phi(R),
  38. // but less than 2*Phi(R), and incrementally checking for co-primeness.
  39. // This would, however, likely take a very long time.
  40. void FindKCandidates()
  41. {
  42.     List<BigInteger> candidates = new List<BigInteger>();
  43.    
  44.     BigInteger test = new BigInteger();
  45.     for (BigInteger i = 2; i < 30; i++)
  46.     {
  47.         test = BigInteger.Multiply(phiR,i) + 1;
  48.         if(IsCoprime(phiR,test))
  49.         {
  50.             candidates.Add(test);
  51.         }
  52.     }
  53.    
  54.     if(candidates.Count>0)
  55.     {
  56.         listBoxK.Items.Clear();
  57.         foreach(BigInteger candidate in candidates)
  58.         {
  59.             listBoxK.Items.Add(candidate.ToString());
  60.         }
  61.     }
  62.     else
  63.     {
  64.         listBoxK.Items.Add("No co-primes found!");
  65.     }
  66. }
  67.  
  68. void ListBoxK_SelectedIndexChanged(object sender, EventArgs e)
  69. {
  70.     if(!BigInteger.TryParse(listBoxK.SelectedItem.ToString(),out K)) {
  71.         return;
  72.     }
  73.    
  74.     // Factor K
  75.     GetFactorPair(K);
  76.    
  77.     // Fill E and D
  78.     textBoxE.Text = E.ToString();
  79.     textBoxD.Text = D.ToString();
  80.    
  81.     // Calculate
  82.     BigInteger remainder = new BigInteger();
  83.     BigInteger.DivRem(BigInteger.Multiply(E,D),R,out remainder);
  84.    
  85.     checkBoxED1modR.Checked   = ( remainder == new BigInteger(1) );
  86.     checkBoxDRcoprime.Checked = IsCoprime(D,R);
  87.     checkBoxERcoprime.Checked = IsCoprime(E,R);
  88. }
  89.  
  90. void GetFactorPair(BigInteger numberToFactor)
  91. {
  92.     E = new BigInteger(0);
  93.     D = new BigInteger(0);
  94.    
  95.     // If numberToFactor is greater than double.MaxValue, our calculation below will fail,
  96.     // As unfortunately BigInteger does not support a Sqrt function. Thus, we are limited
  97.     // to double.MaxValue. Possible improvements include writing own square root function.
  98.     if(numberToFactor>new BigInteger(double.MaxValue)) {
  99.         return;
  100.     }
  101.    
  102.     BigInteger sqrt = new BigInteger(Math.Ceiling(Math.Sqrt((double)numberToFactor)));
  103.    
  104.     // If perfect square, return, as this number will not do.
  105.     if (BigInteger.Multiply(sqrt,sqrt) == numberToFactor) {
  106.         return;
  107.     }
  108.    
  109.     for (BigInteger i = 2; i < sqrt; i++)
  110.     {
  111.         if (numberToFactor % i == 0)
  112.         {
  113.             E = i;
  114.             D = BigInteger.Divide(numberToFactor,i);
  115.             //return;
  116.         }
  117.     }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement