private static string FromByteArray(byte[] ba) { BitcoinKit.BouncyCastle.Math.BigInteger addrremain = new BitcoinKit.BouncyCastle.Math.BigInteger(1, ba); BitcoinKit.BouncyCastle.Math.BigInteger big0 = new BitcoinKit.BouncyCastle.Math.BigInteger("0"); BitcoinKit.BouncyCastle.Math.BigInteger big58 = new BitcoinKit.BouncyCastle.Math.BigInteger("58"); string b58 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"; string rv = ""; while (addrremain.CompareTo(big0) > 0) { int d = System.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; }
private static byte[] ToByteArray(string base58) { BitcoinKit.BouncyCastle.Math.BigInteger bi2 = new BitcoinKit.BouncyCastle.Math.BigInteger("0"); string b58 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"; foreach (char c in base58) { if (b58.IndexOf(c) != -1) { bi2 = bi2.Multiply(new BitcoinKit.BouncyCastle.Math.BigInteger("58")); bi2 = bi2.Add(new BitcoinKit.BouncyCastle.Math.BigInteger(b58.IndexOf(c).ToString())); } else { return null; } } byte[] bb = bi2.ToByteArrayUnsigned(); // interpret leading '1's as leading zero bytes foreach (char c in base58) { if (c != '1') break; byte[] bbb = new byte[bb.Length + 1]; Array.Copy(bb, 0, bbb, 1, bb.Length); bb = bbb; } return bb; }