最近.Net Core引入了SignedCms,因此数据签名成为可能。然而,它并不像看起来那么清楚。我有符号方法,在 .Net 4.6 中效果很好(见下文(
private byte[] SignData(byte[] data, X509Certificate2 signCertificate, bool isAttached)
{
if (data == null || data.Length == 0)
{
throw new ArgumentException("Data to sign is missing", nameof(data));
}
if (signCertificate == null)
{
throw new ArgumentException("Certificate is missing", nameof(signCertificate));
}
var signer = new CmsSigner(SubjectIdentifierType.IssuerAndSerialNumber, signCertificate);
var signedCms = new SignedCms(new ContentInfo(data), !isAttached);
signedCms.ComputeSignature(signer);
return signedCms.Encode();
}
当我在 .Net 4.6 中调用它时,出现以下异常:
{System.Security.Cryptography.CryptographicException: Could not determine signature algorithm for the signer certificate.
at System.Security.Cryptography.Pkcs.CmsSigner.Sign(ReadOnlyMemory`1 data, String contentTypeOid, Boolean silent, X509Certificate2Collection& chainCerts)
at System.Security.Cryptography.Pkcs.SignedCms.ComputeSignature(CmsSigner signer, Boolean silent)
at System.Security.Cryptography.Pkcs.SignedCms.ComputeSignature(CmsSigner signer)
at BusinessLogic.Managers.CryptographyService.SignData(Byte[] data, X509Certificate2 signCertificate, Boolean isAttached)
我的 PC 中安装了带有上述算法的 CSP,在 .Net 4.6 中它工作正常。请给出一些建议。
我的 PC 中安装了具有上述算法的 CSP
这表明您的非对称算法不是 RSA、RSA-PSS、DSA 或 ECDSA 之一。
SignedCms 的 .NET Framework 实现要求 Windows CMS 库进行签名,这些库具有 CAPI/CNG 扩展点。
SignedCms 的 .NET Core 实现使用 .NET 加密类并执行 CMS 格式设置,而无需要求操作系统执行工作。 它目前没有扩展性模型。
如果您只对Windows感兴趣,那么您可以尝试直接在Windows CMS API中进行P/Invoking。 否则,需要向 .NET Core (https://github.com/dotnet/corefx/issues( 发出功能请求。