Exemple #1
0
        private static ECDsa LoadPrivateKey(byte[] key)
        {
            BigInteger     privKeyInt = new BigInteger(+1, key);
            X9ECParameters parameters = SecNamedCurves.GetByName("secp256k1");
            ECPoint        ecPoint    = parameters.G.Multiply(privKeyInt);

            byte[] privKeyX = ecPoint.Normalize().XCoord.ToBigInteger().ToByteArrayUnsigned();
            byte[] privKeyY = ecPoint.Normalize().YCoord.ToBigInteger().ToByteArrayUnsigned();

            return(ECDsa.Create(new ECParameters
            {
                Curve = ECCurve.CreateFromFriendlyName("secp256k1"),
                D = privKeyInt.ToByteArrayUnsigned(),
                Q = new System.Security.Cryptography.ECPoint
                {
                    X = privKeyX,
                    Y = privKeyY
                }
            }));
        }
Exemple #2
0
        private string PrivateKeyHexToPublicKeyHex(string privateKeyHex)
        {
            byte[] privateKeyBytes = this.ValidateAndGetPrivateKeyBytes(privateKeyHex, 0x00);
            if (privateKeyBytes == null)
            {
                return(null);
            }

            X9ECParameters curves            = SecNamedCurves.GetByName("secp256k1");
            BigInteger     privateKeyInteger = new BigInteger(privateKeyBytes);
            ECPoint        p = curves.G.Multiply(privateKeyInteger);

            byte[] publicAddress = new byte[65];
            byte[] y             = p.Normalize().YCoord.ToBigInteger().ToByteArray();
            Array.Copy(y, 0, publicAddress, 64 - y.Length + 1, y.Length);
            byte[] x = p.Normalize().XCoord.ToBigInteger().ToByteArray();
            Array.Copy(x, 0, publicAddress, 32 - x.Length + 1, x.Length);
            publicAddress[0] = 4;

            return(this.ByteArrayToString(publicAddress));
        }