有什么简单的方法可以使用用户提供的密码来加密和解密字符串,并且可以在本机 Win32 和 .NET 中使用?
我有一个本机 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
恐怕没有“神奇”功能可以为您的服务器和客户端执行此操作。编写一个可以在两者之间工作的简单加密/解密方案并不难,因为算法是标准的。构建自己的软件还可以帮助您了解软件中的加密和解密工作原理。
对于 .NET,您可以使用 System.Security.Cryptography 命名空间,对于 Win32,您应该使用 CryptoAPI。
至于加密方案,根据您的用例,您可以使用简单的对称加密方案。
加密:
解密:
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:
Decryption:
您可能应该使用 Microsoft 的加密 API。 开始。
MSDN 文档从此处 crypt32.dll 与 .NET 中的 System.Security.Cryptography API 所使用的 DLL 相同。 (您可以使用 IL 反汇编程序检查它是否已对该 DLL 执行 P/Invoke)。
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).
是否可以是用户的登录密码而不是他们直接提供的密码? 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