dotnet / dotnet/dotnet-api-docs

Example uses Obsolete API

Open
#11,723 2 comments 0 reactions 0 assignees View on GitHub
area-System.Security untriaged
Dominant language
C#
Stars
949
Forks
1.7k
Avg merge
3d 27m
Merged PRs (30d)
49

Description

### Type of issue

Outdated article

### Description

The sample code provided includes:
```csharp
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:
```csharp
var alicePublicKey = (alice.PublicKey as ECDiffieHellmanCngPublicKey)!.Import().Export(CngKeyBlobFormat.EccFullPublicBlob);
```
and then
```csharp
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

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.