0

我正在尝试在 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

但我找不到导入私钥的解决方案。Colud你请帮我解决这个问题?

4

1 回答 1

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);
于 2019-02-04T13:44:40.417 回答