dotnet-api-docs icon indicating copy to clipboard operation
dotnet-api-docs copied to clipboard

Example uses Obsolete API

Open bcdev-com opened this issue 4 months ago • 2 comments

Type of issue

Outdated article

Description

The sample code provided includes:

alicePublicKey = alice.PublicKey.ToByteArray();

but ECDiffieHellmanPublicKey.ToByteArray() is marked as Obsolete in current versions of dotnet, and what it produces throws when passed to CngKey.Import, at least for Curve25519. The recommended replacement, ECDiffieHellmanPublicKey.ExportSubjectPublicKeyInfo() also throws for Curve25519.

The least bad replacement I could figure out is the very unsatisfying:

var alicePublicKey = (alice.PublicKey as ECDiffieHellmanCngPublicKey)!.Import().Export(CngKeyBlobFormat.EccFullPublicBlob);

and then

CngKey.Import(alicePublicKey, CngKeyBlobFormat.EccFullPublicBlob)

to create something that can be used with DeriveKeyMaterial() et al.

Page URL

https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.ecdiffiehellmancng?view=net-9.0

Content source URL

https://github.com/dotnet/dotnet-api-docs/blob/main/xml/System.Security.Cryptography/ECDiffieHellmanCng.xml

Document Version Independent Id

f761f563-a2d2-32f8-f34e-3e88927076e4

Platform Id

6403fb7b-63f1-302c-9c2a-5c3f84f63994

Article author

@dotnet-bot

bcdev-com avatar Aug 22 '25 05:08 bcdev-com