public static PQPair parsePQ(byte[] pqStr) { if (pqStr.Length > 8) { return(null); } PQPair pPair = new PQPair(0, 0); byte[] pStr = new byte[4]; byte[] qStr = new byte[4]; UInt64 pq = 0, p, q; for (UInt32 i = 0, l = (UInt32)pqStr.Length; i < l; ++i) { pq <<= 8; pq |= (UInt64)pqStr[i]; } UInt64 pqSqrt = (UInt64)System.Math.Sqrt((double)pq), ySqr, y; while (pqSqrt * pqSqrt > pq) { --pqSqrt; } while (pqSqrt * pqSqrt < pq) { ++pqSqrt; } for (ySqr = pqSqrt * pqSqrt - pq; ; ++pqSqrt, ySqr = pqSqrt * pqSqrt - pq) { y = (UInt64)System.Math.Sqrt((double)ySqr); while (y * y > ySqr) { --y; } while (y * y < ySqr) { ++y; } if (ySqr == 0 || y + pqSqrt >= pq) { return(null); } if (y * y == ySqr) { p = pqSqrt + y; q = (pqSqrt > y) ? (pqSqrt - y) : (y - pqSqrt); break; } } if (p > q) { Swap(ref p, ref q); } for (UInt32 i = 0; i < 4; ++i) { pStr[3 - i] = (byte)(p & 0xFF); p >>= 8; } pPair.P = new Math.BigInteger(pStr); for (UInt32 i = 0; i < 4; ++i) { qStr[3 - i] = (byte)(q & 0xFF); q >>= 8; } pPair.Q = new Math.BigInteger(qStr); return(pPair); }
public static PQPair parsePQ(byte[] pqStr) { if (pqStr.Length > 8) { return null; } PQPair pPair = new PQPair(0, 0); byte[] pStr = new byte[4]; byte[] qStr = new byte[4]; UInt64 pq = 0, p, q; for (UInt32 i = 0, l = (UInt32)pqStr.Length; i < l; ++i) { pq <<= 8; pq |= (UInt64)pqStr[i]; } UInt64 pqSqrt = (UInt64)System.Math.Sqrt((double)pq), ySqr, y; while (pqSqrt * pqSqrt > pq) { --pqSqrt; } while (pqSqrt * pqSqrt < pq) { ++pqSqrt; } for (ySqr = pqSqrt * pqSqrt - pq; ; ++pqSqrt, ySqr = pqSqrt * pqSqrt - pq) { y = (UInt64)System.Math.Sqrt((double)ySqr); while (y * y > ySqr) { --y; } while (y * y < ySqr) { ++y; } if (ySqr == 0 || y + pqSqrt >= pq) { return null; } if (y * y == ySqr) { p = pqSqrt + y; q = (pqSqrt > y) ? (pqSqrt - y) : (y - pqSqrt); break; } } if (p > q) { Swap(ref p, ref q); } for (UInt32 i = 0; i < 4; ++i) { pStr[3 - i] = (byte)(p & 0xFF); p >>= 8; } pPair.P = new Math.BigInteger(pStr); for (UInt32 i = 0; i < 4; ++i) { qStr[3 - i] = (byte)(q & 0xFF); q >>= 8; } pPair.Q = new Math.BigInteger(qStr); return pPair; }