WireGuard Private and Public Key Generator

729 views Asked by At

Is it possible to generate both a private and public key for WireGuard using C#?

It needs to be in Base64 format, as far as I understand, correct?
I need these keys later to send them to a firewall via SSH.

If yes, how?

Regards, Steffen

1

There are 1 answers

0
mattpr On

Well, in pure C#, that would mean rewriting crypto libraries by hand which no one in their right mind should be doing unless that is their line of work.

So what you are looking for are libraries that you can call from C# to do what you want. Here are the various wireguard libraries and source.

If you are building for Windows, you might want the embeddable-dll-service or WireGuardNT. Details here.

Or if you can interact with openssl from C#, you can use openssl itself to generate valid keys apparently...

This reddit post mentions how to generate wg private/pub key using openssl only (I haven't tested it, ymmv).

openssl genpkey -algorithm X25519 -outform der -out privatekey.der

openssl pkey -inform der -in privatekey.der -pubout -outform der -out pubkey.der

cat pubkey.der | tail -c 32 | base64 > pubkey.txt

cat privatekey.der | tail -c 32 | base64 > privatekey.txt