本文整理汇总了C#中System.Security.Cryptography.RSAPKCS1SignatureDeformatter.SetHashAlgorithm方法的典型用法代码示例。如果您正苦于以下问题:C# RSAPKCS1SignatureDeformatter.SetHashAlgorithm方法的具体用法?C# RSAPKCS1SignatureDeformatter.SetHashAlgorithm怎么用?C# RSAPKCS1SignatureDeformatter.SetHashAlgorithm使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Security.Cryptography.RSAPKCS1SignatureDeformatter
的用法示例。
在下文中一共展示了RSAPKCS1SignatureDeformatter.SetHashAlgorithm方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CerRSAVerifySignature
/// <summary>
/// 引用证书非对称加/解密RSA-公钥验签【OriginalString:原文;SignatureString:签名字符;pubkey_path:证书路径;CertificatePW:证书密码;SignType:签名摘要类型(1:MD5,2:SHA1)】
/// </summary>
public static bool CerRSAVerifySignature(string OriginalString, string SignatureString, string pubkey_path, string CertificatePW, int SignType)
{
byte[] OriginalByte = System.Text.Encoding.UTF8.GetBytes(OriginalString);
byte[] SignatureByte = Convert.FromBase64String(SignatureString);
X509Certificate2 x509_Cer1 = new X509Certificate2(pubkey_path, CertificatePW);
RSACryptoServiceProvider rsapub = (RSACryptoServiceProvider)x509_Cer1.PublicKey.Key;
rsapub.ImportCspBlob(rsapub.ExportCspBlob(false));
RSAPKCS1SignatureDeformatter f = new RSAPKCS1SignatureDeformatter(rsapub);
byte[] HashData;
switch (SignType)
{
case 1:
f.SetHashAlgorithm("MD5");//摘要算法MD5
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
HashData = md5.ComputeHash(OriginalByte);
break;
default:
f.SetHashAlgorithm("SHA1");//摘要算法SHA1
SHA1CryptoServiceProvider sha = new SHA1CryptoServiceProvider();
HashData = sha.ComputeHash(OriginalByte);
break;
}
if (f.VerifySignature(HashData, SignatureByte))
{
return true;
}
else
{
return false;
}
}
示例2: VerifySignature
public DigitalSignatureVerificationResult VerifySignature(DigitalSignatureVerificationArguments arguments)
{
var res = new DigitalSignatureVerificationResult();
try
{
var rsaProviderSender = new RSACryptoServiceProvider();
rsaProviderSender.FromXmlString(arguments.PublicKeyForSignatureVerification.ToString());
var deformatter = new RSAPKCS1SignatureDeformatter(rsaProviderSender);
deformatter.SetHashAlgorithm(_hashingService.HashAlgorithmCode());
var hashResult = _hashingService.Hash(arguments.CipherText);
res.SignaturesMatch = deformatter.VerifySignature(hashResult.HashedBytes, arguments.Signature);
if (res.SignaturesMatch)
{
var rsaProviderReceiver = new RSACryptoServiceProvider();
rsaProviderReceiver.FromXmlString(arguments.FullKeyForDecryption.ToString());
var decryptedBytes = rsaProviderReceiver.Decrypt(Convert.FromBase64String(arguments.CipherText), false);
res.DecodedText = Encoding.UTF8.GetString(decryptedBytes);
}
res.Success = true;
}
catch (Exception ex)
{
res.ExceptionMessage = ex.Message;
}
return res;
}
示例3: Verify
public static bool Verify(Stream input, Stream output, string publicKey)
{
// ファイルから情報の抽出
var buffer = new byte[64];
if (input.Read(buffer, 0, 64) < 64)
throw new Exception("File is corrupted.(KSIG: Magic Not Found)");
var magicstr = Encoding.UTF8.GetString(buffer).Split(new[] { ":" }, StringSplitOptions.None);
if (magicstr.Length < 2)
throw new Exception("File is corrupted.(KSIG: Invalid Magic Length)");
if (magicstr[0] != MagicStr)
throw new Exception("File is corrupted.(KSIG: Invalid Magic String)");
var siglen = int.Parse(magicstr[1]);
var sigbuf = new byte[siglen];
if (input.Read(sigbuf, 0, siglen) < siglen)
throw new Exception("File is corrupted.(KSIG: Invalid Signature)");
var data = ReadAllBytes(input);
using (var sha = new SHA256Managed())
using (var rsa = new RSACryptoServiceProvider())
{
// Compute hash
var hash = sha.ComputeHash(data);
// RSA Initialize
rsa.FromXmlString(publicKey);
// deformat
var deformatter = new RSAPKCS1SignatureDeformatter(rsa);
deformatter.SetHashAlgorithm("SHA256");
if (!deformatter.VerifySignature(hash, sigbuf))
return false;
}
output.Write(data, 0, data.Length);
return true;
}
示例4: Validate
/// <summary>
/// ��ע����Ϣ���ݲ��÷ǶԳƼ��ܵķ�ʽ����
/// </summary>
/// <param name="originalString">δ���ܵ��ı����������</param>
/// <param name="encrytedString">���ܺ���ı�����ע�����к�</param>
/// <returns>�����֤�ɹ�����True������ΪFalse</returns>
public static bool Validate(string originalString, string encrytedString)
{
bool bPassed = false;
using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
{
try
{
rsa.FromXmlString(UIConstants.PublicKey); //��Կ
RSAPKCS1SignatureDeformatter formatter = new RSAPKCS1SignatureDeformatter(rsa);
formatter.SetHashAlgorithm("SHA1");
byte[] key = Convert.FromBase64String(encrytedString); //��֤
SHA1Managed sha = new SHA1Managed();
byte[] name = sha.ComputeHash(ASCIIEncoding.ASCII.GetBytes(originalString));
if (formatter.VerifySignature(name, key))
{
bPassed = true;
}
}
catch
{
}
}
return bPassed;
}
示例5: verify
public bool verify(byte[] sig)
{
m_cs.Close();
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
RSA.ImportParameters(m_RSAKeyInfo);
RSAPKCS1SignatureDeformatter RSADeformatter = new RSAPKCS1SignatureDeformatter(RSA);
RSADeformatter.SetHashAlgorithm("SHA1");
long i = 0;
long j = 0;
byte[] tmp;
if (sig[0] == 0 && sig[1] == 0 && sig[2] == 0)
{
long i1 = (sig[i++] << 24) & 0xff000000;
long i2 = (sig[i++] << 16) & 0x00ff0000;
long i3 = (sig[i++] << 8) & 0x0000ff00;
long i4 = (sig[i++]) & 0x000000ff;
j = i1 | i2 | i3 | i4;
i += j;
i1 = (sig[i++] << 24) & 0xff000000;
i2 = (sig[i++] << 16) & 0x00ff0000;
i3 = (sig[i++] << 8) & 0x0000ff00;
i4 = (sig[i++]) & 0x000000ff;
j = i1 | i2 | i3 | i4;
tmp = new byte[j];
Array.Copy(sig, i, tmp, 0, j);
sig = tmp;
}
return RSADeformatter.VerifySignature(m_sha1, sig);
}
示例6: CompareCode
public static bool CompareCode(string regcode)
{
if (regcode == null || regcode == "")
{
return false;
}
try
{
string toolcode = GetDiskVolumeSerialNumber() + GetCpuSerialNumber();
string pubkey = "<RSAKeyValue><Modulus>xe3teTUwLgmbiwFJwWEQnshhKxgcasglGsfNVFTk0hdqKc9i7wb+gG7HOdPZLh65QyBcFfzdlrawwVkiPEL5kNTX1q3JW5J49mTVZqWd3w49reaLd8StHRYJdyGAL4ZovBhSTThETi+zYvgQ5SvCGkM6/xXOz+lkMaEgeFcjQQs=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>";
string prikey = "<RSAKeyValue><Modulus>xe3teTUwLgmbiwFJwWEQnshhKxgcasglGsfNVFTk0hdqKc9i7wb+gG7HOdPZLh65QyBcFfzdlrawwVkiPEL5kNTX1q3JW5J49mTVZqWd3w49reaLd8StHRYJdyGAL4ZovBhSTThETi+zYvgQ5SvCGkM6/xXOz+lkMaEgeFcjQQs=</Modulus><Exponent>AQAB</Exponent><P>5flMAd7IrUTx92yomBdJBPDzp1Kclpaw4uXB1Ht+YXqwLW/9icI6mcv7d2O0kuVLSWj8DPZJol9V8AtvHkC3oQ==</P><Q>3FRA9UWcFrVPvGR5bewcL7YqkCMZlybV/t6nCH+gyMfbEvgk+p04F+j8WiHDykWj+BahjScjwyF5SGADbrfJKw==</Q><DP>b4WOU1XbERNfF3JM67xW/5ttPNX185zN2Ko8bbMZXWImr1IgrD5RNqXRo1rphVbGRKoxmIOSv7flr8uLrisKIQ==</DP><DQ>otSZlSq2qomgvgg7PaOLSS+F0TQ/i1emO0/tffhkqT4ah7BgE97xP6puJWZivjAteAGxrxHH+kPY0EY1AzRMNQ==</DQ><InverseQ>Sxyz0fEf5m7GrzAngLDRP/i+QDikJFfM6qPyr3Ub6Y5RRsFbeOWY1tX3jmV31zv4cgJ6donH7W2dSBPi67sSsw==</InverseQ><D>nVqofsIgSZltxTcC8fA/DFz1kxMaFHKFvSK3RKIxQC1JQ3ASkUEYN/baAElB0f6u/oTNcNWVPOqE31IDe7ErQelVc4D26RgFd5V7dSsF3nVz00s4mq1qUBnCBLPIrdb0rcQZ8FUQTsd96qW8Foave4tm8vspbM65iVUBBVdSYYE=</D></RSAKeyValue>";
using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
{
rsa.FromXmlString(pubkey);
RSAPKCS1SignatureDeformatter f = new RSAPKCS1SignatureDeformatter(rsa);
f.SetHashAlgorithm("SHA1");
SHA1Managed sha = new SHA1Managed();
byte[] name = sha.ComputeHash(ASCIIEncoding.ASCII.GetBytes(toolcode));
byte[] key = Convert.FromBase64String(regcode);
return f.VerifySignature(name, key);
}
}
catch
{
return false;
}
}
示例7: ValidatePathWithEncodedRSAPKCS1SignatureAndPublicRSAKey
public static bool ValidatePathWithEncodedRSAPKCS1SignatureAndPublicRSAKey(string path, string base64Signature, string publicKey) {
try {
byte[] signature = Convert.FromBase64String(base64Signature);
byte[] data = File.ReadAllBytes(path);
SHA256CryptoServiceProvider cryptoTransformSHA256 = new SHA256CryptoServiceProvider();
byte[] sha256Hash = cryptoTransformSHA256.ComputeHash(data);
string cleanKey = "";
string[] lines = publicKey.Split(new char[] {'\n', '\r'});
foreach (string line in lines) {
cleanKey += line.Trim();
}
byte[] publicKeyData = Convert.FromBase64String(cleanKey);
RSACryptoServiceProvider provider = new RSACryptoServiceProvider();
provider.ImportCspBlob(publicKeyData);
RSAPKCS1SignatureDeformatter formatter = new RSAPKCS1SignatureDeformatter(provider);
formatter.SetHashAlgorithm("SHA256");
return formatter.VerifySignature(sha256Hash, signature);
} catch (Exception ex) {
Console.WriteLine(ex.Message);
return false;
}
}
示例8: CreateDeformatter
public override AsymmetricSignatureDeformatter CreateDeformatter(AsymmetricAlgorithm key)
{
if (key == null)
throw new ArgumentNullException(nameof(key));
var d = new RSAPKCS1SignatureDeformatter(key);
d.SetHashAlgorithm(SHA_512);
return d;
}
示例9: CreateDeformatter
public override AsymmetricSignatureDeformatter CreateDeformatter(AsymmetricAlgorithm key)
{
if (key == null)
throw new ArgumentNullException("key");
RSAPKCS1SignatureDeformatter deformatter = new RSAPKCS1SignatureDeformatter(key);
deformatter.SetHashAlgorithm("SHA256");
return deformatter;
}
示例10: CreateDeformatter
/// <summary>
/// Create a deformatter
/// </summary>
/// <param name="key">Key</param>
/// <returns>Deformatter</returns>
public override AsymmetricSignatureDeformatter CreateDeformatter(AsymmetricAlgorithm key)
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
var df = new RSAPKCS1SignatureDeformatter(key);
df.SetHashAlgorithm(typeof(SHA256Managed).FullName);
return df;
}
示例11: VerifySignature
public bool VerifySignature(byte[] hash, byte[] signedhash)
{
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
RSAParameters RSAKeyInfo = new RSAParameters();
RSAKeyInfo.Modulus = m_public.Modulus;
RSAKeyInfo.Exponent = m_public.Exponent;
RSA.ImportParameters(RSAKeyInfo);
RSAPKCS1SignatureDeformatter RSADeformatter = new RSAPKCS1SignatureDeformatter(RSA);
RSADeformatter.SetHashAlgorithm("MD5");
return RSADeformatter.VerifySignature(hash, signedhash);
}
示例12: VerifySignature
public bool VerifySignature(byte[] hashOfDataToSign, byte[] signature)
{
using (var rsa = new RSACryptoServiceProvider(2048))
{
rsa.ImportParameters(_publicKey);
var rsaDeformatter = new RSAPKCS1SignatureDeformatter(rsa);
rsaDeformatter.SetHashAlgorithm("SHA256");
return rsaDeformatter.VerifySignature(hashOfDataToSign, signature);
}
}
示例13: CreateDeformatter
public override AsymmetricSignatureDeformatter CreateDeformatter(AsymmetricAlgorithm key)
{
if (key == null)
{
throw new Exception("Invalid key specified for RSAPKCS1SHA256SignatureDescription!");
}
RSAPKCS1SignatureDeformatter deformatter = new RSAPKCS1SignatureDeformatter(key);
deformatter.SetHashAlgorithm("SHA256");
return deformatter;
}
示例14: VerifyString
/// <summary>
/// RSA验证
/// </summary>
/// <param name="CypherText">签名后的Base64字符串</param>
/// <param name="xmlString">密钥(至少含公钥)</param>
/// <param name="PlainText">原始串</param>
public static bool VerifyString(string CypherText, string xmlString, string PlainText)
{
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
rsa.FromXmlString(xmlString);
RSAPKCS1SignatureDeformatter f = new RSAPKCS1SignatureDeformatter(rsa);
f.SetHashAlgorithm("SHA1");
SHA1Managed sha = new SHA1Managed();
byte[] bText = System.Convert.FromBase64String(CypherText);
byte[] bEnc = sha.ComputeHash(System.Text.Encoding.UTF8.GetBytes(PlainText));
return f.VerifySignature(bEnc, bText);
}
示例15: VerifyDigitalSignature
/// <summary>
/// デジタル署名を検証する
/// </summary>
/// <param name="message">署名の付いたメッセージ</param>
/// <param name="signature">署名</param>
/// <param name="publicKey">送信者の公開鍵</param>
/// <returns>認証に成功した時はTrue。失敗した時はFalse。</returns>
public static bool VerifyDigitalSignature(byte[] message, byte[] signature, string publicKey)
{
byte[] hashData = SHA256.Create().ComputeHash(message);
using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
{
rsa.FromXmlString(publicKey);
RSAPKCS1SignatureDeformatter rsaDeformatter = new RSAPKCS1SignatureDeformatter(rsa);
rsaDeformatter.SetHashAlgorithm("SHA256");
return rsaDeformatter.VerifySignature(hashData, signature);
}
}