I'm trying to use secp256k1 eliptic curve to get the public key from private key but it gives me an unknown error error
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.