密鑰交換
// AAA 產生私有參數 + 公開值
using var AAA = ECDiffieHellman.Create(ECCurve.NamedCurves.nistP256);
byte[] AAAPublicKey = AAA.PublicKey.ExportSubjectPublicKeyInfo(); // 公開值
var strPeepAAAPublicKey = Convert.ToBase64String(AAAPublicKey); // Ex:MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE6Zo83HL...
// BBB 產生私有參數 + 公開值
using var BBB = ECDiffieHellman.Create(ECCurve.NamedCurves.nistP256);
byte[] BBBPublicKey = BBB.PublicKey.ExportSubjectPublicKeyInfo(); // 公開值
var strPeepBBBPublicKey = Convert.ToBase64String(BBBPublicKey); // Ex:MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEQnfLJII...
// AAA 用 BBB 公開值算共享金鑰
using var BBBPubKey = ECDiffieHellman.Create();
BBBPubKey.ImportSubjectPublicKeyInfo(BBBPublicKey, out _);
byte[] AAASharedKey = AAA.DeriveKeyFromHash(BBBPubKey.PublicKey, HashAlgorithmName.SHA256);
var strPeepAAASharedKey = Convert.ToBase64String(AAASharedKey); // Ex:HIqP8NCQ9dIouBpH3Qxv5G0w8Y...
// BBB 用 AAA 公開值算共享金鑰
using var AAAPubKey = ECDiffieHellman.Create();
AAAPubKey.ImportSubjectPublicKeyInfo(AAAPublicKey, out _);
byte[] BBBSharedKey = BBB.DeriveKeyFromHash(AAAPubKey.PublicKey, HashAlgorithmName.SHA256);
var strPeepBBBSharedKey = Convert.ToBase64String(BBBSharedKey); // Ex:HIqP8NCQ9dIouBpH3Qxv5G0w8Y...
// 驗證共享金鑰一致
if (strPeepAAASharedKey == strPeepBBBSharedKey)
{
}
我只是一棵樹