我开始学习密码学了。我需要生成一个带有XML的pkcs7文件,一个RSA私钥(证书中没有包含的是一个文件扩展名.key)和一个证书.cer扩展名。
为此,我使用了一个BouncyCastle。
编辑:
感谢@khlr的帮助,但我无法解决我的问题。当将数据发送到AC时,会返回"Invalid CMS“。我有这样的代码:
public static byte[] FirmaBytesMensaje(byte[] argBytesMsg, X509Certificate2 argCertFirmante)
{
try
{
//Add message in object ContentInfo
ContentInfo infoContenido = new ContentInfo(argBytesMsg);
SignedCms cmsFirmado = new SignedCms(infoContenido);
CmsSigner cmsFirmante = new CmsSigner(argCertFirmante);
cmsFirmante.IncludeOption = X509IncludeOption.EndCertOnly;
// Sign message PKCS #7
cmsFirmado.ComputeSignature(cmsFirmante);
// Encodeo el mensaje PKCS #7.
return cmsFirmado.Encode();
}
catch (Exception excepcionAlFirmar)
{
throw new Exception("***Error: " + excepcionAlFirmar.Message);
}
}在PKCS7上签名,但这使用"PFX“证书,即在".pfx”文件中包含私钥。当我使用OpenSSL命令时:
openssl smime -sign -signer cert.crt -inkey private.key -out file.xml.cms -in file.xml -outform PEM -nodetach交流电源响应良好。如何使用BouncyCastle、cer和密钥文件来完成此操作?我要疯了!:-(
发布于 2016-01-31 14:28:46
不幸的是,似乎有no bouncycastle API documentation for C#。尽管如此,仍然有一个Java reference据说与C#应用程序接口非常相似。
因此,getEncoded()-method (查找C#等效项,例如GetEncoded())会产生ASN.1编码的byte[]。
然后,您可以继续从中获取一个字符串(请注意,我不熟悉ASN.1编码。这只是一个猜测):
byte[] buffer = datosFirmados.GetEncoded();
string signedDataString = System.Text.Encoding.UTF8.GetString(buffer, 0, buffer.Length);编辑:
也许AsnEncodedData-class更适合这项任务:
byte[] buffer = datosFirmados.GetEncoded();
var asndata = new AsnEncodedData(buffer);
const bool multiline = true;
string signedDataString = asndata.Format(multiline);发布于 2020-08-31 17:51:14
已经过去一段时间了,但仍然没有答案。您需要将证书和密钥文件合并在一起,如下所示。
using (System.Security.Cryptography.X509Certificates.X509Certificate2 _pub = this.PublicKey.X509Certificate2)
{
using (X509Certificate2 _pri = _pub.CopyWithPrivateKey(_rsa))
{
var _infoContenido = new System.Security.Cryptography.Pkcs.ContentInfo(Message);
SignedCms _signedCms = new SignedCms(_infoContenido);
CmsSigner _cmsSigner = new CmsSigner(_pri);
if (IncludeSignDate)
{
_cmsSigner.SignedAttributes.Add(new Pkcs9SigningTime(DateTime.Now)); // [2020-05-02] 서명한 날짜 속성 추가
}
_cmsSigner.IncludeOption = X509IncludeOption.EndCertOnly;
// Sign message PKCS #7
_signedCms.ComputeSignature(_cmsSigner);
var _signedMessage = _signedCms.Encode();
}
}https://stackoverflow.com/questions/35099408
复制相似问题