本文整理汇总了C#中RSACryptoServiceProvider.VerifyData方法的典型用法代码示例。如果您正苦于以下问题:C# RSACryptoServiceProvider.VerifyData方法的具体用法?C# RSACryptoServiceProvider.VerifyData怎么用?C# RSACryptoServiceProvider.VerifyData使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类RSACryptoServiceProvider
的用法示例。
在下文中一共展示了RSACryptoServiceProvider.VerifyData方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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));
}
}
示例2: 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));
}
}
示例3: 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));
}
}
示例4: checkSign
/// <summary>
/// 验签
/// </summary>
/// <param name="content">待验签字符串</param>
/// <param name="signedString">签名</param>
/// <param name="publicKey">公钥</param>
/// <param name="input_charset">编码格式</param>
/// <returns>true(通过),false(不通过)</returns>
public static bool checkSign(string content, string signedString, string publicKey, string input_charset)
{
bool result = false;
byte[] Data = Encoding.GetEncoding(input_charset).GetBytes(content);
byte[] data = Convert.FromBase64String(signedString);
RSAParameters paraPub = ConvertFromPublicKey(publicKey);
RSACryptoServiceProvider rsaPub = new RSACryptoServiceProvider();
rsaPub.ImportParameters(paraPub);
SHA1 sh = new SHA1CryptoServiceProvider();
result = rsaPub.VerifyData(Data, sh, data);
return result;
}
示例5: verify
/**
* 根据传入的参数做验签
* @param srcMsg 签名用源串
* @param signMsg 通联响应中给出的签名串
* @param certPath 证书路径
* @param isAbsolatePath 是否绝对路径,如certpath参数值为证书绝对路径,则填true,否则填false
*/
private bool verify(String srcMsg, String signMsg, String certPath, Boolean isAbsolatePath)
{
//base64解码签名串
Byte[] signMsgBytes = decode(signMsg);
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
//读取x509证书
X509Certificate2 x509 = new X509Certificate2();
if (isAbsolatePath)
{
//设置证书的绝对路径
//x509.Import(@"D:\cert\TLCert.cer");
x509.Import(certPath);
}
else
{
//或者设置证书的相对路径
//x509.Import(HttpContext.Current.Server.MapPath("../cert/TLCert.cer"));
x509.Import(HttpContext.Current.Server.MapPath(certPath));
}
//x509.PublicKey.Key.ToXmlString();
//灌注到rsa
rsa.FromXmlString(x509.PublicKey.Key.ToXmlString(false));
bool verifyResult = rsa.VerifyData(System.Text.Encoding.UTF8.GetBytes(srcMsg), "SHA1", signMsgBytes);
return verifyResult;
}
示例6: VerifyData
public static bool VerifyData(byte[] bytesToVerify, string signedMessage, RSAParameters publicKey)
{
bool success = false;
using (var rsa = new RSACryptoServiceProvider())
{
byte[] signedBytes = Convert.FromBase64String(signedMessage);
try
{
rsa.ImportParameters(publicKey);
SHA512Managed Hash = new SHA512Managed();
byte[] hashedData = Hash.ComputeHash(signedBytes);
success = rsa.VerifyData(bytesToVerify, CryptoConfig.MapNameToOID("SHA512"), signedBytes);
}
catch (CryptographicException e)
{
Console.WriteLine(e.Message);
}
finally
{
rsa.PersistKeyInCsp = false;
}
}
return success;
}
示例7: VerifySignature
private static void VerifySignature(
byte[] signature,
byte[] data,
string hashAlgorithmName,
RSAParameters rsaParameters)
{
RSAParameters publicOnly = new RSAParameters
{
Modulus = rsaParameters.Modulus,
Exponent = rsaParameters.Exponent,
};
bool signatureMatched;
using (var rsa = new RSACryptoServiceProvider())
{
rsa.ImportParameters(publicOnly);
signatureMatched = rsa.VerifyData(data, hashAlgorithmName, signature);
}
Assert.True(signatureMatched);
}
示例8: 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);
}
}
示例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_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);
}
}
示例11: 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);
}
}
示例12: 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);
}
}
示例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: Verify
public static bool Verify(byte[] dataToVerify, byte[] signature, RSAParameters publicKey, string hashAlgorithm = "SHA512", RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048)
{
using (var rsa = new RSACryptoServiceProvider((int)rsaKeyLength))
{
rsa.ImportParameters(publicKey);
var verified = rsa.VerifyData(dataToVerify, hashAlgorithm, signature);
return verified;
}
}