Skip to content

Instantly share code, notes, and snippets.

@FrankSpierings
Last active December 15, 2020 20:52
Show Gist options
  • Save FrankSpierings/a5af505068073feea0ae to your computer and use it in GitHub Desktop.
Save FrankSpierings/a5af505068073feea0ae to your computer and use it in GitHub Desktop.
Powershell ECDiffieHellmanP256 Example
[System.Security.Cryptography.CngKey]$aliceKey = [System.Security.Cryptography.CngKey]::Create([System.Security.Cryptography.CngAlgorithm]::ECDiffieHellmanP256)
[System.Security.Cryptography.CngKey]$bobKey = [System.Security.Cryptography.CngKey]::Create([System.Security.Cryptography.CngAlgorithm]::ECDiffieHellmanP256)
[Byte[]]$alicePubKeyBlob = $aliceKey.Export([System.Security.Cryptography.CngKeyBlobFormat]::EccPublicBlob)
[Byte[]]$bobPubKeyBlob = $bobKey.Export([System.Security.Cryptography.CngKeyBlobFormat]::EccPublicBlob)
[System.Security.Cryptography.ECDiffieHellmanCng]$aliceAlgorithm = New-Object System.Security.Cryptography.ECDiffieHellmanCng($aliceKey)
[System.Security.Cryptography.CngKey]$bobPubKey = [System.Security.Cryptography.CngKey]::Import($bobPubKeyBlob, [System.Security.Cryptography.CngKeyBlobFormat]::EccPublicBlob)
[Byte[]]$aliceSymKey = $aliceAlgorithm.DeriveKeyMaterial($bobPubKey)
[System.Security.Cryptography.ECDiffieHellmanCng]$bobAlgorithm = New-Object System.Security.Cryptography.ECDiffieHellmanCng($bobKey)
[System.Security.Cryptography.CngKey]$alicePubKey = [System.Security.Cryptography.CngKey]::Import($alicePubKeyBlob, [System.Security.Cryptography.CngKeyBlobFormat]::EccPublicBlob)
[Byte[]]$bobSymKey = $bobAlgorithm.DeriveKeyMaterial($alicePubKey)
Write-Host $("Alice has: {0}" -f [Convert]::ToBase64String($aliceSymKey))
Write-Host $("Bob has: {0}" -f [Convert]::ToBase64String($bobSymKey))
@ciis0
Copy link

ciis0 commented Dec 15, 2020

I have wrapped this into a nice script for execution on two different machines. :)
https://gist.github.com/ciis0/d631e5526fab765ab1bc99a5467d05d2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment