Exemplo n.º 1
0
    public static void ECPrivateKeyToPublicKey(
        BigInteger privateKey,
        out BigInteger publicKeyX,
        out BigInteger publicKeyY)
    {
        if (!EllipticCurve.IsPrivateKeyValid(privateKey))
        {
            throw new Exception("privateKey is invalid.");
        }

        var parameters = Org.BouncyCastle.Asn1.Sec.SecNamedCurves.GetByName("secp256k1");
        var d          = new Org.BouncyCastle.Math.BigInteger(privateKey.ToString());
        var Q          = parameters.G.Multiply(d).Normalize();

        publicKeyX = BigInteger.Parse(Q.XCoord.ToBigInteger().ToString());
        publicKeyY = BigInteger.Parse(Q.YCoord.ToBigInteger().ToString());
    }
Exemplo n.º 2
0
    public static BigInteger PasswordToPrivateKey(string password)
    {
        // change salt for improved security
        // reference https://en.wikipedia.org/wiki/Key_stretching
        // reference https://en.wikipedia.org/wiki/Salt_(cryptography)
        string salt = "Cryptography";

        var pbkdf2 = new Rfc2898DeriveBytes(
            Encoding.UTF8.GetBytes(password),
            Encoding.UTF8.GetBytes(salt),
            4096);

        BigInteger privateKey = Encode.BytesToBigInteger(pbkdf2.GetBytes(32));

        if (!EllipticCurve.IsPrivateKeyValid(privateKey))
        {
            throw new Exception("privateKey is invalid.");              // extreme low probability to happen
        }
        return(privateKey);
    }