<p>Within an application, I've got Secret Keys uses to calculate a hash for an API call.  In a .NET application it's fairly easy to use a program like Reflector to pull out information from the assembly to include these keys.</p>

<p>Is obfuscating the assembly a good way of securing these keys?</p>

[{'answer_id': 33295, 'author': 'TheSmurf', 'author_id': 1975282, 'author_profile': 'https://Stackoverflow.com/users/1975282', 'pm_score': 4, 'selected': True, 'text': "<p>Probably not.</p>\n\n<p>Look into cryptography and Windows' built-in information-hiding mechanisms (DPAPI and storing the keys in an ACL-restricted registry key, for example). That's as good as you're going to get for security you need to keep on the same system as your application.</p>\n\n<p>If you are looking for a way to stop someone physically sitting at the machine from getting your information, forget it. If someone is determined, and has unrestricted access to a computer that is not under your control, there is no way to be 100% certain that the data is protected under all circumstances. Someone who is determined will get at it if they want to.</p>\n"}, {'answer_id': 33304, 'author': 'Craig Walker', 'author_id': 3488, 'author_profile': 'https://Stackoverflow.com/users/3488', 'pm_score': 1, 'selected': False, 'text': "<p>I wouldn't think so, as obfuscating (as I understand it at least) will simply mess around with the method names to make it hard (but not impossible) to understand the code. This  won't change the data of the actual key (which I'm guessing you have stored in a constant somewhere). </p>\n\n<p>If you just want to make it somewhat harder to see, you could run a simple cipher on the plaintext (like ROT-13 or something) so that it's at least not stored in the clear in the code itself. But that's certainly not going to stop any determined hacker from accessing your key. A stronger encryption method won't help because you'd still need to store the key for THAT in the code, and there's nothing protecting that.</p>\n\n<p>The only really secure thing I can think of is to keep the key outside of the application somehow, and then restrict access to the key. For instance, you could keep the key in a separate file and then protected the file with an OS-level user-based restriction; that would probably work. You could do the same with a database connection (again, relying on the user-based access restriction to keep non-authorized users out of the database). </p>\n\n<p>I've toyed with the idea of doing this for my apps but I've never implemented it. </p>\n"}, {'answer_id': 57727, 'author': 'Euro Micelli', 'author_id': 2230, 'author_profile': 'https://Stackoverflow.com/users/2230', 'pm_score': 1, 'selected': False, 'text': '<p>DannySmurf is correct that you can\'t hide keys from the person running an application; if the application can get to the keys, so can the person.</p>\n\n<p>However, What you are trying to accomplish exactly?</p>\n\n<p>Depending on what it is, there are often ways to accomplish your goal that don\'t simply rely on keeping a secret "secret", on your user\'s machine.</p>\n'}, {'answer_id': 42867627, 'author': 'RhinoTX', 'author_id': 7716023, 'author_profile': 'https://Stackoverflow.com/users/7716023', 'pm_score': 1, 'selected': False, 'text': '<p>Late to the game here...</p>\n\n<p>The approach of storing the keys in the assembly / assembly config is fundamentally insecure. There is no possible ironclad way to store it as a determined user will have access. I don\'t care if you use the best / most expensive obfuscation product on the planet. I don\'t care if you use PDAPI to secure the data (although this is better). I don\'t care if you use a local OS-protected key store (this is even better still). None are ideal as all suffer from the same core issue: the user has access to the keys, and they are there, unchanging for days, weeks, possibly even months and years.</p>\n\n<p>A far more secure approach would be is to secure your API calls with tried and true PKI. However, this has obvious performance overhead if your API calls are chatty, but for the vast majority of applications this is a non-issue.</p>\n\n<p>If performance is a concern, you can use Diffie-Hellman over asymmetric PKI to establish a shared secret symmetric key for use with a cipher such as AES. "shared" in this case means shared between client and server, not all clients / users. There is no hard-coded / baked in key. Anywhere.</p>\n\n<p>The keys are transient, regenerated every time the user runs the program, or if you are truly paranoid, they could time-out and require recreation.</p>\n\n<p>The computed shared secret symmetric keys themselves get stored in memory only, in SecureString. They are hard to extract, and even if you do, they are only good for a very short time, and only for communication between that particular client (ie that session). In other words, even if somebody does hack their local keys, they are only good for interfering with local communication. They can\'t use this knowledge to affect other users, unlike a baked-in key shared by all users via code / config.</p>\n\n<p>Furthermore, the entire keys themselves are never, ever passed over the network. The client Alice and server Bob independently compute them. The information they pass in order to do this could in theory be intercepted by third party Charlie, allowing him to independently calculate the shared secret key. That is why you use that (significantly more costLy) asymmetric PKI to protect the key generation between Alice and Bob.</p>\n\n<p>In these systems, the key generation is quite often coupled with authentication and thus session creation.  You "login" and create your "session" over PKI, and after that is complete, both the client and the server independently have a symmetric key which can be used for order-of-magnitude faster encryption for all subsequent communication in that session. For high-scale servers, this is important to save compute cycles on decryption over using say TLS for everything.</p>\n\n<p>But wait: we\'re not secure yet. We\'ve only prevented reading the messages.</p>\n\n<p>Note that it is still necessary to use a message digest mechanism to prevent man-in-the-middle manipulation. While nobody can read the data being transmitted, without a MD there is nothing preventing them from modifying it. So you hash the message before encryption, then send the hash along with the message. The server then re-hashes the payload upon decryption and verifies that it matches the hash that was part of the message. If the message was modified in transit, they won\'t, and the entire message is discarded / ignored.</p>\n\n<p>The final mechanism needed to guard against is replay attacks. At this point, you have prevented people from reading your data, as well as modifying your data, but you haven\'t prevented them from simply sending it again. If this is a problem for your application, it\'s protocol must provide data and both client and server must have enough stateful information to detect a replay. This could be something as simple as a counter that is part of the encrypted payload. Note that if you are using a transport such as UDP, you probably already have a mechanism to deal with duplicated packets, and thus can already deal with replay attacks.</p>\n\n<p>What should be obvious is getting this right is not easy. Thus, use PKI unless you ABSOLUTELY cannot.</p>\n\n<p>Note that this approach is used heavily in the games industry where it is highly desirable to spend as little compute on each player as possible to achieve higher scalability, while at the same time providing security from hacking / prying eyes.</p>\n\n<p>So in conclusion, if this is really something that is a concern, instead of trying to find a securely store the API keys, don\'t. Instead, change how your app uses this API (assuming you have control of both sides, naturally). Use a PKI, or use a PKI-shared symmetric hybrid if PKI will be too slow (which is RARELY a problem these days). Then you won\'t have anything stored that is a security concern.</p>\n'}]
['https://Stackoverflow.com/questions/33288', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2723/']