internal uint ModularPowerSmall( ulong Input, Integer Exponent, uint Modulus ) { if( Input == 0 ) return 0; if( Input == Modulus ) { // It is congruent to zero % Modulus. return 0; } // Result is not zero at this point. if( Exponent.IsZero() ) return 1; ulong Result = Input; if( Input > Modulus ) Result = Input % Modulus; if( Exponent.IsOne()) return (uint)Result; ulong XForModPowerU = Result; ExponentCopy.Copy( Exponent ); int TestIndex = 0; Result = 1; while( true ) { if( (ExponentCopy.GetD( 0 ) & 1) == 1 ) // If the bottom bit is 1. { Result = Result * XForModPowerU; Result = Result % Modulus; } ExponentCopy.ShiftRight( 1 ); // Divide by 2. if( ExponentCopy.IsZero()) break; // Square it. XForModPowerU = XForModPowerU * XForModPowerU; XForModPowerU = XForModPowerU % Modulus; } return (uint)Result; }
internal bool FindTwoFactorsWithFermat( Integer Product, Integer P, Integer Q, ulong MinimumX ) { ECTime StartTime = new ECTime(); StartTime.SetToNow(); Integer TestSqrt = new Integer(); Integer TestSquared = new Integer(); Integer SqrRoot = new Integer(); TestSquared.Copy( Product ); IntMath.Multiply( TestSquared, Product ); IntMath.SquareRoot( TestSquared, SqrRoot ); TestSqrt.Copy( SqrRoot ); IntMath.DoSquare( TestSqrt ); // IntMath.Multiply( TestSqrt, SqrRoot ); if( !TestSqrt.IsEqual( TestSquared )) throw( new Exception( "The square test was bad." )); // Some primes: // 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, // 101, 103, 107 P.SetToZero(); Q.SetToZero(); Integer TestX = new Integer(); SetupQuadResArray( Product ); ulong BaseTo37 = QuadResBigBase * 29UL * 31UL * 37UL; // ulong BaseTo31 = QuadResBigBase * 29UL * 31UL; ulong ProdModTo37 = IntMath.GetMod64( Product, BaseTo37 ); // ulong ProdModTo31 = IntMath.GetMod64( Product, BaseTo31 ); for( ulong BaseCount = 0; BaseCount < (29 * 31 * 37); BaseCount++ ) { if( (BaseCount & 0xF) == 0 ) Worker.ReportProgress( 0, "Find with Fermat BaseCount: " + BaseCount.ToString() ); if( Worker.CancellationPending ) return false; ulong Base = (BaseCount + 1) * QuadResBigBase; // BaseCount times 223,092,870. if( Base < MinimumX ) continue; Base = BaseCount * QuadResBigBase; // BaseCount times 223,092,870. for( uint Count = 0; Count < QuadResArrayLast; Count++ ) { // The maximum CountPart can be is just under half the size of // the Product. (Like if Y - X was equal to 1, and Y + X was // equal to the Product.) If it got anywhere near that big it // would be inefficient to try and find it this way. ulong CountPart = Base + QuadResArray[Count]; ulong Test = ProdModTo37 + (CountPart * CountPart); // ulong Test = ProdModTo31 + (CountPart * CountPart); Test = Test % BaseTo37; // Test = Test % BaseTo31; if( !IntegerMath.IsQuadResidue29( Test )) continue; if( !IntegerMath.IsQuadResidue31( Test )) continue; if( !IntegerMath.IsQuadResidue37( Test )) continue; ulong TestBytes = (CountPart & 0xFFFFF); TestBytes *= (CountPart & 0xFFFFF); ulong ProdBytes = Product.GetD( 1 ); ProdBytes <<= 8; ProdBytes |= Product.GetD( 0 ); uint FirstBytes = (uint)(TestBytes + ProdBytes); if( !IntegerMath.FirstBytesAreQuadRes( FirstBytes )) { // Worker.ReportProgress( 0, "First bytes aren't quad res." ); continue; } TestX.SetFromULong( CountPart ); IntMath.MultiplyULong( TestX, CountPart ); TestX.Add( Product ); // uint Mod37 = (uint)IntMath.GetMod32( TestX, 37 ); // if( !IntegerMath.IsQuadResidue37( Mod37 )) // continue; // Do more of these tests with 41, 43, 47... // if( !IntegerMath.IsQuadResidue41( Mod37 )) // continue; // Avoid doing this square root at all costs. if( IntMath.SquareRoot( TestX, SqrRoot )) { Worker.ReportProgress( 0, " " ); if( (CountPart & 1) == 0 ) Worker.ReportProgress( 0, "CountPart was even." ); else Worker.ReportProgress( 0, "CountPart was odd." ); // Found an exact square root. // P + (CountPart * CountPart) = Y*Y // P = (Y + CountPart)Y - CountPart) P.Copy( SqrRoot ); Integer ForSub = new Integer(); ForSub.SetFromULong( CountPart ); IntMath.Subtract( P, ForSub ); // Make Q the bigger one and put them in order. Q.Copy( SqrRoot ); Q.AddULong( CountPart ); if( P.IsOne() || Q.IsOne()) { // This happens when testing with small primes. Worker.ReportProgress( 0, " " ); Worker.ReportProgress( 0, " " ); Worker.ReportProgress( 0, "Went all the way to 1 in FindTwoFactorsWithFermat()." ); Worker.ReportProgress( 0, " " ); Worker.ReportProgress( 0, " " ); P.SetToZero(); // It has no factors. Q.SetToZero(); return true; // Tested everything, so it's a prime. } Worker.ReportProgress( 0, "Found P: " + IntMath.ToString10( P ) ); Worker.ReportProgress( 0, "Found Q: " + IntMath.ToString10( Q ) ); Worker.ReportProgress( 0, "Seconds: " + StartTime.GetSecondsToNow().ToString( "N1" )); Worker.ReportProgress( 0, " " ); throw( new Exception( "Testing this." )); // return true; // With P and Q. } // else // Worker.ReportProgress( 0, "It was not an exact square root." ); } } // P and Q would still be zero if it never found them. return false; }
// This is the standard modular power algorithm that // you could find in any reference, but its use of // the new modular reduction algorithm is new. // The square and multiply method is in Wikipedia: // https://en.wikipedia.org/wiki/Exponentiation_by_squaring // x^n = (x^2)^((n - 1)/2) if n is odd. // x^n = (x^2)^(n/2) if n is even. internal void ModularPower( Integer Result, Integer Exponent, Integer Modulus, bool UsePresetBaseArray ) { if( Result.IsZero()) return; // With Result still zero. if( Result.IsEqual( Modulus )) { // It is congruent to zero % ModN. Result.SetToZero(); return; } // Result is not zero at this point. if( Exponent.IsZero() ) { Result.SetFromULong( 1 ); return; } if( Modulus.ParamIsGreater( Result )) { // throw( new Exception( "This is not supposed to be input for RSA plain text." )); IntMath.Divide( Result, Modulus, Quotient, Remainder ); Result.Copy( Remainder ); } if( Exponent.IsOne()) { // Result stays the same. return; } if( !UsePresetBaseArray ) SetupGeneralBaseArray( Modulus ); XForModPower.Copy( Result ); ExponentCopy.Copy( Exponent ); int TestIndex = 0; Result.SetFromULong( 1 ); while( true ) { if( (ExponentCopy.GetD( 0 ) & 1) == 1 ) // If the bottom bit is 1. { IntMath.Multiply( Result, XForModPower ); ModularReduction( TempForModPower, Result ); Result.Copy( TempForModPower ); } ExponentCopy.ShiftRight( 1 ); // Divide by 2. if( ExponentCopy.IsZero()) break; // Square it. IntMath.Multiply( XForModPower, XForModPower ); ModularReduction( TempForModPower, XForModPower ); XForModPower.Copy( TempForModPower ); } // When ModularReduction() gets called it multiplies a base number // by a uint sized digit. So that can make the result one digit bigger // than GeneralBase. Then when they are added up you can get carry // bits that can make it a little bigger. int HowBig = Result.GetIndex() - Modulus.GetIndex(); // if( HowBig > 1 ) // throw( new Exception( "This does happen. Diff: " + HowBig.ToString() )); if( HowBig > 2 ) throw( new Exception( "The never happens. Diff: " + HowBig.ToString() )); ModularReduction( TempForModPower, Result ); Result.Copy( TempForModPower ); IntMath.Divide( Result, Modulus, Quotient, Remainder ); Result.Copy( Remainder ); if( Quotient.GetIndex() > 1 ) throw( new Exception( "This never happens. The quotient index is never more than 1." )); }
internal void TestBigDigits() { try { uint Base = 2 * 3 * 5; Integer BigBase = new Integer(); Integer Minus1 = new Integer(); Integer IntExponent = new Integer(); Integer IntBase = new Integer(); Integer Gcd = new Integer(); BigBase.SetFromULong( Base ); IntBase.SetFromULong( Base ); for( uint Count = 2; Count < 200; Count++ ) { // At Count = 2 BigBase will be 100, or 10^2. IntMath.MultiplyULong( BigBase, Base ); uint Exponent = Count + 1; IntExponent.SetFromULong( Exponent ); IntMath.GreatestCommonDivisor( IntBase, IntExponent, Gcd ); if( !Gcd.IsOne() ) { // ShowStatus( Exponent.ToString() + " has a factor in common with base." ); continue; } Minus1.Copy( BigBase ); IntMath.SubtractULong( Minus1, 1 ); ShowStatus( " " ); ulong ModExponent = IntMath.GetMod32( Minus1, Exponent ); if( ModExponent != 0 ) ShowStatus( Exponent.ToString() + " is not a prime." ); else ShowStatus( Exponent.ToString() + " might or might not be a prime." ); uint FirstFactor = IntMath.GetFirstPrimeFactor( Exponent ); if( (FirstFactor == 0) || (FirstFactor == Exponent)) { ShowStatus( Exponent.ToString() + " is a prime." ); } else { ShowStatus( Exponent.ToString() + " is composite with a factor of " + FirstFactor.ToString() ); } } } catch( Exception Except ) { ShowStatus( "Exception in TestDigits()." ); ShowStatus( Except.Message ); } }
private bool IsQuadResModProduct( uint Prime ) { // Euler's Criterion: Integer Exponent = new Integer(); Integer Result = new Integer(); Integer Modulus = new Integer(); Exponent.SetFromULong( Prime ); IntMath.SubtractULong( Exponent, 1 ); Exponent.ShiftRight( 1 ); // Divide by 2. Result.Copy( Product ); Modulus.SetFromULong( Prime ); IntMath.IntMathNew.ModularPower( Result, Exponent, Modulus, false ); if( Result.IsOne() ) return true; else return false; // Result should be Prime - 1. }
private void FindFactorsFromLeft( ulong A, ulong C, Integer Left, Integer Temp, Integer B ) { if( Worker.CancellationPending ) return; /* // (323 - 2*4 / 5) = xy5 + 2y + 4x // (315 / 5) = xy5 + 2y + 4x // 63 = xy5 + 2y + 4x // 21 * 3 = xy5 + 2y + 4x // 3*7*3 = xy5 + 2y + 4x // 3*7*3 = 3y5 + 2y + 4*3 // 3*7*3 = 15y + 2y + 12 // 3*7*3 - 12 = y(15 + 2) // 3*7*3 - 3*4 = y(15 + 2) // 51 = 3 * 17 // (323 - 1*3 / 5) = xy5 + 1y + 3x // (320 / 5) = xy5 + 1y + 3x // 64 = xy5 + 1y + 3x // 64 - 3x = xy5 + 1y // 64 - 3x = y(x5 + 1) // 64 - 3x = y(x5 + 1) 1 = y(x5 + 1) mod 3 */ Left.Copy( Product ); Temp.SetFromULong( A * C ); IntMath.Subtract( Left, Temp ); IntMath.Divide( Left, B, Quotient, Remainder ); if( !Remainder.IsZero()) throw( new Exception( "Remainder is not zero for Left." )); Left.Copy( Quotient ); // Worker.ReportProgress( 0, "Left: " + IntMath.ToString10( Left )); // Worker.ReportProgress( 0, "A: " + A.ToString() + " C: " + C.ToString()); FindFactors1.FindSmallPrimeFactorsOnly( Left ); FindFactors1.ShowAllFactors(); MaxX.Copy( ProductSqrRoot ); Temp.SetFromULong( A ); if( MaxX.ParamIsGreater( Temp )) return; // MaxX would be less than zero. IntMath.Subtract( MaxX, Temp ); IntMath.Divide( MaxX, B, Quotient, Remainder ); MaxX.Copy( Quotient ); // Worker.ReportProgress( 0, "MaxX: " + IntMath.ToString10( MaxX )); Temp.Copy( MaxX ); IntMath.MultiplyULong( Temp, C ); if( Left.ParamIsGreater( Temp )) { throw( new Exception( "Does this happen? MaxX can't be that big." )); /* Worker.ReportProgress( 0, "MaxX can't be that big." ); MaxX.Copy( Left ); Temp.SetFromULong( C ); IntMath.Divide( MaxX, Temp, Quotient, Remainder ); MaxX.Copy( Quotient ); Worker.ReportProgress( 0, "MaxX was set to: " + IntMath.ToString10( MaxX )); */ } // P = (xB + a)(yB + c) // P = (xB + a)(yB + c) // P - ac = xyBB + ayB + xBc // ((P - ac) / B) = xyB + ay + xc // ((P - ac) / B) = y(xB + a) + xc // This is congruent to zero mod one really big prime. // ((P - ac) / B) - xc = y(xB + a) // BottomPart is when x is at max in: // ((P - ac) / B) - xc Integer BottomPart = new Integer(); BottomPart.Copy( Left ); Temp.Copy( MaxX ); IntMath.MultiplyULong( Temp, C ); IntMath.Subtract( BottomPart, Temp ); if( BottomPart.IsNegative ) throw( new Exception( "Bug. BottomPart is negative." )); // Worker.ReportProgress( 0, "BottomPart: " + IntMath.ToString10( BottomPart )); Integer Gcd = new Integer(); Temp.SetFromULong( C ); IntMath.GreatestCommonDivisor( BottomPart, Temp, Gcd ); if( !Gcd.IsOne()) throw( new Exception( "This can't happen with the GCD." )); // FindFactors1.FindSmallPrimeFactorsOnly( BottomPart ); // Temp.SetFromULong( C ); // FindFactors1.FindSmallPrimeFactorsOnly( Temp ); // FindFactors1.ShowAllFactors(); MakeXYRecArray( Left, B, A, C ); FindXTheHardWay( B, Temp, A ); }
internal void MakeRSAKeys() { int ShowBits = (PrimeIndex + 1) * 32; // int TestLoops = 0; Worker.ReportProgress( 0, "Making RSA keys." ); Worker.ReportProgress( 0, "Bits size is: " + ShowBits.ToString()); // ulong Loops = 0; while( true ) { if( Worker.CancellationPending ) return; Thread.Sleep( 1 ); // Give up the time slice. Let other things on the server run. // Make two prime factors. // Normally you'd only make new primes when you pay the Certificate // Authority for a new certificate. if( !MakeAPrime( PrimeP, PrimeIndex, 20 )) return; IntegerBase TestP = new IntegerBase(); IntegerBaseMath IntBaseMath = new IntegerBaseMath( IntMath ); string TestS = IntMath.ToString10( PrimeP ); IntBaseMath.SetFromString( TestP, TestS ); string TestS2 = IntBaseMath.ToString10( TestP ); if( TestS != TestS2 ) throw( new Exception( "TestS != TestS2 for IntegerBase." )); if( Worker.CancellationPending ) return; if( !MakeAPrime( PrimeQ, PrimeIndex, 20 )) return; if( Worker.CancellationPending ) return; // This is extremely unlikely. Integer Gcd = new Integer(); IntMath.GreatestCommonDivisor( PrimeP, PrimeQ, Gcd ); if( !Gcd.IsOne()) { Worker.ReportProgress( 0, "They had a GCD: " + IntMath.ToString10( Gcd )); continue; } if( Worker.CancellationPending ) return; // This would never happen since the public key exponent used here // is one of the small primes in the array in IntegerMath that it // was checked against. But it does show here in the code that // they have to be co-prime to each other. And in the future it // might be found that the public key exponent has to be much larger // than the one used here. IntMath.GreatestCommonDivisor( PrimeP, PubKeyExponent, Gcd ); if( !Gcd.IsOne()) { Worker.ReportProgress( 0, "They had a GCD with PubKeyExponent: " + IntMath.ToString10( Gcd )); continue; } if( Worker.CancellationPending ) return; IntMath.GreatestCommonDivisor( PrimeQ, PubKeyExponent, Gcd ); if( !Gcd.IsOne()) { Worker.ReportProgress( 0, "2) They had a GCD with PubKeyExponent: " + IntMath.ToString10( Gcd )); continue; } // For Modular Reduction. This only has to be done // once, when P and Q are made. IntMathNewForP.SetupGeneralBaseArray( PrimeP ); IntMathNewForQ.SetupGeneralBaseArray( PrimeQ ); PrimePMinus1.Copy( PrimeP ); IntMath.SubtractULong( PrimePMinus1, 1 ); PrimeQMinus1.Copy( PrimeQ ); IntMath.SubtractULong( PrimeQMinus1, 1 ); // These checks should be more thorough. if( Worker.CancellationPending ) return; Worker.ReportProgress( 0, "The Index of Prime P is: " + PrimeP.GetIndex().ToString() ); Worker.ReportProgress( 0, "Prime P:" ); Worker.ReportProgress( 0, IntMath.ToString10( PrimeP )); Worker.ReportProgress( 0, " " ); Worker.ReportProgress( 0, "Prime Q:" ); Worker.ReportProgress( 0, IntMath.ToString10( PrimeQ )); Worker.ReportProgress( 0, " " ); PubKeyN.Copy( PrimeP ); IntMath.Multiply( PubKeyN, PrimeQ ); Worker.ReportProgress( 0, " " ); Worker.ReportProgress( 0, "PubKeyN:" ); Worker.ReportProgress( 0, IntMath.ToString10( PubKeyN )); Worker.ReportProgress( 0, " " ); // Euler's Theorem: // https://en.wikipedia.org/wiki/Euler's_theorem // if x ≡ y (mod φ(n)), // then a^x ≡ a^y (mod n). // Euler's Phi function (aka Euler's Totient function) is calculated // next. // PhiN is made from the two factors: (P - 1)(Q - 1) // PhiN is: (P - 1)(Q - 1) = PQ - P - Q + 1 // If I add (P - 1) to PhiN I get: // PQ - P - Q + 1 + (P - 1) = PQ - Q. // If I add (Q - 1) to that I get: // PQ - Q + (Q - 1) = PQ - 1. // (P - 1)(Q - 1) + (P - 1) + (Q - 1) = PQ - 1 // If (P - 1) and (Q - 1) had a larger GCD then PQ - 1 would have // that same factor too. IntMath.GreatestCommonDivisor( PrimePMinus1, PrimeQMinus1, Gcd ); Worker.ReportProgress( 0, "GCD of PrimePMinus1, PrimeQMinus1 is: " + IntMath.ToString10( Gcd )); if( !Gcd.IsULong()) { Worker.ReportProgress( 0, "This GCD number is too big: " + IntMath.ToString10( Gcd )); continue; } else { ulong TooBig = Gcd.GetAsULong(); // How big of a GCD is too big? if( TooBig > 1234567 ) { // (P - 1)(Q - 1) + (P - 1) + (Q - 1) = PQ - 1 Worker.ReportProgress( 0, "This GCD number is bigger than 1234567: " + IntMath.ToString10( Gcd )); continue; } } Integer Temp1 = new Integer(); PhiN.Copy( PrimePMinus1 ); Temp1.Copy( PrimeQMinus1 ); IntMath.Multiply( PhiN, Temp1 ); Worker.ReportProgress( 0, " " ); Worker.ReportProgress( 0, "PhiN:" ); Worker.ReportProgress( 0, IntMath.ToString10( PhiN )); Worker.ReportProgress( 0, " " ); if( Worker.CancellationPending ) return; // In RFC 2437 there are commonly used letters/symbols to represent // the numbers used. So the number e is the public exponent. // The number e that is used here is called PubKeyExponentUint = 65537. // In the RFC the private key d is the multiplicative inverse of // e mod PhiN. Which is mod (P - 1)(Q - 1). It's called // PrivKInverseExponent here. if( !IntMath.IntMathNew.FindMultiplicativeInverseSmall( PrivKInverseExponent, PubKeyExponent, PhiN, Worker )) return; if( PrivKInverseExponent.IsZero()) continue; Worker.ReportProgress( 0, " " ); Worker.ReportProgress( 0, "PrivKInverseExponent: " + IntMath.ToString10( PrivKInverseExponent )); if( Worker.CancellationPending ) return; // In RFC 2437 it defines a number dP which is the multiplicative // inverse, mod (P - 1) of e. That dP is named PrivKInverseExponentDP here. Worker.ReportProgress( 0, " " ); if( !IntMath.IntMathNew.FindMultiplicativeInverseSmall( PrivKInverseExponentDP, PubKeyExponent, PrimePMinus1, Worker )) return; Worker.ReportProgress( 0, " " ); Worker.ReportProgress( 0, "PrivKInverseExponentDP: " + IntMath.ToString10( PrivKInverseExponentDP )); if( PrivKInverseExponentDP.IsZero()) continue; // PrivKInverseExponentDP is PrivKInverseExponent mod PrimePMinus1. Integer Test1 = new Integer(); Test1.Copy( PrivKInverseExponent ); IntMath.Divide( Test1, PrimePMinus1, Quotient, Remainder ); Test1.Copy( Remainder ); if( !Test1.IsEqual( PrivKInverseExponentDP )) throw( new Exception( "Bug. This does not match the definition of PrivKInverseExponentDP." )); if( Worker.CancellationPending ) return; // In RFC 2437 it defines a number dQ which is the multiplicative // inverse, mod (Q - 1) of e. That dQ is named PrivKInverseExponentDQ here. Worker.ReportProgress( 0, " " ); if( !IntMath.IntMathNew.FindMultiplicativeInverseSmall( PrivKInverseExponentDQ, PubKeyExponent, PrimeQMinus1, Worker )) return; if( PrivKInverseExponentDQ.IsZero()) continue; Worker.ReportProgress( 0, " " ); Worker.ReportProgress( 0, "PrivKInverseExponentDQ: " + IntMath.ToString10( PrivKInverseExponentDQ )); if( Worker.CancellationPending ) return; Test1.Copy( PrivKInverseExponent ); IntMath.Divide( Test1, PrimeQMinus1, Quotient, Remainder ); Test1.Copy( Remainder ); if( !Test1.IsEqual( PrivKInverseExponentDQ )) throw( new Exception( "Bug. This does not match the definition of PrivKInverseExponentDQ." )); // Make a random number to test encryption/decryption. Integer ToEncrypt = new Integer(); int HowManyBytes = PrimeIndex * 4; byte[] RandBytes = MakeRandomBytes( HowManyBytes ); if( RandBytes == null ) { Worker.ReportProgress( 0, "Error making random bytes in MakeRSAKeys()." ); return; } if( !ToEncrypt.MakeRandomOdd( PrimeIndex - 1, RandBytes )) { Worker.ReportProgress( 0, "Error making random number ToEncrypt." ); return; } Integer PlainTextNumber = new Integer(); PlainTextNumber.Copy( ToEncrypt ); Worker.ReportProgress( 0, " " ); Worker.ReportProgress( 0, "Before encrypting number: " + IntMath.ToString10( ToEncrypt )); Worker.ReportProgress( 0, " " ); IntMath.IntMathNew.ModularPower( ToEncrypt, PubKeyExponent, PubKeyN, false ); if( Worker.CancellationPending ) return; Worker.ReportProgress( 0, IntMath.GetStatusString() ); Integer CipherTextNumber = new Integer(); CipherTextNumber.Copy( ToEncrypt ); Worker.ReportProgress( 0, " " ); Worker.ReportProgress( 0, "Encrypted number: " + IntMath.ToString10( CipherTextNumber )); Worker.ReportProgress( 0, " " ); ECTime DecryptTime = new ECTime(); DecryptTime.SetToNow(); IntMath.IntMathNew.ModularPower( ToEncrypt, PrivKInverseExponent, PubKeyN, false ); Worker.ReportProgress( 0, "Decrypted number: " + IntMath.ToString10( ToEncrypt )); if( !PlainTextNumber.IsEqual( ToEncrypt )) { throw( new Exception( "PlainTextNumber not equal to unencrypted value." )); // Because P or Q wasn't really a prime? // Worker.ReportProgress( 0, "PlainTextNumber not equal to unencrypted value." ); // continue; } Worker.ReportProgress( 0, " " ); Worker.ReportProgress( 0, "Decrypt time seconds: " + DecryptTime.GetSecondsToNow().ToString( "N2" )); Worker.ReportProgress( 0, " " ); if( Worker.CancellationPending ) return; // Test the standard optimized way of decrypting: if( !ToEncrypt.MakeRandomOdd( PrimeIndex - 1, RandBytes )) { Worker.ReportProgress( 0, "Error making random number in MakeRSAKeys()." ); return; } PlainTextNumber.Copy( ToEncrypt ); IntMath.IntMathNew.ModularPower( ToEncrypt, PubKeyExponent, PubKeyN, false ); if( Worker.CancellationPending ) return; CipherTextNumber.Copy( ToEncrypt ); // QInv is the multiplicative inverse of PrimeQ mod PrimeP. if( !IntMath.MultiplicativeInverse( PrimeQ, PrimeP, QInv, Worker )) throw( new Exception( "MultiplicativeInverse() returned false." )); if( QInv.IsNegative ) throw( new Exception( "This is a bug. QInv is negative." )); Worker.ReportProgress( 0, "QInv is: " + IntMath.ToString10( QInv )); DecryptWithQInverse( CipherTextNumber, ToEncrypt, // Decrypt it to this. PlainTextNumber, // Test it against this. PubKeyN, PrivKInverseExponentDP, PrivKInverseExponentDQ, PrimeP, PrimeQ, Worker ); Worker.ReportProgress( 0, " " ); Worker.ReportProgress( 0, "Found the values:" ); Worker.ReportProgress( 0, "Seconds: " + StartTime.GetSecondsToNow().ToString( "N0" )); Worker.ReportProgress( 0, " " ); Worker.ReportProgress( 1, "Prime1: " + IntMath.ToString10( PrimeP )); Worker.ReportProgress( 0, " " ); Worker.ReportProgress( 1, "Prime2: " + IntMath.ToString10( PrimeQ )); Worker.ReportProgress( 0, " " ); Worker.ReportProgress( 1, "PubKeyN: " + IntMath.ToString10( PubKeyN )); Worker.ReportProgress( 0, " " ); Worker.ReportProgress( 1, "PrivKInverseExponent: " + IntMath.ToString10( PrivKInverseExponent )); /* Worker.ReportProgress( 0, " " ); Worker.ReportProgress( 0, " " ); Worker.ReportProgress( 0, " " ); DoCRTTest( PrivKInverseExponent ); Worker.ReportProgress( 0, "Finished CRT test." ); Worker.ReportProgress( 0, " " ); */ return; // Comment this out to just leave it while( true ) for testing. } }
private bool GetFactors( Integer Y, ExponentVectorNumber XExp ) { Integer XRoot = new Integer(); Integer X = new Integer(); Integer XwithY = new Integer(); Integer Gcd = new Integer(); XExp.GetTraditionalInteger( X ); if( !IntMath.SquareRoot( X, XRoot )) throw( new Exception( "Bug. X should have an exact square root." )); XwithY.Copy( Y ); XwithY.Add( XRoot ); IntMath.GreatestCommonDivisor( Product, XwithY, Gcd ); if( !Gcd.IsOne()) { if( !Gcd.IsEqual( Product )) { SolutionP.Copy( Gcd ); IntMath.Divide( Product, SolutionP, Quotient, Remainder ); if( !Remainder.IsZero()) throw( new Exception( "The Remainder with SolutionP can't be zero." )); SolutionQ.Copy( Quotient ); MForm.ShowStatus( "SolutionP: " + IntMath.ToString10( SolutionP )); MForm.ShowStatus( "SolutionQ: " + IntMath.ToString10( SolutionQ )); return true; } else { MForm.ShowStatus( "GCD was Product." ); } } else { MForm.ShowStatus( "GCD was one." ); } MForm.ShowStatus( "XRoot: " + IntMath.ToString10( XRoot )); MForm.ShowStatus( "Y: " + IntMath.ToString10( Y )); XwithY.Copy( Y ); if( Y.ParamIsGreater( XRoot )) throw( new Exception( "This can't be right. XRoot is bigger than Y." )); IntMath.Subtract( Y, XRoot ); IntMath.GreatestCommonDivisor( Product, XwithY, Gcd ); if( !Gcd.IsOne()) { if( !Gcd.IsEqual( Product )) { SolutionP.Copy( Gcd ); IntMath.Divide( Product, SolutionP, Quotient, Remainder ); if( !Remainder.IsZero()) throw( new Exception( "The Remainder with SolutionP can't be zero." )); SolutionQ.Copy( Quotient ); MForm.ShowStatus( "SolutionP: " + IntMath.ToString10( SolutionP )); MForm.ShowStatus( "SolutionQ: " + IntMath.ToString10( SolutionQ )); return true; } else { MForm.ShowStatus( "GCD was Product." ); } } else { MForm.ShowStatus( "GCD was one." ); } return false; }