我開始進行密碼學了。 我需要使用XML,RSA私鑰(證書中未包含擴展名爲.key的文件)和證書.cer擴展名生成PKCS#7文件。在C#中用BouncyCastle生成一個CMS PKCS#7文件
爲此,我使用了BouncyCastle。
編輯:
感謝@khlr您的幫助,但我解決不了我的問題。將數據發送給AC時,會返回「無效的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);
}
}
上簽名PKCS#7,但是這使得以「PFX」證書,也就是說,包含在「.PFX」文件中的私鑰。 當我使用OpenSSL命令時:
openssl smime -sign -signer cert.crt -inkey private.key -out file.xml.cms -in file.xml -outform PEM -nodetach
AC響應良好。 我怎麼能用BouncyCastle和cer和密鑰文件做到這一點?我快瘋了! :-(
您的實際問題是......? – khlr
嗨! @khlr我的問題是我無法創建包含以下內容的CMS文件:--- BEGIN PKCS#7 --- .......................... .................... --- END PKCS#7 --- – Emily
那麼,你有沒有得到任何例外,或只是你得到的不同的輸出你在期待什麼? – khlr