在BouncyCastle中导入公共/私有ECDH密钥

时间:2019-01-31 09:33:47

标签: c# private bouncycastle public ecdh

我正在尝试在BouncyCastle中导入公共和私有ECDH密钥。要导入公共密钥,请使用下面的C#代码,该代码可以正常工作:

byte[] pc = HexStringToByteArray(PUBLIC_KEY);
var ecdp = TlsEccUtilities.GetParametersForNamedCurve(NamedCurve.secp256r1);
var basePoint = TlsEccUtilities.ValidateECPublicKey(TlsEccUtilities.DeserializeECPublicKey(null, ecdp, pc));
SubjectPublicKeyInfo subinfo = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(basePoint);
ECPublicKeyParameters publicKey = (ECPublicKeyParameters)PublicKeyFactory.CreateKey(subinfo);

公钥: 042e3e5ccf6b9ab04be7a22f3faccfde73c87e87155394a34815408a896ca18a374dac669af3bf6220fc863767f4af47507c5bc221fc4a19874daf39b4074e3eb8

私钥: be3f9bff87973356d04dad279e21535925c3656db011c9ae76b5a7f09ef8d9de

但是我找不到导入私钥的解决方案。柯洛德,请您帮我解决这个问题?

1 个答案:

答案 0 :(得分:0)

Finlay我找到了解决方法:

// Import public key
byte[] pc = HexStringToByteArray(PUBLIC_KEY);
var ecdp = TlsEccUtilities.GetParametersForNamedCurve(NamedCurve.secp256r1);
var basePoint = TlsEccUtilities.ValidateECPublicKey(TlsEccUtilities.DeserializeECPublicKey(null, ecdp, pc));
SubjectPublicKeyInfo subinfo = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(basePoint);
ECPublicKeyParameters publicKey = (ECPublicKeyParameters)PublicKeyFactory.CreateKey(subinfo);

// Import private key
BigInteger bi = new BigInteger(HexStringToByteArray(PRIVATE_KEY));
ECPrivateKeyParameters privateKey = new ECPrivateKeyParameters(bi, publicKey.Parameters);