简体   繁体   English

C#中的加密密钥保护

[英]encryption key protection in C#

Is there an industry standard for solving following problem: 是否有解决以下问题的行业标准:

Story; 故事; I have a program (=>game) that saves it state (=>level,..) in an xml file; 我有一个程序(=>游戏),它将状态(=> level,..)保存在xml文件中; this state should not be user editable; 这个状态不应该是用户可编辑的; and I want to prevent people writing patch software that can patch my xml & program state. 我想阻止人们编写可修补我的xml和程序状态的补丁软件。

You want to protect your xml file. 您想要保护您的xml文件。 You can do this by encrypting. 您可以通过加密来完成此操作。 However what do you do with the key? 不过你怎么用钥匙?

Because someone can always just reverse engineer (open your dll) en read the key...? 因为有人可以随时进行逆向工程(打开你的dll)阅读密钥......?

public static string Encrypt (string toEncrypt)
{
 byte[] keyArray = UTF8Encoding.UTF8.GetBytes ("12345678901234567890123456789012");
 // 256-AES key
 byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes (toEncrypt);
 RijndaelManaged rDel = new RijndaelManaged ();
 rDel.Key = keyArray;
 rDel.Mode = CipherMode.ECB;
 // http://msdn.microsoft.com/en-us/library/system.security.cryptography.ciphermode.aspx
 rDel.Padding = PaddingMode.PKCS7;
 // better lang support
 ICryptoTransform cTransform = rDel.CreateEncryptor ();
 byte[] resultArray = cTransform.TransformFinalBlock (toEncryptArray, 0, toEncryptArray.Length);
 return Convert.ToBase64String (resultArray, 0, resultArray.Length);
}

public static string Decrypt (string toDecrypt)
{
 byte[] keyArray = UTF8Encoding.UTF8.GetBytes ("12345678901234567890123456789012");
 // AES-256 key
 byte[] toEncryptArray = Convert.FromBase64String (toDecrypt);
 RijndaelManaged rDel = new RijndaelManaged ();
 rDel.Key = keyArray;
 rDel.Mode = CipherMode.ECB;
 // http://msdn.microsoft.com/en-us/library/system.security.cryptography.ciphermode.aspx
 rDel.Padding = PaddingMode.PKCS7;
 // better lang support
 ICryptoTransform cTransform = rDel.CreateDecryptor ();
 byte[] resultArray = cTransform.TransformFinalBlock (toEncryptArray, 0, toEncryptArray.Length);
 return UTF8Encoding.UTF8.GetString (resultArray);
}

edit: app is 100% client side. 编辑:app是100%客户端。

I have a program (=>game) that saves it state (=>level,..) in an xml file; 我有一个程序(=>游戏),它将状态(=> level,..)保存在xml文件中; this state should not be user editable; 这个状态不应该是用户可编辑的; and I want to prevent people writing patch software that can patch my xml & program state. 我想阻止人们编写可修补我的xml和程序状态的补丁软件。 You can do this by encrypting. 您可以通过加密来完成此操作。 However what do you do with the key? 不过你怎么用钥匙? Because someone can always just reverse engineer (open your dll) and read the key. 因为有人可以随时进行逆向工程(打开你的dll)并阅读密钥。

You have produced a convincing argument that what you want to do is impossible. 你已经产生了一个令人信服的论据,即你想做的事情是不可能的。 Your argument is correct. 你的论点是正确的。

Security systems are designed to protect users from attackers , not to protect the user's data from the users themselves . 安全系统旨在保护用户免受攻击者的攻击 ,而不是保护用户自己 的数据

Think about it this way: the game is a program that can edit the state. 以这种方式思考:游戏是一个可以编辑状态的程序。 The user can run the game. 用户可以运行游戏。 Therefore the user can run a program that can edit the state . 因此,用户可以运行可以编辑状态的程序 You don't even need to consider key management because the entire scenario is fundamentally impossible. 您甚至不需要考虑密钥管理,因为整个方案根本不可能。 You can't both require that the user be able to run a program that changes the state and forbid it at the same time . 您不能同时要求用户能够运行更改状态的程序并同时禁止它

If you really want the game state to be protected from the user then the thing that has to give is: the user must not be allowed to run the game . 如果你真的希望游戏状态不受用户保护,那么必须提供的是: 不允许用户运行游戏 Instead the game must run on a server which you own , and the user runs a client which communicates with the server. 相反,游戏必须在您拥有的服务器上运行,并且用户运行与服务器通信的客户端 The server is then responsible for determining whether the client is hostile or not, and determining what the game state is. 然后,服务器负责确定客户端是否是敌对的,并确定游戏状态是什么。

Since the game is now running on a server that you own, and saving the state to a server which you own, you know that the user is not going to edit the state because they cannot run a program which does so . 由于游戏现在在您拥有的服务器上运行,并将状态保存到您拥有的服务器,因此您知道用户不会编辑状态,因为他们无法运行执行此操作的程序

Yes, you ask the user for a key (password). 是的,您要求用户输入密钥(密码)。 This is built into the OS with APIs like Data Protection API . 这是使用Data Protection APIAPI构建到操作系统中的。

If you're looking for a way to hide a secret from the user then the problem you're trying to solve is called DRM (Digital Rights Management) and you need a DRM solution. 如果您正在寻找隐藏用户秘密的方法,那么您尝试解决的问题称为DRM(数字版权管理),您需要DRM解决方案。

Create a user account service that generates user specific keys. 创建用于生成用户特定密钥的用户帐户服务。 Then the application just needs to encrypt and decrypt the sensitive data you want to protect with it. 然后,应用程序只需要加密和解密您要保护的敏感数据。 Each time the game runs make the user log in to his account. 每次游戏运行都会让用户登录到他的帐户。 The key is never stored in the user's machine so your data is relatively safe (at least almost as safe as your account service). 密钥永远不会存储在用户的计算机中,因此您的数据相对安全(至少几乎和您的帐户服务一样安全)。

Downside: You need an internet connection to play the game, but hey, who doesn't have a connection nowdays? 缺点:你需要一个互联网连接来玩这个游戏,但嘿,谁现在没有连接?

Is this a 100% secure? 这是100%安全吗? No, a determined user with enough time could plausibly decrypt his saved game. 不,有足够时间的坚定用户可以合理地解密他保存的游戏。 Of course he would only be able to hack his saved games, so the damage would be minimal (as long as the hacked data is user specific and not shared data). 当然,他只能破解他保存的游戏,因此损害将是最小的(只要被黑客入侵的数据是用户特定的而不是共享数据)。

To make things harder you could always make your service change passwords each few times a given user logs in. 为了使事情变得更难,您可以随时让服务更改密码,每次给定用户登录。

If you want a 100% secure system, or the sensitive data is shared data and not user specific (MMORPG for instance), then, as Eric points out, your only solution would be to have the data in your server, not in the client's machine. 如果您想要一个100%安全的系统,或者敏感数据是共享数据而不是用户特定的(例如MMORPG),那么,正如Eric指出的那样,您唯一的解决方案是将数据存储在服务器中,而不是客户端中。机。

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

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