AdS*_*Ssa 5 c# hash .net-4.0 sha
我使用 OpenSSL 创建了一个自签名证书,如下所示:
openssl req -x509 -sha512 -new keyrsa:2048 -keyout key2.pem -out cert2.pem -days 100
openssl pkcs12 -export -out pkcs12_cert_test2.pfx -inkey key2.pem -in cert2.pem
我已经安装pkcs12_cert_test2.pfx在 Windows 上,证书签名算法值为sha512RSA
然后,我在 C# .NET 4.0 中编写了以下代码:
public static bool DSHandler(string operation, string path, string devicePath)
{
bool result = false;
CryptoConfig.AddAlgorithm(typeof(USBSaferAppEFB.RsaPkCs1Sha512SignatureDescription),
"http://www.w3.org/2001/04/xmldsig-more#rsa-sha512");
string password = "xxxx";
bool validSignature = false;
byte[] hashchain = generateHash(path);
string digSignFile = Utils.RegVarValue(DIGSIGNFILE);
string subjectDN = Utils.RegVarValue(SUBJECTDN);
if (operation == "sign")
{
byte[] signature = SignFromContainer(hashchain, subjectDN);
if (signature != null)
{
string signaturestring = Convert.ToBase64String(signature);
File.WriteAllBytes(devicePath + digSignFile, signature);
result = true;
}
else
result = false;
}
}
static byte[] SignFromContainer(byte[] hashchain, string certSubject)
{
try
{
X509Store my = new X509Store(StoreName.My, StoreLocation.CurrentUser);
my.Open(OpenFlags.ReadOnly);
RSACryptoServiceProvider csp = null;
foreach (X509Certificate2 cert in my.Certificates)
{
if (cert.Subject.Contains(certSubject))
{
// We found it.
// Get its associated CSP and private key
csp = (RSACryptoServiceProvider)cert.PrivateKey;
}
}
if (csp == null)
{
return null;
}
byte[] myHash = { 59,4,248,102,77,97,142,201,
210,12,224,93,25,41,100,197,
210,12,224,93};
// Sign the hash
return csp.SignHash(hashchain, CryptoConfig.MapNameToOID("SHA-512"));
}
catch (Exception e)
{
log.Logger("Exception: "+e.Message, "debug");
log.Logger(e.StackTrace, "debug");
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
使用一些文章和答案中的解决方案,我在RsaPkCs1Sha512SignatureDescription课堂上尝试实现和注册签名描述:
public class RsaPkCs1Sha512SignatureDescription : SignatureDescription
{
public RsaPkCs1Sha512SignatureDescription()
{
KeyAlgorithm = typeof(RSACryptoServiceProvider).FullName;
DigestAlgorithm = typeof(SHA512Managed).FullName;
FormatterAlgorithm = typeof(RSAPKCS1SignatureFormatter).FullName;
DeformatterAlgorithm = typeof(RSAPKCS1SignatureDeformatter).FullName;
}
public override AsymmetricSignatureDeformatter CreateDeformatter(AsymmetricAlgorithm key)
{
var sigProcessor = (AsymmetricSignatureDeformatter)CryptoConfig.CreateFromName(DeformatterAlgorithm);
sigProcessor.SetKey(key);
sigProcessor.SetHashAlgorithm("SHA-512");
return sigProcessor;
}
public override AsymmetricSignatureFormatter CreateFormatter(AsymmetricAlgorithm key)
{
var sigProcessor =
(AsymmetricSignatureFormatter)CryptoConfig.CreateFromName(FormatterAlgorithm);
sigProcessor.SetKey(key);
sigProcessor.SetHashAlgorithm("SHA-512");
return sigProcessor;
}
}
Run Code Online (Sandbox Code Playgroud)
我已经验证哈希是64字节长,它返回一个CryptographicException: Bad Hash. 但是,如果我使用myHashvar(20 字节长),虽然在SignHashis 中指定了算法SHA512,但它可以工作,并使用SHA1算法对哈希进行签名。
另外,如果我打印csp.SignatureAlgorithm,其值为http://www.w3.org/2000/09/xmldsig#rsa-sha1。如果已安装证书的详细信息显示为签名算法,为什么签名算法等于http://www.w3.org/2000/09/xmldsig#rsa-sha1sha512RSA?这可能是真正的问题吗?如果是这样,我该如何正确创建证书?提前致谢!
此过程已在 .NET 4.6 中简化:
byte[] signature;
using (RSA rsa = cert.GetRSAPrivateKey())
{
signature = rsa.SignHash(hash, HashAlgorithmName.SHA512, RSASignaturePadding.Pkcs1);
}
Run Code Online (Sandbox Code Playgroud)
请注意,.NET 4.6 RSA 类型不再需要转换为 RSACryptoServiceProvider,并且如果您使用 GetRSAPrivateKey()(而不是 PrivateKey 属性),则返回的对象可能会处理 SHA-512 签名生成。GetRSAPrivateKey 每次也返回一个唯一的对象,因此您应该适当地处理它。
好的。不需要实现RsaPkCs1Sha512SignatureDescription类或类似的东西,但需要指定正确的 CSP 以允许 CryptoAPI 使用 SHA256 或 SHA512:“Microsoft 增强型 RSA 和 AES 加密提供程序”,类型 24。
我们只需将 CSP 类型作为 CspParameter 并导出/导入关键信息:
byte[] privateKeyBlob;
CspParameters cp = new CspParameters(24);
RSACryptoServiceProvider csp = (RSACryptoServiceProvider)cert.PrivateKey;
privateKeyBlob = csp.ExportCspBlob(true);
csp = new RSACryptoServiceProvider(cp);
csp.ImportCspBlob(privateKeyBlob);
// Sign the hash
return csp.SignHash(hashchain, CryptoConfig.MapNameToOID("SHA-512"));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5542 次 |
| 最近记录: |