我正在使用这篇 MSDN 文章中关于DSACryptoServiceProvider
该类的示例。问题是每次运行代码时都会得到不同的签名。
我尝试了 OpenSSL 并没有遇到这个问题,但这次我需要使用 System.Security.Cryptography。
这是一些源代码:
这是要签名的哈希值
byte[] HashValue =
{
59, 4, 248, 102, 77, 97, 142, 201,
210, 12, 224, 93, 25, 41, 100, 197,
213, 134, 130, 135
};
这就是问题所在
// The value to hold the signed value.
byte[] SignedHashValue1 = DSASignHash(HashValue, privateKeyInfo, "SHA1");
byte[] SignedHashValue2 = DSASignHash(HashValue, privateKeyInfo, "SHA1");
我用调试器找出SignedHashValue1
不等于SignedHashValue2
文章中的代码:
using System;
using System.Security.Cryptography;
public class DSACSPSample
{
public static void Main()
{
try
{
DSAParameters privateKeyInfo;
DSAParameters publicKeyInfo;
// Create a new instance of DSACryptoServiceProvider to generate
// a new key pair.
using (DSACryptoServiceProvider DSA = new DSACryptoServiceProvider())
{
privateKeyInfo = DSA.ExportParameters(true);
publicKeyInfo = DSA.ExportParameters(false);
}
// The hash value to sign.
byte[] HashValue =
{
59, 4, 248, 102, 77, 97, 142, 201,
210, 12, 224, 93, 25, 41, 100, 197,
213, 134, 130, 135
};
// The value to hold the signed value.
byte[] SignedHashValue = DSASignHash(HashValue, privateKeyInfo, "SHA1");
// Verify the hash and display the results.
bool verified = DSAVerifyHash(HashValue, SignedHashValue, publicKeyInfo, "SHA1");
if (verified)
{
Console.WriteLine("The hash value was verified.");
}
else
{
Console.WriteLine("The hash value was not verified.");
}
}
catch (ArgumentNullException e)
{
Console.WriteLine(e.Message);
}
}
public static byte[] DSASignHash(byte[] HashToSign, DSAParameters DSAKeyInfo,
string HashAlg)
{
byte[] sig = null;
try
{
// Create a new instance of DSACryptoServiceProvider.
using (DSACryptoServiceProvider DSA = new DSACryptoServiceProvider())
{
// Import the key information.
DSA.ImportParameters(DSAKeyInfo);
// Create an DSASignatureFormatter object and pass it the
// DSACryptoServiceProvider to transfer the private key.
DSASignatureFormatter DSAFormatter = new DSASignatureFormatter(DSA);
// Set the hash algorithm to the passed value.
DSAFormatter.SetHashAlgorithm(HashAlg);
// Create a signature for HashValue and return it.
sig = DSAFormatter.CreateSignature(HashToSign);
}
}
catch (CryptographicException e)
{
Console.WriteLine(e.Message);
}
return sig;
}
public static bool DSAVerifyHash(byte[] HashValue, byte[] SignedHashValue,
DSAParameters DSAKeyInfo, string HashAlg)
{
bool verified = false;
try
{
// Create a new instance of DSACryptoServiceProvider.
using (DSACryptoServiceProvider DSA = new DSACryptoServiceProvider())
{
// Import the key information.
DSA.ImportParameters(DSAKeyInfo);
// Create an DSASignatureDeformatter object and pass it the
// DSACryptoServiceProvider to transfer the private key.
DSASignatureDeformatter DSADeformatter = new DSASignatureDeformatter(DSA);
// Set the hash algorithm to the passed value.
DSADeformatter.SetHashAlgorithm(HashAlg);
// Verify signature and return the result.
verified = DSADeformatter.VerifySignature(HashValue, SignedHashValue);
}
}
catch (CryptographicException e)
{
Console.WriteLine(e.Message);
}
return verified;
}
}