3
以下函數使用證書進行簽名和驗證。如何使用c#密碼檢索簽名散列算法友好名稱?
private static bool Sign_Verify(X509Certificate2 x509, byte[] random_data)
{
//
// Local variable definitions
//
bool isVerified = false;
byte[] buffer = Encoding.Default.GetBytes("Hello World ... !");
byte[] signature_Data = null;
byte[] signature_Hash = null;
//
// Retrieve the private key of the certificate selected.
//
RSACryptoServiceProvider privateKey
= x509.PrivateKey as RSACryptoServiceProvider;
if (null == privateKey)
{
Console.WriteLine("Invalid Private Key");
} // if
else
{
Console.WriteLine("Certificate has private key.");
} // else
HashAlgorithm hashAlgo = HashAlgorithm.Create("SHA256");
byte[] hash = hashAlgo.ComputeHash(random_data);
//
// Perform signing using the private key
try
{
signature_Hash = privateKey.SignHash(
hash,
CryptoConfig.MapNameToOID("SHA256"));
if (null == signature_Hash)
{
Console.WriteLine("Error: SignHash\n\n");
} // if
else
{
Console.WriteLine("Signature_Hash generated......\n\n");
} // else
} // try
catch (CryptographicException)
{
//e.StackTrace();
} // catch
//
// Retrieve the public key of the certificate selected.
//
RSACryptoServiceProvider publicKey
= x509.PublicKey.Key as RSACryptoServiceProvider;
if (null == publicKey)
{
Console.WriteLine("Invalid Public Key");
} // if
else
{
Console.WriteLine("Certificate has public key.");
} // else
isVerified = publicKey.VerifyHash(
hash,
CryptoConfig.MapNameToOID("SHA256"),
signature_Hash);
if (false == isVerified)
{
Console.WriteLine("Signature of Hash verification failed.");
} // if
else
{
Console.WriteLine("Signature of Hash verified successfully.");
} // else
return isVerified;
} // Sign_Verify
在HashAlgorithm.Create(「SHA256」); ,我們對簽名散列算法的名稱進行了硬編碼。如何獲得證書的簽名散列算法的名稱而不是硬編碼呢?
CmsSignedData和IDictionary的是有缺失的命名空間,我已經添加了命名空間System.Security.Cryptography.Pkcs;仍然是給出相同的缺少命名空間錯誤 – user5271376
你能否告訴如果.Net Framework 4 0r 4.5支持Bouncy Castle庫,並且我從未使用過Boucny Castle庫 – user5271376
當然可以。如果您使用nuget軟件包管理器 - 搜索「Bouncy Castle」並安裝該軟件包。如果你不使用nuget - 這是圖書館開發者網站上的直接鏈接 - http://www.bouncycastle.org/csharp/download/bccrypto-csharp-1.8.1-bin.zip。解壓縮並將引用添加到您的項目的dll。 – Evk