- 甲方拥有ECC证书(连同其私钥)
- 甲方将此ECC证书发送给乙方
乙方根据该证书计算 ECDiffieHellmanPublicKey
var cert = new X509Certificate2(certBytes); var bytesWithFormatIndicator = cert.PublicKey.EncodedKeyValue.RawData; var bytesWithoutFormatIndicator = bytesWithFormatIndicator.Skip(1).ToArray(); var preBytes = "45434b3120000000".HexToBytes(); // First 8 bytes are specific to elliptic curve P-256 var rawBytes = bytesWithoutFormatIndicator; var fullBytes = new byte[preBytes.Length + rawBytes.Length]; Buffer.BlockCopy(preBytes, 0, fullBytes, 0, preBytes.Length); Buffer.BlockCopy(rawBytes, 0, fullBytes, preBytes.Length, rawBytes.Length); ECDiffieHellmanPublicKey senderPublicKey = ECDiffieHellmanCngPublicKey.FromByteArray(fullBytes, CngKeyBlobFormat.EccPublicBlob);
乙方创建 ECDH 实现(即临时公钥和私钥对)
var ecdhReceiver = ECDiffieHellman.Create(ECCurve.NamedCurves.nistP256);
B 方在 ECDH 实现上与 A 方 ECC 证书公钥(以及一些前置和附加字节)一起使用并计算对称密钥
var prependBytes = "00000001".HexToBytes(); var appendBytes = "00000002".HexToBytes(); var derivedSymmetricKey = ecdhReceiver.DeriveKeyFromHash(senderPublicKey, HashAlgorithmName.SHA256, prependBytes, appendBytes);
乙方将临时公钥(来自上述 ECDH 实现)发送给甲方
var ephemeralKeyBytesWithoutFormat = ecdhReceiver.PublicKey.ToByteArray().Skip(8).ToArray()
甲方现在的目标是使用乙方的临时公钥和甲方的 ECC 证书(及其私钥)计算相同的对称密钥。
我被困在(7)。任何帮助表示赞赏。
谢谢。