简体   繁体   English

.NET中的加密/解密

[英]Encryption/Decryption in .NET

I'm looking for a secure way to encrypt and decrypt a string in a Visual Studio Project (in C#). 我正在寻找一种安全的方法来加密和解密Visual Studio项目中的字符串(在C#中)。 I found that there is native DES classes, but it's not secure enough. 我发现有原生的DES类,但它不够安全。 Do you have any suggestions? 你有什么建议吗?

UPDATE : OK then, the question is : What's the most secure way to encrypt/decrypt a string without too much hassle (aka having to install external tools, etc. An external library is fine though). 更新:那么,问题是:加密/解密字符串的最安全的方法是什么,没有太多的麻烦(也就是说必须安装外部工具等等。外部库虽然很好)。 And where to put the secret "key" (is compiling the value inside the code secure enough?). 在哪里放秘密“密钥”(正在编译代码中的值足够安全吗?)。

Update #2 If I'm using something like this code to save encrypted string in a config file : 更新#2如果我使用类似此代码的内容将加密字符串保存在配置文件中:

using System.Security.Cryptography;
using System.Security;
byte[] encrypted = ProtectedData.Protect(StrToByteArray("my secret text"), null, DataProtectionScope.LocalMachine);
byte[] derypted = ProtectedData.Unprotect(encrypted , null, DataProtectionScope.LocalMachine);

Is this secure enough? 这足够安全吗? I guess that with the "LocalMachine" parameter instead of "User" parameter, somebody could just write an application in .net, put it on the machine and execute it to decrypt the encrypted string. 我想用“LocalMachine”参数而不是“User”参数,有人可以在.net中编写一个应用程序,将它放在机器上并执行它来解密加密的字符串。 So if I want it more secure, I'll have to have a config file different for each user? 因此,如果我希望它更安全,我将不得不为每个用户配置不同的配置文件? Am I understanding that correctly? 我理解正确吗?

To answer your second question, no, storing the encryption key in the executable, even obfuscated, is not secure at all. 要回答第二个问题,不,将加密密钥存储在可执行文件中,甚至是模糊处理,根本不安全。 It'll keep casual prying eyes out, but not those with an hour to devote to walking through your decompiled source. 它会保持随意窥探的眼睛,但不是那些有一个小时专门用于穿过你的反应源的人。

Think hard about where to store your encryption key - it looks like that'll be your weak point. 仔细考虑存储加密密钥的位置 - 看起来这将是你的弱点。 And yes, this is a hard problem to solve. 是的,这是一个难以解决的问题。 The most secure way to store encryption keys is not to - require the user to type a password, or require external hardware, like a key fob. 存储加密密钥的最安全方法不是 - 要求用户键入密码,或者需要外部硬件,如密钥卡。

If you're encrypting contents intended to be read only on a single machine or by a single domain user, consider the Data Protection API (DPAPI) . 如果要加密要在单台计算机上或单个域用户上读取的内容,请考虑使用Data Protection API(DPAPI) It takes the encryption key out of your hands - it uses the user's Windows credentials as the key. 它需要加密密钥 - 它使用用户的Windows凭据作为密钥。

I've got a little more detail in another answer here: Persistent storage of encrypted data using .Net 我在另一个答案中有更多细节: 使用.Net持久存储加密数据

Regarding your second edit ( is DataProtectionScope.LocalMachine good enough? ); 关于你的第二次编辑( DataProtectionScope.LocalMachine是否足够好? ); this MSDN blog entry summarizes it well: 这篇MSDN博客文章总结得很好:

Setting a scope of DataProtectionScope.CurrentUser encrypts the data so that only the currently logged on user can decrypt it. 设置DataProtectionScope.CurrentUser的范围会加密数据,以便只有当前登录的用户才能对其进行解密。 Switching to DataProtectionScope.LocalMachine allows any process running on the current machine to decrypt the data. 切换到DataProtectionScope.LocalMachine允许在当前计算机上运行的任何进程解密数据。 This could be useful in a server scenario, where there are no untrusted logins to the machine, but for a general purpose workstation using LocalMachine encryption is almost equivalent to using no encryption at all (since anybody logged in can get at the data). 这在服务器场景中非常有用,其中没有不受信任的登录到机器,但对于通用工作站,使用LocalMachine加密几乎等同于根本不使用加密(因为任何登录的人都可以获取数据)。

它也有AES

If I read your update correctly, you basically want to conceal some string constant from a sysadmin snooping around your assembly. 如果我正确地读取了您的更新,您基本上想要隐藏来自程序集周围的sysadmin窥探的一些字符串常量。

There is no way to make it impossible that someone with too much time extracts your string constant eventually. 没有办法让那些有太多时间的人最终无法提取你的弦。 But you can annoy them, hoping that they give up trying before they unmask your secret. 但是你可以惹恼他们,希望他们在揭露你的秘密之前放弃尝试。

One way to achieve that are Obfuscation Tools . 实现这一目标的一种方法是混淆工具 These obfuscate your compiled assembly as much as possible, making it much harder to follow program flow when decompiling it with Reflector. 这些会尽可能地混淆编译的程序集,这使得在使用Reflector对其进行反编译时更难以遵循程序流程。 Try it. 试试吧。 If your string constant is still not hidden enough, you can additionally invent your own scheme to make it harder to find. 如果你的字符串常量仍然不够隐藏,你可以另外发明自己的方案,使其更难找到。

If you need more security, the almost only option is to not give the relevant parts of the code to the user. 如果您需要更高的安全性,几乎唯一的选择是不向用户提供代码的相关部分。 Create a web service that contains the secret parts of your application and secure the connection with SSL/TLS. 创建一个包含应用程序秘密部分的Web服务,并使用SSL / TLS保护连接。

尝试使用AesManaged

That depends on your definition of secure enough. 这取决于你对足够安全的定义。 You may use triple DES. 您可以使用三重DES。 .Net also has native Rijandel class. .Net也有本土Rijandel类。 Is it secure enough? 它足够安全吗? http://www.obviex.com/samples/Encryption.aspx http://www.obviex.com/samples/Encryption.aspx

Using a well tested and accepted library is a good idea too... 使用经过良好测试和认可的图书馆也是一个好主意......

http://www.bouncycastle.org/csharp/ http://www.bouncycastle.org/csharp/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM