有什么简单的方法可以使用用户提供的密码来加密和解密字符串,并且可以在本机 Win32 和 .NET 中使用?

发布于 2025-01-04 20:11:59 字数 357 浏览 2 评论 0原文

我有一个本机 Win32 客户端应用程序和一个 .NET Web 应用程序。我希望能够在客户端上加密字符串(给定用户提供的密码),并能够在给定相同密码的服务器上解密该字符串。我的客户端应用程序是 32 位,我的 .NET Web 应用程序是 64 位。在客户端上,我无法部署 .NET 框架。

我需要一个简单而强大的解决方案。简单地说,我的意思是我更喜欢单个函数调用。我所说的稳健是指我想要一个行业标准的加密算法,例如 AES,并且我不希望出现内存泄漏。

关于如何以简单而强大的方式完成此任务有什么建议吗?

(也许一个 DLL 有 32 位和 64 位版本?我的 .NET Web 应用程序可以 P/Invoke 它,而我的本机应用程序可以使用它。)

I have a native Win32 client application, and a .NET web application. I'd like to be able to encrypt a string on the client (given a user supplied password), and to be able to decrypt this string on the server given the same password. My client application is 32-bit, and my .NET web application is 64-bit. On the client, I'm not able to deploy the .NET framework.

I need a simple and robust solution. By simple, I mean I'd prefer a single function call. By robust, I mean I'd like an industry standard encryption algorithm like AES, and I don't want memory leaks.

Any suggestions on how I can accomplish this in a simple and robust way?

(Perhaps a DLL that comes in both a 32-bit and a 64-bit version? My .NET web application could P/Invoke to it, and my native application could just use it.)

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

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

发布评论

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

评论(3

爱格式化 2025-01-11 20:11:59

恐怕没有“神奇”功能可以为您的服务器和客户端执行此操作。编写一个可以在两者之间工作的简单加密/解密方案并不难,因为算法是标准的。构建自己的软件还可以帮助您了解软件中的加密和解密工作原理。

对于 .NET,您可以使用 System.Security.Cryptography 命名空间,对于 Win32,您应该使用 CryptoAPI

至于加密方案,根据您的用例,您可以使用简单的对称加密方案。

加密:

  1. 将用户密码与常量盐一起哈希以创建 32 位缓冲区。为此,您可以使用 SHA256。不过,CryptoAPI SHA256 仅支持 XP SP3 及更高版本。否则,您可以在线找到许多开源实现。
  2. 将前 16 个字节作为密钥,后 16 个字节作为 IV。
  3. 使用 AES CryptoProvider 在 CryptoAPI 中使用密钥和 IV 进行加密。

解密:

  1. 和 2. 将与加密相同。 .NET 已为 SHA256 构建了类你可以用这个。执行这些步骤应该会为您提供与加密期间相同的密钥。
  2. 使用 AesCryptoServiceProvider 类来解密数据,使用密钥和 IV。请参阅此处的示例: https://gist.github.com/1833986 (这个不使用任何盐)。

I'm afraid that there is no "magic" function that will do this for both your server and the client. It is not hard to write a simple encryption / decryption scheme that will work across both though, since the algorithms are standard. Building your own will also help you to understand how the encryption and decryption works in your software.

For .NET, you can use the System.Security.Cryptography namespace and for Win32 you should use the CryptoAPI.

As for the encryption scheme, going by your use case, you can use a simple symmetric encryption scheme.

Encryption:

  1. Hash the user password together with a constant salt to create a 32-bit buffer. You can use SHA256 for this. The CryptoAPI SHA256 is only supported XP SP3 and up though. Otherwise you can find many open source implementations online.
  2. Take the first 16-bytes as the key and last 16 as the IV.
  3. Use the AES CryptoProvider in the CryptoAPI to do the encryption using the key and the IV.

Decryption:

  1. and 2. will be same as for encryption. .NET has built in classes for SHA256 that you can use for this. Doing these steps should give you the same key as you have during encryption.
  2. Use the AesCryptoServiceProvider class to decrypt the data, using the key and the IV. See example here: https://gist.github.com/1833986 (this one doesn't use any salt).
-残月青衣踏尘吟 2025-01-11 20:11:59

You should probably use Microsoft's Cryptography API. MSDN documentation starts here

This uses the crypt32.dll and is the same DLL that is used under the cover by the System.Security.Cryptography API in .NET. (You can check with an IL Disassembler that it already does P/Invoke on that DLL).

没︽人懂的悲伤 2025-01-11 20:11:59

是否可以是用户的登录密码而不是他们直接提供的密码? Windows 支持在用户登录凭据下对数据进行稳健加密(这通常是密码,但也可以是智能卡)。与 Active Directory 一起使用时,甚至可以配置密码恢复选项。

这称为数据保护 API。可以通过此处记录的调用从本机代码访问它并通过调用 此处

Can it be the user's log in password and not one they supply directly?. Windows supports encrypting data robustly under the users log in credentials( this usually is a password, but could be a smart card). When used with Active Directory, it is even possible to configure password recovery options.

This is called the Data Protection API. It can be accessed from native code via calls documented here and from native code via calls here

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