CAPICOM 与 P/Invoke

发布于 2024-11-26 13:59:51 字数 371 浏览 0 评论 0原文

我想在 C# 中使用 CryptoAPI 来访问证书存储并签署消息。

这篇 MSDN 文章“CAPICOM:CryptoAPI 变得简单” 显示了 2 种方法为此:使用 CAPICOM 或 P/Invoke。

  1. 哪个最好? CAPICOM.dll 或 P/Invoke [DllImport("crypt32.dll", ...)]

  2. “crypt32.dll”会始终存在于任何计算机上吗?

I would like to use CryptoAPI in C# to access certificate store and sign message.

This MSDN article "CAPICOM: CryptoAPI Made Easy" shows 2 way to do that: using CAPICOM or P/Invoke.

  1. Which is best? CAPICOM.dll or P/Invoke [DllImport("crypt32.dll", ...)]

  2. Will "crypt32.dll" always be present on any machine?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

衣神在巴黎 2024-12-03 13:59:51

CAPICOM 是一项旧技术,已被 Microsoft 弃用。 P/Invoke(平台调用)允许托管代码调用在 DLL 中实现的非托管代码。

它是 CryptoAPI 库的基于 COM 的包装器。您发布的文章是 2003 年的。那是 8 年前的事了,技术已经在进步。

MSDN 上的这篇文章 建议您使用 .NET代替 CAPICOM 的安全功能框架

CAPICOM is an old technology that has/is being deprecated by Microsoft. P/Invoke (Platform Invoke) allows managed code to call unmanaged code that are implemented in a DLL.

It is a COM-based wrapper for the CryptoAPI library. The article you posted is from 2003. That was over 8 years ago and technology has moved on.

This article at MSDN here recommends you use the .NET framework for security features in lieu of CAPICOM.

橘亓 2024-12-03 13:59:51

crypt32 的可用性 不应该出现问题,而capicom 的 确实如此。

同样,在前往 p\invoke 之前,您应该确保 .Net 中没有托管包装器

crypt32's availablility shouldn't oppose a problem, while capicom's does.

On the same note though, you should make sure there's no managed wrapper in .Net before heading to p\invoke

↙温凉少女 2024-12-03 13:59:51

使用 CAPICOM 是必要条件吗?它会起作用,但有一些非常烦人的缺点,如果你没有/没有/使用它,你最好使用 System.Security.Cryptography。

无论如何:要使用 CAPICOM,您必须首先在项目中添加对它的引用。然后:

    CAPICOM.SignedData signeddata = new CAPICOM.SignedData();
    FileStream file = File.Open(tbSourceFile.Text, FileMode.Open);
    byte[] Content = new byte[(int)file.Length];

    file.Read(Content, 0, (int)file.Length);
    file.Close();

    StringWriter sw = new StringWriter();
    sw.Write(Content);

    signeddata.Content = sw.ToString();

    IStore store = new CAPICOM.Store();
    store.Open(CAPICOM.CAPICOM_STORE_LOCATION.CAPICOM_CURRENT_USER_STORE, "MY", CAPICOM.CAPICOM_STORE_OPEN_MODE.CAPICOM_STORE_OPEN_READ_ONLY | CAPICOM.CAPICOM_STORE_OPEN_MODE.CAPICOM_STORE_OPEN_EXISTING_ONLY);
    ICertificates2 certificates = (ICertificates2)store.Certificates;

    certificates = certificates.Find(CAPICOM_CERTIFICATE_FIND_TYPE.CAPICOM_CERTIFICATE_FIND_KEY_USAGE, CAPICOM_KEY_USAGE.CAPICOM_DIGITAL_SIGNATURE_KEY_USAGE, true);

    if (certificates.Count > 0)
    {
        certificates = certificates.Select();
    }
    if (certificates.Count > 0)
    {
        ISigner2 signers = new CAPICOM.Signer();
        signers.Certificate = certificates[1];
        tbSignatureBlock.Text = signeddata.Sign(signers, true);
    }

Is using CAPICOM a requirement ? It will work but is has a couple of really annoying downside and, if you don't /have/ to use it, you'd be better using System.Security.Cryptography.

Anyway: To use CAPICOM, you must first add a reference to it in your project. Then:

    CAPICOM.SignedData signeddata = new CAPICOM.SignedData();
    FileStream file = File.Open(tbSourceFile.Text, FileMode.Open);
    byte[] Content = new byte[(int)file.Length];

    file.Read(Content, 0, (int)file.Length);
    file.Close();

    StringWriter sw = new StringWriter();
    sw.Write(Content);

    signeddata.Content = sw.ToString();

    IStore store = new CAPICOM.Store();
    store.Open(CAPICOM.CAPICOM_STORE_LOCATION.CAPICOM_CURRENT_USER_STORE, "MY", CAPICOM.CAPICOM_STORE_OPEN_MODE.CAPICOM_STORE_OPEN_READ_ONLY | CAPICOM.CAPICOM_STORE_OPEN_MODE.CAPICOM_STORE_OPEN_EXISTING_ONLY);
    ICertificates2 certificates = (ICertificates2)store.Certificates;

    certificates = certificates.Find(CAPICOM_CERTIFICATE_FIND_TYPE.CAPICOM_CERTIFICATE_FIND_KEY_USAGE, CAPICOM_KEY_USAGE.CAPICOM_DIGITAL_SIGNATURE_KEY_USAGE, true);

    if (certificates.Count > 0)
    {
        certificates = certificates.Select();
    }
    if (certificates.Count > 0)
    {
        ISigner2 signers = new CAPICOM.Signer();
        signers.Certificate = certificates[1];
        tbSignatureBlock.Text = signeddata.Sign(signers, true);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文