不久前Net Core引入了SignedCms,因此数据签名成为可能。然而,这并不像看上去那么清楚。我有sign方法,在中效果很好。净额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)
我在我的电脑中安装了具有上述算法的CSP。Net 4.6它运行良好。
请给出一些建议。