【发布时间】:2015-06-10 12:49:15
【问题描述】:
我关注this article (in it there is a link to a .cs file at the bottom of the page) 以生成自签名X509Certificate2。文章中的代码有效,但现在我想扩展它。我正在尝试将可选参数_In_opt_ PCRYPT_ALGORITHM_IDENTIFIER pSignatureAlgorithm 传递给CertCreateSelfSignCertificate。
我已经为它创建了这个结构:
struct CryptoApiBlob
{
public Int32 cbData;
public IntPtr pbData;
}
struct CryptAlgorithmIdentifier {
public String pszObjId;
public CryptoApiBlob Parameters;
}
我试图用来创建它的代码是:
CryptAlgorithmIdentifier algorithm = new CryptAlgorithmIdentifier { pszObjId = "szOID_NIST_AES256_CBC", Parameters = new CryptoApiBlob { cbData = 0 } };
algorithmPointer = Marshal.AllocHGlobal(Marshal.SizeOf(algorithm));
Marshal.StructureToPtr(algorithm, algorithmPointer, false);
然后我将algorithmPointer 传递给方法。
当我尝试将其传递给 CertCreateSelfSignCertificate 时出现此错误:
未处理的类型异常 在 mscorlib.dll 中发生“System.Runtime.InteropServices.COMException”
附加信息:函数调用的 ASN1 参数错误。 (HRESULT 异常:0x80093109)
是否有人碰巧知道为什么会发生这种情况,或者可以看到我定义结构或在内存中分配结构的方式有任何问题?
【问题讨论】:
-
COMException?无需通过 P/Invoke 调用 COM。 .NET 很容易与 COM 集成。但乍一看,strings 很难互操作,而且你没有表现出足够的帮助。哦,你不需要任何 P/Invokes 来处理这个 - .NET 有你需要的一切。例如,请参阅stackoverflow.com/questions/22230745/…。 -
@Luaan .NET 有自签名证书吗?很抱歉,您发布的示例使用了名为 Bouncy Castle 的第 3 方 API。我不打算使用任何 3rd 方 API。
-
呃,我的错 - 看到这个,直接使用 COM - stackoverflow.com/questions/13806299/… 仍然可以在纯 .NET 中做所有事情,但 COM 工作得很好。
-
@Luaan 很有帮助,谢谢!
-
是的,这只是被无异常代码隐藏的真正错误位置。但是很明显,您尝试 P/Invoke 的方法只是底层 COM 功能的 C 包装器。坚持使用 COM - .NET 基本上是 COM 2.0 :))
标签: c# .net pinvoke marshalling x509certificate2