本文整理汇总了C#中X509Certificate2.GetECDsaPublicKey方法的典型用法代码示例。如果您正苦于以下问题:C# X509Certificate2.GetECDsaPublicKey方法的具体用法?C# X509Certificate2.GetECDsaPublicKey怎么用?C# X509Certificate2.GetECDsaPublicKey使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类X509Certificate2
的用法示例。
在下文中一共展示了X509Certificate2.GetECDsaPublicKey方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestECDsa224PublicKey
public static void TestECDsa224PublicKey()
{
using (var cert = new X509Certificate2(TestData.ECDsa224Certificate))
{
// It is an Elliptic Curve Cryptography public key.
Assert.Equal("1.2.840.10045.2.1", cert.PublicKey.Oid.Value);
ECDsa ecdsa;
try
{
ecdsa = cert.GetECDsaPublicKey();
}
catch (CryptographicException)
{
// Windows 7, Windows 8, CentOS.
return;
}
// Other Unix
using (ecdsa)
{
byte[] data = ByteUtils.AsciiBytes("Hello");
byte[] signature = (
// r
"8ede5053d546d35c1aba829bca3ecf493eb7a73f751548bd4cf2ad10" +
// s
"5e3da9d359001a6be18e2b4e49205e5219f30a9daeb026159f41b9de").HexToByteArray();
Assert.True(ecdsa.VerifyData(data, signature, HashAlgorithmName.SHA1));
}
}
}
示例2: TestKey_ECDsaCng
private static void TestKey_ECDsaCng(byte[] certBytes, TestData.ECDsaCngKeyValues expected)
{
#if !NETNATIVE
using (X509Certificate2 cert = new X509Certificate2(certBytes))
{
ECDsaCng e = (ECDsaCng)(cert.GetECDsaPublicKey());
CngKey k = e.Key;
byte[] blob = k.Export(CngKeyBlobFormat.EccPublicBlob);
using (BinaryReader br = new BinaryReader(new MemoryStream(blob)))
{
int magic = br.ReadInt32();
int cbKey = br.ReadInt32();
Assert.Equal(expected.QX.Length, cbKey);
byte[] qx = br.ReadBytes(cbKey);
byte[] qy = br.ReadBytes(cbKey);
Assert.Equal<byte>(expected.QX, qx);
Assert.Equal<byte>(expected.QY, qy);
}
}
#endif //!NETNATIVE
}
示例3: TestECDsaPublicKey_ValidatesSignature
public static void TestECDsaPublicKey_ValidatesSignature()
{
// This signature was produced as the output of ECDsaCng.SignData with the same key
// on .NET 4.6. Ensure it is verified here as a data compatibility test.
//
// Note that since ECDSA signatures contain randomness as an input, this value is unlikely
// to be reproduced by another equivalent program.
byte[] existingSignature =
{
// r:
0x7E, 0xD7, 0xEF, 0x46, 0x04, 0x92, 0x61, 0x27,
0x9F, 0xC9, 0x1B, 0x7B, 0x8A, 0x41, 0x6A, 0xC6,
0xCF, 0xD4, 0xD4, 0xD1, 0x73, 0x05, 0x1F, 0xF3,
0x75, 0xB2, 0x13, 0xFA, 0x82, 0x2B, 0x55, 0x11,
0xBE, 0x57, 0x4F, 0x20, 0x07, 0x24, 0xB7, 0xE5,
0x24, 0x44, 0x33, 0xC3, 0xB6, 0x8F, 0xBC, 0x1F,
// s:
0x48, 0x57, 0x25, 0x39, 0xC0, 0x84, 0xB9, 0x0E,
0xDA, 0x32, 0x35, 0x16, 0xEF, 0xA0, 0xE2, 0x34,
0x35, 0x7E, 0x10, 0x38, 0xA5, 0xE4, 0x8B, 0xD3,
0xFC, 0xE7, 0x60, 0x25, 0x4E, 0x63, 0xF7, 0xDB,
0x7C, 0xBF, 0x18, 0xD6, 0xD3, 0x49, 0xD0, 0x93,
0x08, 0xC5, 0xAA, 0xA6, 0xE5, 0xFD, 0xD0, 0x96,
};
byte[] helloBytes = Encoding.ASCII.GetBytes("Hello");
using (var cert = new X509Certificate2(TestData.ECDsa384Certificate))
using (ECDsa publicKey = cert.GetECDsaPublicKey())
{
Assert.Equal(384, publicKey.KeySize);
bool isSignatureValid = publicKey.VerifyData(helloBytes, existingSignature, HashAlgorithmName.SHA256);
Assert.True(isSignatureValid, "isSignatureValid");
}
}
示例4: TestECDsaPublicKey
public static void TestECDsaPublicKey()
{
byte[] helloBytes = Encoding.ASCII.GetBytes("Hello");
using (var cert = new X509Certificate2(TestData.ECDsa384Certificate))
using (ECDsa publicKey = cert.GetECDsaPublicKey())
{
Assert.Equal(384, publicKey.KeySize);
// The public key should be unable to sign.
Assert.ThrowsAny<CryptographicException>(() => publicKey.SignData(helloBytes, HashAlgorithmName.SHA256));
}
}
示例5: TestECDsaPublicKey_NonSignatureCert
public static void TestECDsaPublicKey_NonSignatureCert()
{
using (var cert = new X509Certificate2(TestData.EccCert_KeyAgreement))
using (ECDsa publicKey = cert.GetECDsaPublicKey())
{
// It is an Elliptic Curve Cryptography public key.
Assert.Equal("1.2.840.10045.2.1", cert.PublicKey.Oid.Value);
// But, due to KeyUsage, it shouldn't be used for ECDSA.
Assert.Null(publicKey);
}
}
示例6: TestECDsaPublicKey_BrainpoolP160r1_ValidatesSignature
public static void TestECDsaPublicKey_BrainpoolP160r1_ValidatesSignature(byte[] curveData, byte[] existingSignature)
{
byte[] helloBytes = Encoding.ASCII.GetBytes("Hello");
try
{
using (var cert = new X509Certificate2(curveData))
{
using (ECDsa publicKey = cert.GetECDsaPublicKey())
{
Assert.Equal(160, publicKey.KeySize);
// It is an Elliptic Curve Cryptography public key.
Assert.Equal("1.2.840.10045.2.1", cert.PublicKey.Oid.Value);
bool isSignatureValid = publicKey.VerifyData(helloBytes, existingSignature, HashAlgorithmName.SHA256);
Assert.True(isSignatureValid, "isSignatureValid");
unchecked
{
--existingSignature[existingSignature.Length - 1];
}
isSignatureValid = publicKey.VerifyData(helloBytes, existingSignature, HashAlgorithmName.SHA256);
Assert.False(isSignatureValid, "isSignatureValidNeg");
}
}
}
catch (CryptographicException)
{
// Windows 7, Windows 8, Ubuntu 14, CentOS can fail. Verify known good platforms don't fail.
Assert.False(PlatformDetection.IsWindows && PlatformDetection.WindowsVersion >= 10);
Assert.False(PlatformDetection.IsUbuntu1604);
Assert.False(PlatformDetection.IsUbuntu1610);
Assert.False(PlatformDetection.IsOSX);
return;
}
}
示例7: TestKey_ECDsabrainpool_PublicKey
public static void TestKey_ECDsabrainpool_PublicKey(byte[] curveData, byte[] notUsed)
{
byte[] helloBytes = Encoding.ASCII.GetBytes("Hello");
try
{
using (var cert = new X509Certificate2(curveData))
{
using (ECDsa ec = cert.GetECDsaPublicKey())
{
Assert.Equal(160, ec.KeySize);
// The public key should be unable to sign.
Assert.ThrowsAny<CryptographicException>(() => ec.SignData(helloBytes, HashAlgorithmName.SHA256));
}
}
}
catch (CryptographicException)
{
// Windows 7, Windows 8, Ubuntu 14, CentOS can fail. Verify known good platforms don't fail.
Assert.False(PlatformDetection.IsWindows && PlatformDetection.WindowsVersion >= 10);
Assert.False(PlatformDetection.IsUbuntu1604);
Assert.False(PlatformDetection.IsUbuntu1610);
Assert.False(PlatformDetection.IsOSX);
return;
}
}