I generated a .pem file that contains Diffie-Hellman parameters. I used this command line:
openssl dhparam -outform PEM 2048 -out dhparam.pem
The file looks like this:
-----BEGIN DH PARAMETERS-----
MIIBCAKCAQEA6gS7LGwOkRMfJJX2sBK+NRwSL1OaegjVeEh+FJJbWRLG7pB9W7JX
[4 lines omitted]
7LyTYZEvaaAK27xuf4uo4YCFnaOkxp/R6wIBAg==
-----END DH PARAMETERS-----
How can I extract the Generator and Prime values from this file using .Net Framework code?
Note that there is an answer How to extract DH parameters from a DH key in PEM format for how to accomplish this task on the command line.
For .NET Framework it is most convenient to retrieve the data using BouncyCastle's
Utilities.IO.Pem.PemReaderand ASN.1 parser:The data can be checked with an ASN.1 online parser, e.g. https://lapo.it/asn1js:
Edit: As of .NET 5, e.g. the native classes
PemEncodingandAsnReadercan be used, so BouncyCastle is no longer needed: