Bitcoin Forum

Bitcoin => Development & Technical Discussion => Topic started by: PrettyGirl741963 on February 25, 2020, 10:08:26 AM



Title: private key to public key generation bitcoin vb.net gives unknown error
Post by: PrettyGirl741963 on February 25, 2020, 10:08:26 AM
I'm trying to use secp256k1 eliptic curve to get the public key from private key but it gives me an unknown error error

Quote
Private Sub Button13_Click(sender As Object, e As EventArgs) Handles Button13.Click
    Dim private_key = "68040878110175628235481263019639686"
    'public key should be Nr6MbFUfMovKCX4vd5YpQnRYsN4rq6pNPEEBKmicAEwwuYLpJrt5LsRvfvR2G8pJ5rMchEMWDYJ7rdY GY7PjxHEa
    Dim public_key As String


    Using eliptic As New ECDsaCng()
        eliptic.HashAlgorithm = CngAlgorithm.ECDsaP256

        Dim data() As Byte = Encoding.UTF8.GetBytes(private_key)
        Dim key As Byte() = eliptic.SignData(data)
        public_key = key.ToString
    End Using

    TextBox15.Text = public_key
End Sub


Title: Re: private key to public key generation bitcoin vb.net gives unknown error
Post by: odolvlobo on February 25, 2020, 08:39:11 PM
I'm trying to use secp256k1 eliptic curve to get the public key from private key but it gives me an unknown error error

Quote
Private Sub Button13_Click(sender As Object, e As EventArgs) Handles Button13.Click
    Dim private_key = "68040878110175628235481263019639686"
    'public key should be Nr6MbFUfMovKCX4vd5YpQnRYsN4rq6pNPEEBKmicAEwwuYLpJrt5LsRvfvR2G8pJ5rMchEMWDYJ7rdY GY7PjxHEa
    Dim public_key As String


    Using eliptic As New ECDsaCng()
        eliptic.HashAlgorithm = CngAlgorithm.ECDsaP256

        Dim data() As Byte = Encoding.UTF8.GetBytes(private_key)
        Dim key As Byte() = eliptic.SignData(data)
        public_key = key.ToString
    End Using

    TextBox15.Text = public_key
End Sub

I'm not an expert, but after looking at the docs (https://docs.microsoft.com/en-us/dotnet/api/system.security.cryptography.ecdsacng?view=netframework-4.8), I think I see several problems:

1. New ECDsaCng() initializes an instance with a random key pair.
2. ECDsaCng.SignData() does not return a public key. It signs a message. In this case it is signing the message "68040878110175628235481263019639686" ad returning the signatre.
3. Encoding.UTF8.GetBytes() does not covert a string to a number.
4. Byte.ToString() does not encode data using base-64

To extract the public key from ECDsaCng (once it is initialized correctly), I believe you can use ECDsaCng.Key.Export(). However, I can't figure out how to import the private key in the first place. Perhaps, ECDsaCng is not appropriate for what you are trying to do. Generating the public key is straightforward, but I don't know how to do it in .NET.


Title: Re: private key to public key generation bitcoin vb.net gives unknown error
Post by: pooya87 on February 26, 2020, 03:46:05 AM
additionally the elliptic curve that you are using is called secp256r1 (not ...k1) which is a different curve than what bitcoin uses so your key pairs should also be different and invalid in bitcoin.
i am assuming you are just learning things but be very careful if you want to use whatever you are making with real funds. it is also best if you find a bitcoin library and use that instead of .net framework.