本文整理汇总了C#中X509Certificate2.GetRSAPrivateKey方法的典型用法代码示例。如果您正苦于以下问题:C# X509Certificate2.GetRSAPrivateKey方法的具体用法?C# X509Certificate2.GetRSAPrivateKey怎么用?C# X509Certificate2.GetRSAPrivateKey使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类X509Certificate2
的用法示例。
在下文中一共展示了X509Certificate2.GetRSAPrivateKey方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestPrivateKey
public static void TestPrivateKey()
{
using (var c = new X509Certificate2(TestData.PfxData, TestData.PfxDataPassword))
{
bool hasPrivateKey = c.HasPrivateKey;
Assert.True(hasPrivateKey);
using (RSA rsa = c.GetRSAPrivateKey())
{
byte[] hash = new byte[20];
byte[] sig = rsa.SignHash(hash, HashAlgorithmName.SHA1, RSASignaturePadding.Pkcs1);
Assert.Equal(s_expectedSig, sig);
}
}
}
示例2: TestPrivateKey
private static void TestPrivateKey(X509Certificate2 c, bool expectSuccess)
{
if (expectSuccess)
{
using (RSA rsa = c.GetRSAPrivateKey())
{
byte[] hash = new byte[20];
byte[] sig = rsa.SignHash(hash, HashAlgorithmName.SHA1, RSASignaturePadding.Pkcs1);
Assert.Equal(TestData.PfxSha1Empty_ExpectedSig, sig);
}
}
else
{
Assert.ThrowsAny<CryptographicException>(() => c.GetRSAPrivateKey());
}
}
示例3: EphemeralImport_HasNoKeyName
public static void EphemeralImport_HasNoKeyName()
{
using (var cert = new X509Certificate2(TestData.PfxData, TestData.PfxDataPassword, X509KeyStorageFlags.EphemeralKeySet))
using (RSA rsa = cert.GetRSAPrivateKey())
{
Assert.NotNull(rsa);
// While RSACng is not a guaranteed answer, it's currently the answer and we'd have to
// rewrite the rest of this test if it changed.
RSACng rsaCng = rsa as RSACng;
Assert.NotNull(rsaCng);
CngKey key = rsaCng.Key;
Assert.NotNull(key);
Assert.True(key.IsEphemeral, "key.IsEphemeral");
Assert.Null(key.KeyName);
}
}
示例4: TestCopyConstructor_Lifetime_Independent
public static void TestCopyConstructor_Lifetime_Independent()
{
var c1 = new X509Certificate2(TestData.PfxData, TestData.PfxDataPassword);
using (var c2 = new X509Certificate2(TestData.PfxData, TestData.PfxDataPassword))
{
RSA rsa = c2.GetRSAPrivateKey();
byte[] hash = new byte[20];
byte[] sig = rsa.SignHash(hash, HashAlgorithmName.SHA1, RSASignaturePadding.Pkcs1);
Assert.Equal(TestData.PfxSha1Empty_ExpectedSig, sig);
c1.Dispose();
rsa.Dispose();
GC.Collect();
GC.WaitForPendingFinalizers();
// Verify other cert and previous key do not affect cert
using (rsa = c2.GetRSAPrivateKey())
{
hash = new byte[20];
sig = rsa.SignHash(hash, HashAlgorithmName.SHA1, RSASignaturePadding.Pkcs1);
Assert.Equal(TestData.PfxSha1Empty_ExpectedSig, sig);
}
}
}
示例5: TestPrivateKey
public static void TestPrivateKey(X509KeyStorageFlags keyStorageFlags)
{
using (var c = new X509Certificate2(TestData.PfxData, TestData.PfxDataPassword, keyStorageFlags))
{
bool hasPrivateKey = c.HasPrivateKey;
Assert.True(hasPrivateKey);
using (RSA rsa = c.GetRSAPrivateKey())
{
VerifyPrivateKey(rsa);
}
}
}