Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- *
- * RSA Key Generation Implementation in C#
- *
- * http://csharpcodewhisperer.blogspot.com
- *
- */
- BigInteger P = new BigInteger();
- BigInteger Q = new BigInteger();
- BigInteger R = new BigInteger();
- BigInteger phiR = new BigInteger();
- BigInteger K = new BigInteger();
- BigInteger E = new BigInteger();
- BigInteger D = new BigInteger();
- void CalculateRandPhiR(string LargePrime1,string LargePrime2)
- {
- BigInteger.TryParse(LargePrime1,out P);
- BigInteger.TryParse(LargePrime2,out Q);
- R = BigInteger.Multiply(P,Q);
- phiR = BigInteger.Multiply(P-1,Q-1);
- textBoxR.Text = R.ToString();
- textBoxPhiR.Text = phiR.ToString();
- }
- bool IsCoprime(BigInteger value1, BigInteger value2)
- {
- return ( BigInteger.GreatestCommonDivisor(value1,value2) == 1 );
- }
- // Note: This method for finding candidates for K is extremely weak, and not cryptographically sound.
- // The cryptographically secure method would involve picking a random number greater than Phi(R),
- // but less than 2*Phi(R), and incrementally checking for co-primeness.
- // This would, however, likely take a very long time.
- void FindKCandidates()
- {
- List<BigInteger> candidates = new List<BigInteger>();
- BigInteger test = new BigInteger();
- for (BigInteger i = 2; i < 30; i++)
- {
- test = BigInteger.Multiply(phiR,i) + 1;
- if(IsCoprime(phiR,test))
- {
- candidates.Add(test);
- }
- }
- if(candidates.Count>0)
- {
- listBoxK.Items.Clear();
- foreach(BigInteger candidate in candidates)
- {
- listBoxK.Items.Add(candidate.ToString());
- }
- }
- else
- {
- listBoxK.Items.Add("No co-primes found!");
- }
- }
- void ListBoxK_SelectedIndexChanged(object sender, EventArgs e)
- {
- if(!BigInteger.TryParse(listBoxK.SelectedItem.ToString(),out K)) {
- return;
- }
- // Factor K
- GetFactorPair(K);
- // Fill E and D
- textBoxE.Text = E.ToString();
- textBoxD.Text = D.ToString();
- // Calculate
- BigInteger remainder = new BigInteger();
- BigInteger.DivRem(BigInteger.Multiply(E,D),R,out remainder);
- checkBoxED1modR.Checked = ( remainder == new BigInteger(1) );
- checkBoxDRcoprime.Checked = IsCoprime(D,R);
- checkBoxERcoprime.Checked = IsCoprime(E,R);
- }
- void GetFactorPair(BigInteger numberToFactor)
- {
- E = new BigInteger(0);
- D = new BigInteger(0);
- // If numberToFactor is greater than double.MaxValue, our calculation below will fail,
- // As unfortunately BigInteger does not support a Sqrt function. Thus, we are limited
- // to double.MaxValue. Possible improvements include writing own square root function.
- if(numberToFactor>new BigInteger(double.MaxValue)) {
- return;
- }
- BigInteger sqrt = new BigInteger(Math.Ceiling(Math.Sqrt((double)numberToFactor)));
- // If perfect square, return, as this number will not do.
- if (BigInteger.Multiply(sqrt,sqrt) == numberToFactor) {
- return;
- }
- for (BigInteger i = 2; i < sqrt; i++)
- {
- if (numberToFactor % i == 0)
- {
- E = i;
- D = BigInteger.Divide(numberToFactor,i);
- //return;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement