Web Security: Hashing, Salting, and Encryption
Tags: security, csharp, programming, webdev
Published: 2021-04-08 05:11:57


<p>As a web developer or engineer it is imperative that your users’ information is safe and secure in a database and during communication with your front end. But adding extra security to your code can add complexity to how your system works. Here is some basic terminology that every developer needs to know at a minimum before handling user information.</p>
<h3><strong>Hashing</strong></h3>
<p>Passwords and sensitive information should never be transmitted as plain text. It can and will be stolen, no matter how many users you have on your system. A hash generates a unique fixed length output representation of a given input. A website should maintain a known secret key, and combine both the password and key to generate a scrambled output. Even if a bad actor were to acquire the hashed value of the password, they would still not be able to work out what the password is if a proper salt is used. Hashing is deterministic and therefore is not good enough on its own and not a replacement for encryption. But at a bare minimum it is a step up from transmitting raw&nbsp;text.</p>
<figure><img alt="" src="https://cdn-images-1.medium.com/max/659/0*w8QVgl_Z3KCxv1_7"><figcaption>Here is an ASP.NET Core example of how to hash a string. Source: <a href="https://docs.microsoft.com/en-us/aspnet/core/security/data-protection/consumer-apis/password-hashing?view=aspnetcore-5.0">Microsoft Docs</a></figcaption></figure><h3><strong>Salting</strong></h3>
<p>If you notice in the code above, there is a variable called salt that is a 128 byte array. What in the world is this? Salting is the addition of unique, random characters known only to the site at the beginning of the string that you want to hash. The salt is typically then stored by the site and used when validating the hash. The reason that a salt is needed is because inputs could potentially be hashed to the same string, but since the salt is random the hashes will then be&nbsp;unique.</p>
<p><em>Pro tip:</em> You can hash and sal multiple times to increase the difficulty in breaking the security.</p>
<h3><strong>Encryption</strong></h3>
<p>Encryption is a function of cryptography and is similar to hashing, except encryption is designed to be undone, while hashing is not. Let’s take a look at an example of how to encrypt data in an ASP.NET c# environment. The using aes variable in the example is what is doing the encryption. Aes is a reference to the AES encryption algorithm which you can read more about&nbsp;<a href="https://www.educative.io/edpresso/what-is-the-aes-algorithm">here</a>.</p>
<figure><img alt="" src="https://cdn-images-1.medium.com/max/695/0*LUwMXnLt_C1bRWRN"><figcaption>Here is an ASP.NET Core example of how to encrypt data. Source: <a href="https://docs.microsoft.com/en-us/dotnet/standard/security/encrypting-data">Microsoft Docs</a></figcaption></figure><h3><strong>Keys</strong></h3>
<p>Symmetric and Asymmetric Keys are a great security practice for keeping data safe. Continuing with the above example, AES is a symmetric algorithm meaning that you need to use the same key to encrypt and decrypt the data. As with everything in this post, never store a cryptographic key in plaintext or transfer a key between machines in plain text. It’s not sage. Instead, consider using a <a href="https://docs.microsoft.com/en-us/dotnet/standard/security/how-to-store-asymmetric-keys-in-a-key-container">secure key container</a> to store any cryptographic keys. In the below example we create an EncryptionMethod object that is initialize to the URL identifies of the cryptographic algorithm used to generate the AES&nbsp;key.</p>
<figure><img alt="" src="https://cdn-images-1.medium.com/max/435/0*DQ2oijUceMLdz-C7"><figcaption>Encrypt XML Elements with Symmetric Keys Source: <a href="https://docs.microsoft.com/en-us/dotnet/standard/security/how-to-encrypt-xml-elements-with-symmetric-keys">Microsoft Docs</a></figcaption></figure><p>There are so many resources out there that can help you if you are trying to add more security to your platform. And the best practices can really save you and your company later down the line. Personally I have never had to write production encryption code, but doing research on the topic makes me want to know more about the&nbsp;topic!</p>
<p><a href="https://twitter.com/maurerpwer">Tweet me</a> if you work with encryption and any challenges that you&nbsp;face!</p>
<img src="https://medium.com/_/stat?event=post.clientViewed&amp;referrerSource=full_rss&amp;postId=e5d9a49621db" width="1" height="1" alt="">