.net 加密:为页面中的示例寻找等效的 C# 代码

发布于 2024-12-09 19:43:22 字数 727 浏览 0 评论 0原文

我们有一个用于加密的特定库(.net dll)。但它使用了 Win32 例程。这里的问题是我们不能将其用于 64 位操作系统。至少我认为是这样......我们在程序集中的代码是这样写的:

Friend Declare Function CryptAcquireContext Lib "advapi32.dll" Alias "CryptAcquireContextA" (ByRef phProv As Integer, ByVal pszContainer As String, ByVal pszProvider As String, ByVal dwProvType As Integer, ByVal dwFlags As Integer) As Integer

我在那里看到一个 32;所以我猜它不适用于 64 位机器。 :(

不管怎样,我的最终目标是将整个解决方案转换为更可移植的代码(这样它就可以在 32 位和 64 位机器上运行),但我似乎无法理解如何编写等效的 .net 代码。经过大量谷歌搜索后,我发现了一个 C++ 实现,它与旧的 .net dll 中的工作方式类似,可以找到这里

作为这个领域的新手,我很难理解我想他们使用 RSA 算法,因为我们只传递一个密钥来进行加密/解密,如果您需要任何其他详细信息,请添加评论;我 能...

There is a particular library (.net dll) we had used for cryptography. But it made use of Win32 routines. The issue here is we cannot use that for a 64-bit operating system. At least I think its so...we have code in the assembly written like this:

Friend Declare Function CryptAcquireContext Lib "advapi32.dll" Alias "CryptAcquireContextA" (ByRef phProv As Integer, ByVal pszContainer As String, ByVal pszProvider As String, ByVal dwProvType As Integer, ByVal dwFlags As Integer) As Integer

I see a 32 there; so I guess it is not meant for 64-bit machines. :(

Anyway, my ultimate goal is to convert that whole solution to a more portable code (so it can work on both 32 & 64 bit machines), but I can't seem to understand how to write the equivalent .net code. After a lot of googling I found a C++ implementation which is similar to how things were done in the old .net dll. That can be found here

Being a novice in this area it is quite difficult for me to understand the process. I suppose they use the RSA algorithm. And I guess this is a symmetric algorithm, because we pass only one key to do the encryption/decryption. If you need any other details please add a comment; I'll try to respond when I can...

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

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

发布评论

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

评论(4

我很OK 2024-12-16 19:43:22

不幸的是,您调用的函数不是常规加密函数,它用于获取 加密服务提供商(CSP)通常,.net 中不支持除通过本机代码调用之外的对 CSP 的访问 ≤ 4.0。

如果没有你的代码,一旦你抓住了服务提供商的句柄,我就无法弄清楚你正在与服务提供商做什么。如果它是标准对称或非对称函数或类似函数之一,您可能可以完全通过托管代码来完成它。

尝试查看 System.security.cryptography。还有一组 Microsoft 编写的 CNG/CSP 数据提供程序的包装器 这里可能会直接暴露确切的 CSP 及其功能。最后,如果您想要真正可移植的代码,请尝试搜索可以处理 win32、windows ce、win8 和 mono 的 bouncy castle 库。

Unfortunately, the function you are calling is not a regular crypto function, its something used to get a cryptographic service provider(CSP)Generally, access to CSP's except via native code calls are unsupported in .net ≤ 4.0.

Absent your code,I can't figure out what you are doing with the service provider once you grab the handle to it. If its one of the standard symmetric or asymmetric functions or something similar, you can probably do it entirely from managed code.

Try looking at System.security.cryptography. There is also a Microsoft written set of wrappers around CNG/CSP data providers here that might directly expose expose the exact CSP and its functionality . Finally, if you want really really portable code, try searching for the bouncy castle library which would handle both win32, windows ce, win8, and mono.

梦幻的心爱 2024-12-16 19:43:22

对于 RSA,有 RSACryptoServiceProvider 类,它是System.Security.Cryptography

这可能会让您在 .NET 上具有相当的可移植性(但不幸的是,.NET Compact Framework 并不支持所有这些)。如果您想要更便携,crypto++ 是原生 C++。

For RSA there's the RSACryptoServiceProvider class, which is part of System.Security.Cryptography.

That will probably get you fairly portable on .NET (but .NET Compact Framework doesn't support all of this, unfortunately). If you want more portable, crypto++ is native C++.

梦里梦着梦中梦 2024-12-16 19:43:22

advapi32.dll 确实存在于我的 win 7 x64 机器上,因此它应该仍然可以工作,否则 System.Security 不包含等效功能怎么样?

advapi32.dll does exist on my win 7 x64 box so it should still work, otherwise how about System.Security does that not contain equivilent functionality?

四叶草在未来唯美盛开 2024-12-16 19:43:22

如果您使用 IntPtr。您的指针固定为 32 位,这可能(很可能)在 64 位环境中导致问题。例如:

    Friend Declare Function CryptAcquireContext Lib "advapi32.dll" Alias "CryptAcquireContextA" (ByRef phProv As IntPtr, ByVal pszContainer As String, ByVal pszProvider As String, ByVal dwProvType As Integer, ByVal dwFlags As Integer) As IntPtr

就是这样。 dwFlagsdwProvType 保持 Integer,因为无论平台如何(DWORD),它们都是 32 位。您必须修复所有平台调用才能使用IntPtr。例如,您对 CryptReleaseContext 的调用还应采用 IntPtr 作为第一个参数,而不是 Integer。

或者,您可以将应用程序更改为始终针对 x86 平台而不是任何 CPU,然后让 WOW64 处理它。然后,不需要更改代码。

如果您可以解释您需要此函数的用途,则可能有一个更直接的 .NET 实现来实现您想要执行的操作。例如,如果您尝试使用 x509 证书,您可能对 X509Certificate2 类。

That function should work fine on 64-bit windows if you change your pointers to be the same size at the platform using IntPtr. Your pointers are fixed at 32-bits, which may (most likely) cause problems in a 64-bit environments. For example:

    Friend Declare Function CryptAcquireContext Lib "advapi32.dll" Alias "CryptAcquireContextA" (ByRef phProv As IntPtr, ByVal pszContainer As String, ByVal pszProvider As String, ByVal dwProvType As Integer, ByVal dwFlags As Integer) As IntPtr

And that's it. The dwFlags and dwProvType stay Integer because they are 32-bit regardless of platform (DWORD). You will have to fix all platform invoke calls to use IntPtr. For example, your call to CryptReleaseContext should also take an IntPtr as the first parameter rather than Integer.

Alternatively, you can change your application to always target the x86 platform instead of Any CPU and just let WOW64 handle it. Then, no code changes are needed.

If you can explain what you need this function for, there might be a more straight forward .NET implementation of what you are trying to do. For instance, if you are trying to work with x509 certificates, you may be more interested in the X509Certificate2 class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文