本文整理汇总了C#中RSACryptoServiceProvider.SignData方法的典型用法代码示例。如果您正苦于以下问题:C# RSACryptoServiceProvider.SignData方法的具体用法?C# RSACryptoServiceProvider.SignData怎么用?C# RSACryptoServiceProvider.SignData使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类RSACryptoServiceProvider
的用法示例。
在下文中一共展示了RSACryptoServiceProvider.SignData方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AlgorithmLookups
public static void AlgorithmLookups(string primaryId, object halg)
{
using (var rsa = new RSACryptoServiceProvider())
{
rsa.ImportParameters(TestData.RSA2048Params);
byte[] primary = rsa.SignData(s_dataToSign, primaryId);
byte[] lookup = rsa.SignData(s_dataToSign, halg);
Assert.Equal(primary, lookup);
}
}
示例2: AlgorithmLookups
public static void AlgorithmLookups(string primaryId, object halg)
{
byte[] data = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
using (var rsa = new RSACryptoServiceProvider())
{
rsa.ImportParameters(TestData.RSA2048Params);
byte[] primary = rsa.SignData(data, primaryId);
byte[] lookup = rsa.SignData(data, halg);
Assert.Equal(primary, lookup);
}
}
示例3: ApiInterop_NewToOld
public static void ApiInterop_NewToOld()
{
using (var rsa = new RSACryptoServiceProvider())
{
byte[] newSignature = rsa.SignData(s_dataToSign, HashAlgorithmName.SHA384, RSASignaturePadding.Pkcs1);
Assert.True(rsa.VerifyData(s_dataToSign, "SHA384", newSignature));
}
}
示例4: ApiInterop_OldToNew_Positional
public static void ApiInterop_OldToNew_Positional()
{
using (var rsa = new RSACryptoServiceProvider())
{
byte[] oldSignature = rsa.SignData(s_dataToSign, 3, 2, "SHA384");
Assert.True(rsa.VerifyData(s_dataToSign, 3, 2, oldSignature, HashAlgorithmName.SHA384, RSASignaturePadding.Pkcs1));
}
}
示例5: ApiInterop_OldToNew_Stream
public static void ApiInterop_OldToNew_Stream()
{
const int TotalCount = 10;
using (var rsa = new RSACryptoServiceProvider())
using (PositionValueStream stream1 = new PositionValueStream(TotalCount))
using (PositionValueStream stream2 = new PositionValueStream(TotalCount))
{
byte[] oldSignature = rsa.SignData(stream1, "SHA384");
Assert.True(rsa.VerifyData(stream2, oldSignature, HashAlgorithmName.SHA384, RSASignaturePadding.Pkcs1));
}
}
示例6: SignAndVerify
private static void SignAndVerify(byte[] data, string hashAlgorithmName, RSAParameters rsaParameters)
{
using (var rsa = new RSACryptoServiceProvider())
{
rsa.ImportParameters(rsaParameters);
byte[] signature = rsa.SignData(data, hashAlgorithmName);
bool signatureMatched = rsa.VerifyData(data, hashAlgorithmName, signature);
Assert.True(signatureMatched);
}
}
示例7: ExpectSignature
private static void ExpectSignature(
byte[] expectedSignature,
byte[] data,
string hashAlgorithmName,
RSAParameters rsaParameters)
{
// RSA signatures use PKCS 1.5 EMSA encoding (encoding method, signature algorithm).
// EMSA specifies a fixed filler type of { 0x01, 0xFF, 0xFF ... 0xFF, 0x00 } whose length
// is as long as it needs to be to match the block size. Since the filler is deterministic,
// the signature is deterministic, so we can safely verify it here.
byte[] signature;
using (var rsa = new RSACryptoServiceProvider())
{
rsa.ImportParameters(rsaParameters);
signature = rsa.SignData(data, hashAlgorithmName);
}
Assert.Equal(expectedSignature, signature);
}
示例8: NegativeVerify_BadKeysize
public static void NegativeVerify_BadKeysize()
{
byte[] signature;
using (var rsa = new RSACryptoServiceProvider())
{
rsa.ImportParameters(TestData.RSA2048Params);
signature = rsa.SignData(TestData.HelloBytes, "SHA1");
}
using (var rsa = new RSACryptoServiceProvider())
{
rsa.ImportParameters(TestData.RSA1024Params);
bool signatureMatched = rsa.VerifyData(TestData.HelloBytes, "SHA1", signature);
Assert.False(signatureMatched);
}
}
示例9: NegativeVerify_TamperedData
public static void NegativeVerify_TamperedData()
{
using (var rsa = new RSACryptoServiceProvider())
{
rsa.ImportParameters(TestData.RSA2048Params);
byte[] signature = rsa.SignData(TestData.HelloBytes, "SHA1");
bool signatureMatched = rsa.VerifyData(Array.Empty<byte>(), "SHA1", signature);
Assert.False(signatureMatched);
}
}
示例10: NegativeVerify_WrongSignature
public static void NegativeVerify_WrongSignature()
{
using (var rsa = new RSACryptoServiceProvider())
{
rsa.ImportParameters(TestData.RSA2048Params);
byte[] signature = rsa.SignData(TestData.HelloBytes, "SHA1");
// Invalidate the signature.
signature[0] = (byte)~signature[0];
bool signatureMatched = rsa.VerifyData(TestData.HelloBytes, "SHA1", signature);
Assert.False(signatureMatched);
}
}
示例11: NegativeVerify_WrongAlgorithm
public static void NegativeVerify_WrongAlgorithm()
{
using (var rsa = new RSACryptoServiceProvider())
{
rsa.ImportParameters(TestData.RSA2048Params);
byte[] signature = rsa.SignData(TestData.HelloBytes, "SHA1");
bool signatureMatched = rsa.VerifyData(TestData.HelloBytes, "SHA256", signature);
Assert.False(signatureMatched);
}
}
示例12: ExpectSignature_SHA256_1024_Stream
public static void ExpectSignature_SHA256_1024_Stream()
{
byte[] expectedSignature = new byte[]
{
0x78, 0x6F, 0x42, 0x00, 0xF4, 0x5A, 0xDB, 0x09,
0x72, 0xB9, 0xCD, 0xBE, 0xB8, 0x46, 0x54, 0xE0,
0xCF, 0x02, 0xB5, 0xA1, 0xF1, 0x7C, 0xA7, 0x5A,
0xCF, 0x09, 0x60, 0xB6, 0xFF, 0x6B, 0x8A, 0x92,
0x8E, 0xB4, 0xD5, 0x2C, 0x64, 0x90, 0x3E, 0x38,
0x8B, 0x1D, 0x7D, 0x0E, 0xE8, 0x3C, 0xF0, 0xB9,
0xBB, 0xEF, 0x90, 0x49, 0x7E, 0x6A, 0x1C, 0xEC,
0x51, 0xB9, 0x13, 0x9B, 0x02, 0x02, 0x66, 0x59,
0xC6, 0xB1, 0x51, 0xBD, 0x17, 0x2E, 0x03, 0xEC,
0x93, 0x2B, 0xE9, 0x41, 0x28, 0x57, 0x8C, 0xB2,
0x42, 0x60, 0xDE, 0xB4, 0x18, 0x85, 0x81, 0x55,
0xAE, 0x09, 0xD9, 0xC4, 0x87, 0x57, 0xD1, 0x90,
0xB3, 0x18, 0xD2, 0x96, 0x18, 0x91, 0x2D, 0x38,
0x98, 0x0E, 0x68, 0x3C, 0xA6, 0x2E, 0xFE, 0x0D,
0xD0, 0x50, 0x18, 0x55, 0x75, 0xA9, 0x85, 0x40,
0xAB, 0x72, 0xE6, 0x7F, 0x9F, 0xDC, 0x30, 0xB9,
};
byte[] signature;
using (Stream stream = new PositionValueStream(10))
using (var rsa = new RSACryptoServiceProvider())
{
rsa.ImportParameters(TestData.RSA1024Params);
signature = rsa.SignData(stream, "SHA256");
}
Assert.Equal(expectedSignature, signature);
}
示例13: TestPEM
/// <summary>Demonstrates signing and verifying based on PEM textual public/private key.</summary>
protected static void TestPEM(byte[] dataToSign)
{
Console.WriteLine("Testing PEM...\n");
// -----BEGIN PUBLIC KEY-----...-----END PUBLIC KEY-----
string sPublicKeyPEM = Encoding.ASCII.GetString(RSACryptoServiceProviderExtensionDemo.GetDataFromResource("RSACryptoServiceProviderExtensionPublicKey.pem"));
// -----BEGIN RSA PRIVATE KEY-----...-----END RSA PRIVATE KEY-----
string sPrivateKeyPEM = Encoding.ASCII.GetString(RSACryptoServiceProviderExtensionDemo.GetDataFromResource("RSACryptoServiceProviderExtensionPrivateKey.pem"));
Console.WriteLine("Public key:\n{0}", sPublicKeyPEM);
Console.WriteLine("Private key:\n{0}", sPrivateKeyPEM);
byte[] signature;
bool bVerifyResultOriginal;
bool bVerifyResultModified;
using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
{
rsa.PersistKeyInCsp = false;
rsa.LoadPrivateKeyPEM(sPrivateKeyPEM);
using (SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider())
signature = rsa.SignData(dataToSign, sha1);
}
using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
{
rsa.PersistKeyInCsp = false;
rsa.LoadPublicKeyPEM(sPublicKeyPEM);
using (SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider())
bVerifyResultOriginal = rsa.VerifyData(dataToSign, sha1, signature);
// invalidate signature so the next check must fail
signature[signature.Length - 1] ^= 0xFF;
using (SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider())
bVerifyResultModified = rsa.VerifyData(dataToSign, sha1, signature);
}
Console.WriteLine("PEM: original signature is {0}valid.", bVerifyResultOriginal ? String.Empty : "in");
Console.WriteLine("PEM: tampered signature is {0}valid.", bVerifyResultModified ? String.Empty : "in");
Console.WriteLine("\nDone testing PEM.\n");
}
示例14: TestDER
/// <summary>Demonstrates signing and verifying based on DER binary public/private key.</summary>
protected static void TestDER(byte[] dataToSign)
{
Console.WriteLine("Testing DER...\n");
byte[] publicKeyDER = RSACryptoServiceProviderExtensionDemo.GetDataFromResource("RSACryptoServiceProviderExtensionPublicKey.der");
byte[] privateKeyDER = RSACryptoServiceProviderExtensionDemo.GetDataFromResource("RSACryptoServiceProviderExtensionPrivateKey.der");
Console.WriteLine("Public key:\n{0}\n", BitConverter.ToString(publicKeyDER).Replace("-", ""));
Console.WriteLine("Private key:\n{0}\n", BitConverter.ToString(privateKeyDER).Replace("-", ""));
byte[] signature;
bool bVerifyResultOriginal;
bool bVerifyResultModified;
using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
{
rsa.PersistKeyInCsp = false;
rsa.LoadPrivateKeyDER(privateKeyDER);
using (SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider())
signature = rsa.SignData(dataToSign, sha1);
}
using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
{
rsa.PersistKeyInCsp = false;
rsa.LoadPublicKeyDER(publicKeyDER);
using (SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider())
bVerifyResultOriginal = rsa.VerifyData(dataToSign, sha1, signature);
// invalidate signature so the next check must fail
signature[signature.Length - 1] ^= 0xFF;
using (SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider())
bVerifyResultModified = rsa.VerifyData(dataToSign, sha1, signature);
}
Console.WriteLine("DER: original signature is {0}valid.", bVerifyResultOriginal ? String.Empty : "in");
Console.WriteLine("DER: tampered signature is {0}valid.", bVerifyResultModified ? String.Empty : "in");
Console.WriteLine("\nDone testing DER.\n");
}
示例15: Authenticate
public static byte[] Authenticate(byte[] dataToSign, RSAParameters privateKey, string hashAlgorithm = "SHA512", RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048)
{
using (var rsa = new RSACryptoServiceProvider((int)rsaKeyLength))
{
rsa.ImportParameters(privateKey);
//.NET 4.5 doesn't let you specify padding, defaults to PKCS#1 v1.5 padding
var signature = rsa.SignData(dataToSign, hashAlgorithm);
return signature;
}
}