Skip to content

Instantly share code, notes, and snippets.

@NiclasOlofsson
Last active April 20, 2016 15:37
Show Gist options
  • Save NiclasOlofsson/c18573c7b54d921481a61e486a2dd77d to your computer and use it in GitHub Desktop.
Save NiclasOlofsson/c18573c7b54d921481a61e486a2dd77d to your computer and use it in GitHub Desktop.
ECDH key import
var clientKey = CreateEcDiffieHellmanPublicKey("MHYwEAYHKoZIzj0CAQYFK4EEACIDYgAEDEKneqEvcqUqqFMM1HM1A4zWjJC+I8Y+aKzG5dl+6wNOHHQ4NmG2PEXRJYhujyodFH+wO0dEr4GM1WoaWog8xsYQ6mQJAC0eVpBM96spUB1eMN56+BwlJ4H3Qx4TAvAs");
private ECDiffieHellmanPublicKey CreateEcDiffieHellmanPublicKey(string clientPubKeyString)
{
byte[] clientPublicKeyBlob = Base64Url.Decode(clientPubKeyString);
clientPublicKeyBlob = FixPublicKey(clientPublicKeyBlob.Skip(23).ToArray());
ECDiffieHellmanPublicKey clientKey = ECDiffieHellmanCngPublicKey.FromByteArray(clientPublicKeyBlob, CngKeyBlobFormat.EccPublicBlob);
return clientKey;
}
private byte[] FixPublicKey(byte[] publicKeyBlob)
{
var keyType = new byte[] { 0x45, 0x43, 0x4b, 0x33 };
var keyLength = new byte[] { 0x30, 0x00, 0x00, 0x00 };
return keyType.Concat(keyLength).Concat(publicKeyBlob.Skip(1)).ToArray();
}
<ECDHKeyValue xmlns="http://www.w3.org/2001/04/xmldsig-more#">
<DomainParameters>
<NamedCurve URN="urn:oid:1.3.132.0.34" />
</DomainParameters>
<PublicKey>
<X Value="1887043347549410128067565566542224816143452851761995862716407003225384875968035424168426761708290432619967335574045" xsi:type="PrimeFieldElemType" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" />
<Y Value="3155051437858668033874279868728733492162411352943682858660003704694667689155247425242901825513751062462637183004716" xsi:type="PrimeFieldElemType" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" />
</PublicKey>
</ECDHKeyValue>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment