starkbank / ecdsa-dotnet

A lightweight and fast pure C# ECDSA library
https://starkbank.com
Other
49 stars 20 forks source link

got error when Sign: System.ArgumentException: 'expected '1' at start of DER private key, got 0' #26

Closed smartkodian closed 2 years ago

smartkodian commented 2 years ago

Hi,

I have this Private key:

-----BEGIN EC PRIVATE KEY----- MIGEAgEAMBAGByqGSM49AgEGBSuBBAAKBG0wawIBAQQgPUAdAJuELXoxEumrKUPd yHLP9bITTV+yOSw5q1H8W/2hRANCAASW6MSUA/wJRcj0AljN0tnpMBp5ISqTp8j/ rY7C2BXCXyy03V/lP7jn0LSgJvykVyNRPXfA4zjpFRaOUNWUBNuU -----END EC PRIVATE KEY-----

when I use it I got this error: System.ArgumentException: 'expected '1' at start of DER private key, got 0'

but Actually I used it for same library but in php and it works fine but in C# gives me that error!

Any help please?

smartkodian commented 2 years ago

I solved it by getting the PK from p12 file with this command: openssl pkcs12 -in test.p12 -nodes -nocerts | openssl ec -out OUTFILE.key

leandro-stark commented 2 years ago

Hi, @smartkodian!

It's important to understand that ECDSA PHP it's actually a tiny wrapper of OpenSSL, differently from other ECDSAs that have a native implementation. So it's expected that ECDSA Dotnet and ECDSA PHP work differently and have distinct behavior.

This error:

System.ArgumentException: 'expected '1' at start of DER private key, got 0'

It's returned when the private key that you are using it's not a valid or has a different format than expected.

As mentioned, the ECDSA PHP works in a distinct way from other and thats why it does not have this kind of validation.

If you try to import this private key in the other ECDSAs that have native implementation, all of them will return this error.

In this case, we recommend generating a new pair of keys and try to import again.

You can do it easily in 3 ways here.

If you have any questions, do not hesitate to ask!

Best,