jpassing / dotnet-crypto-utils

Apache License 2.0
10 stars 4 forks source link

Can this be extended for ECC? #3

Open sun2sirius opened 5 months ago

jpassing commented 5 months ago

I've been working on a more complete library, Jpki.Security.Cryptography which, among other things, includes .NET Framework equivalents for:

{ECDSA, RSA}.ExportSubjectPublicKeyInfo {ECDSA, RSA}.ExportSubjectPublicKeyInfoPem {ECDSA, RSA}.ImportSubjectPublicKeyInfo {ECDSA, RSA}.ImportFromPem

Does this cover what you're looing for?

sun2sirius commented 5 months ago

Hi Johannes, thanks for the quick response! I simply needed to export a public portion of a generated ECC key in PEM format. My code is still at 4.8 Framework and ECDsaCng only exports private portion in Pkcs8. I did not want to add BouncyCastle or any other heavy library for this, so I hacked it simply like this: using (ECDsaCng dsaProvider = new ECDsaCng(256)) { byte[] privPkcs8 = dsaProvider.Key.Export(CngKeyBlobFormat.Pkcs8PrivateBlob); byte[] pub = dsaProvider.Key.Export(CngKeyBlobFormat.EccPublicBlob); byte[] pubRaw = new byte[pub.Length - 8]; Array.Copy(pub, 8, pubRaw, 0, pubRaw.Length); byte[] derHdr = {0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04 }; byte[] pubPkcs8 = Misc.ConcatArrays(derHdr, pubRaw); string pubPem = Convert.ToBase64String(pubPkcs8); }

I know that doing it right would take more code, which ultimately would become a library, etc. I would probably want to include a full BLOB/PEM conversion in my app later and will have to resort to a library like yours.

On Wed, Mar 27, 2024 at 5:21 PM Johannes Passing @.***> wrote:

I've been working on a more complete library, Jpki.Security.Cryptography https://github.com/jpassing/jpki-net which, among other things, includes .NET Framework equivalents for:

{ECDSA, RSA}.ExportSubjectPublicKeyInfo https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.asymmetricalgorithm.exportsubjectpublickeyinfo?view=net-7.0 {ECDSA, RSA}.ExportSubjectPublicKeyInfoPem https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.asymmetricalgorithm.exportsubjectpublickeyinfopem?view=net-7.0 {ECDSA, RSA}.ImportSubjectPublicKeyInfo https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.asymmetricalgorithm.importsubjectpublickeyinfo?view=net-7.0 {ECDSA, RSA}.ImportFromPem https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.asymmetricalgorithm.importfrompem?view=net-7.0

Does this cover what you're looing for?

— Reply to this email directly, view it on GitHub https://github.com/jpassing/dotnet-crypto-utils/issues/3#issuecomment-2024189683, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADEFXQSGWRLWUQC6QSLBC7DY2NO6JAVCNFSM6AAAAABFL2HGVGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDAMRUGE4DSNRYGM . You are receiving this because you authored the thread.Message ID: @.***>