我有用於私鑰和公鑰的AsymmetricKeyParameter對象。什麼是最簡單的方法來查看它們是否匹配?如何使用(.NET/BouncyCastle)檢查私鑰/公鑰對是否匹配?
我想加密一些文本(私鑰)並解密一些文本(公鑰)。 到目前爲止,我還沒有能夠做到這一點,但它似乎是錯誤的方法。
更新:這裏是示例代碼:
X509Certificate2 c = new X509Certificate2(@"certificate.cer");
byte[] privateKeyData = System.IO.File.ReadAllBytes(@"private.key");
Org.BouncyCastle.X509.X509Certificate cert = DotNetUtilities.FromX509Certificate(c);
RsaPrivateCrtKeyParameters privateKey = (RsaPrivateCrtKeyParameters)PrivateKeyFactory.CreateKey(privateKeyData);
RsaKeyParameters publicKey = (RsaKeyParameters)cert.GetPublicKey();
if (privateKey.Modulus.Equals(publicKey.Modulus) && publicKey.Exponent.Equals(privateKey.PublicExponent))
{
//they match
}
如果您使用公鑰進行解密,您是否實際嘗試簽名數據而不是加密? – weston 2012-01-18 10:03:39
我想匹配鍵,我只是不知道如何去做。 – 2012-01-18 11:48:33