How to write a “truly” private method in C#?

2019-05-06 13:07发布

In fact, private methods are implemented in C# that can still be searched with Reflection.

What I am going to do is to write public string Encrypt(string data) and private string Decrypt(string cipher) methods to perform encryption and decryption.

Unfortunately, if someone knows .NET framework, he can use Reflection to find Decrypt methods and it decrypt everything that is encrypted.

It seems that is not that secure. So I want to make Decrypt method to truly private method.

But how to do that?


Updated 09 Jan 2012 10:52PM Sydney Time

bdares provides the technical explanation of this question

Eric Lippert provides the political explanation of this question

Thanks both experts!

5条回答
我欲成王,谁敢阻挡
2楼-- · 2019-05-06 13:29

I believe you are referring to obfuscation, which is an attempt to hide/disguise code from being read by humans when opened in program such as Reflector. Supplied within Visual Studio is a community use license for PreEmptive Solutions dotfuscator which will provide this functionality on small projects, and also for Windows Phone projects (if you download the add-on). There are also commercial platforms available too, from the same vendor and others .

This blog post explains a little more.

查看更多
趁早两清
3楼-- · 2019-05-06 13:31

You can't. If the attacker has access to your code, compiled or source, he can trace your program and find where it's being encrypted or decrypted.

You can add a layer of security by storing the key in a separate location, but generally if the attacker is executing code on your server, you're already screwed.

(You're only worried about this if the attacker is executing code on your server, because otherwise it doesn't matter whether or not the method is private. Also, he can't use reflection to find method names unless he's executing code on your server. In short: you're worrying about the wrong thing here.)

查看更多
Evening l夕情丶
4楼-- · 2019-05-06 13:32

I am not sure about your exact application. But if you are selling a product to a customer who will be doing both the Encryption and Decryption on their own system, then there is no way to keep the encryption secret from them. But you can instead allow them to generate a new Private Key for their own use. In this way each customer's data is 'secure' in regards to other customers; though obviously still not so secure within the same customer's site. In other situations where you control the encrypted content you can also look into creating a private master key to be generated on your side and only allow the customer to have a public key.

查看更多
等我变得足够好
5楼-- · 2019-05-06 13:37

If you're creating your own encryption method, you're doing it wrong. People who know way more about encryption than you or I have already come up with excellent methods for encryption, and MS has implemented most of them already.

For good encryption, it's the keys, not the method, that makes encryption secure. Keep the keys safe and the algorithm can (and should) be published for all to see.

If you're trying to distribute both content and keep it encrypted, aka DRM, you're most probably doomed to failure unless you can keep the keys very well hidden in hardware, and even that will only buy you some time -- maybe months, maybe years.

查看更多
Rolldiameter
6楼-- · 2019-05-06 13:44

Your fundamental problem is that you've got the trust model wrong. If someone can use reflection then they are the user. You are the software provider. You work for them. Trust flows from them, not from you. They are the person who has to trust you, not you them.

If you don't trust the user then do not sell them your software in the first place. Don't sell weapons to people who you believe plan to attack you.

查看更多
登录 后发表回答