public static string ByteArrayToBase58(byte[] ba) { Org.BouncyCastle.Math.BigInteger addrremain = new Org.BouncyCastle.Math.BigInteger(1, ba); Org.BouncyCastle.Math.BigInteger big0 = new Org.BouncyCastle.Math.BigInteger("0"); Org.BouncyCastle.Math.BigInteger big58 = new Org.BouncyCastle.Math.BigInteger("58"); string b58 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"; string rv = ""; while (addrremain.CompareTo(big0) > 0) { int d = Convert.ToInt32(addrremain.Mod(big58).ToString()); addrremain = addrremain.Divide(big58); rv = b58.Substring(d, 1) + rv; } // handle leading zeroes foreach (byte b in ba) { if (b != 0) { break; } rv = "1" + rv; } return(rv); }
public AsymmetricCipherKeyPair BuildKey(byte[] seed, byte[] payload) { var publicExponent = new BigInteger("10001", 16); var keygen = new RsaKeyPairGenerator(); keygen.Init(new RsaKeyGenerationParameters(publicExponent, new SecureRandom(new SeededGenerator(seed)), 2048, 80)); var pair = keygen.GenerateKeyPair(); var paramz = ((RsaPrivateCrtKeyParameters) pair.Private); var modulus = paramz.Modulus.ToByteArray(); Replace(modulus, payload, 80); var p = paramz.P; var n = new BigInteger(modulus); var preQ = n.Divide(p); var q = preQ.NextProbablePrime(); return ComposeKeyPair(p, q, publicExponent); }
public string ToString( int radix) { // TODO Make this method work for other radices (ideally 2 <= radix <= 16) switch (radix) { case 2: case 10: case 16: break; default: throw new FormatException("Only base 10 or 16 are allowed"); } // NB: Can only happen to internally managed instances if (magnitude == null) return "null"; if (sign == 0) return "0"; Debug.Assert(magnitude.Length > 0); StringBuilder sb = new StringBuilder(); if (radix == 16) { sb.Append(magnitude[0].ToString("x")); for (int i = 1; i < magnitude.Length; i++) { sb.Append(magnitude[i].ToString("x8")); } } else if (radix == 2) { for (int i = BitLength - 1; i >= 0; --i) { sb.Append(TestBit(i) ? '1' : '0'); } } else { // This is algorithm 1a from chapter 4.4 in Seminumerical Algorithms, slow but it works Stack S = new Stack(); BigInteger bs = ValueOf(radix); // The sign is handled separatly. // Notice however that for this to work, radix 16 _MUST_ be a special case, // unless we want to enter a recursion well. In their infinite wisdom, why did not // the Sun engineers made a c'tor for BigIntegers taking a BigInteger as parameter? // (Answer: Becuase Sun's BigIntger is clonable, something bouncycastle's isn't.) BigInteger u = new BigInteger(Abs().ToString(16), 16); BigInteger b; while (u.sign != 0) { b = u.Mod(bs); if (b.sign == 0) { S.Push("0"); } else { // see how to interact with different bases S.Push(b.magnitude[0].ToString("d")); } u = u.Divide(bs); } // Then pop the stack while (S.Count != 0) { sb.Append((string) S.Pop()); } } string s = sb.ToString(); Debug.Assert(s.Length > 0); // Strip leading zeros. (We know this number is not all zeroes though) if (s[0] == '0') { int nonZeroPos = 0; while (s[++nonZeroPos] == '0') {} s = s.Substring(nonZeroPos); } if (sign == -1) { s = "-" + s; } return s; }
public void TestDivide() { for (int i = -5; i <= 5; ++i) { try { val(i).Divide(zero); Assert.Fail("expected ArithmeticException"); } catch (ArithmeticException) {} } int product = 1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9; int productPlus = product + 1; BigInteger bigProduct = val(product); BigInteger bigProductPlus = val(productPlus); for (int divisor = 1; divisor < 10; ++divisor) { // Exact division BigInteger expected = val(product / divisor); Assert.AreEqual(expected, bigProduct.Divide(val(divisor))); Assert.AreEqual(expected.Negate(), bigProduct.Negate().Divide(val(divisor))); Assert.AreEqual(expected.Negate(), bigProduct.Divide(val(divisor).Negate())); Assert.AreEqual(expected, bigProduct.Negate().Divide(val(divisor).Negate())); expected = val((product + 1)/divisor); Assert.AreEqual(expected, bigProductPlus.Divide(val(divisor))); Assert.AreEqual(expected.Negate(), bigProductPlus.Negate().Divide(val(divisor))); Assert.AreEqual(expected.Negate(), bigProductPlus.Divide(val(divisor).Negate())); Assert.AreEqual(expected, bigProductPlus.Negate().Divide(val(divisor).Negate())); } for (int rep = 0; rep < 10; ++rep) { BigInteger a = new BigInteger(100 - rep, 0, random); BigInteger b = new BigInteger(100 + rep, 0, random); BigInteger c = new BigInteger(10 + rep, 0, random); BigInteger d = a.Multiply(b).Add(c); BigInteger e = d.Divide(a); Assert.AreEqual(b, e); } // Special tests for power of two since uses different code path internally for (int i = 0; i < 100; ++i) { int shift = random.Next(64); BigInteger a = one.ShiftLeft(shift); BigInteger b = new BigInteger(64 + random.Next(64), random); BigInteger bShift = b.ShiftRight(shift); string data = "shift=" + shift +", b=" + b.ToString(16); Assert.AreEqual(bShift, b.Divide(a), data); Assert.AreEqual(bShift.Negate(), b.Divide(a.Negate()), data); Assert.AreEqual(bShift.Negate(), b.Negate().Divide(a), data); Assert.AreEqual(bShift, b.Negate().Divide(a.Negate()), data); } // Regression { int shift = 63; BigInteger a = one.ShiftLeft(shift); BigInteger b = new BigInteger(1, Hex.Decode("2504b470dc188499")); BigInteger bShift = b.ShiftRight(shift); string data = "shift=" + shift +", b=" + b.ToString(16); Assert.AreEqual(bShift, b.Divide(a), data); Assert.AreEqual(bShift.Negate(), b.Divide(a.Negate()), data); // Assert.AreEqual(bShift.Negate(), b.Negate().Divide(a), data); Assert.AreEqual(bShift, b.Negate().Divide(a.Negate()), data); } }