{"id":79884,"date":"2023-01-12T08:00:29","date_gmt":"2023-01-12T07:00:29","guid":{"rendered":"https:\/\/code-maze.com\/?p=79884"},"modified":"2023-01-12T17:51:06","modified_gmt":"2023-01-12T16:51:06","slug":"dotnet-cryptography-implementations","status":"publish","type":"post","link":"https:\/\/code-maze.com\/dotnet-cryptography-implementations\/","title":{"rendered":"Cryptography Implementations in .NET"},"content":{"rendered":"<p>Cryptography is the security backbone of what our modern society is built upon. This science has enabled us to keep data and communication safe from being compromised or stolen. We can find examples of its use in personal password managers up to the security aspect of HTTPS communication. In this article, we will learn more about cryptography and how we can apply it in code.<\/p>\n<div style=\"padding: 20px; border-left: 5px #dc2323 solid; display: block; margin-bottom: 20px; box-shadow: 1px 1px 5px 0px lightgrey;\">To download the source code for this article, you can visit our <a href=\"https:\/\/github.com\/CodeMazeBlog\/CodeMazeGuides\/tree\/main\/authorization-dotnet\/CryptographyDotnet\" target=\"_blank\" rel=\"nofollow noopener\">GitHub repository<\/a>.<\/div>\n<p>Let&#8217;s start.<\/p>\n<h2>What is Cryptography<\/h2>\n<p>Cryptography is the study, development, and application of mathematical algorithms on data or communications to <strong>secure the data from being easily read or unencrypted<\/strong>. Cryptographic algorithms are applicable in multiple ways to achieve goals. We can generate cryptographic keys from them, hash data, digitally sign files and programs, enable safe payment online, and encrypt data or communications.<\/p>\n<p>We can access the .NET library of cryptographic algorithms through the <code>System.Security.Cryptography<\/code> namespace.<\/p>\n<p>In this article, we will discuss what hashing is and how it is different from encryption. We will cover the major points of encryption and dive into different encryption methods.\u00a0<\/p>\n<h2>Hash Functions in Cryptography<\/h2>\n<p>Cryptographic <strong>hashing functions are one-way mathematical algorithms<\/strong>. Hash functions map a data set of any size down to a byte array of fixed size. It is essential to understand that <strong>hashing data is irreversible<\/strong>. This is an important feature of hashing and one we can take advantage of to validate data is correct without having to reveal it. Another property of a hash function is that they are deterministic algorithms. This means it produces the same result for a given input every time. Lastly, no two distinct data sets should produce the same hash value or output. When a hashing algorithm produces the same output for different inputs, it causes a <strong>collision<\/strong>. These three properties are what make hashing a great method to use for security in cryptography.<\/p>\n<p>Because hash functions take inputs of any size and always produce an output of fixed size, it is inevitable that collisions will occur. Although unlikely, this can be an issue when absolute data security is a priority. A good way to avoid a situation like this is to use algorithms that are not deprecated or cryptographically broken. Moreover, using hashing with larger outputs can make collisions statistically very probable. For example, although SHA256 is 60% slower than MD5 the chances of a collision are 4.3*10<sup>&#8211;<\/sup><sup>60<\/sup>.<\/p>\n<h3>How Hash Functions Are Utilized<\/h3>\n<p>We see examples of hashing being used in a variety of settings. For example, it is standard practice that companies with user accounts <strong>do not store passwords in plain text, but rather hashes of the passwords.<\/strong> We use hashing when verifying the integrity of a file.<\/p>\n<p>Let&#8217;s say we received a sensitive executable file. We would first like to verify the file has not been tampered with before running it. If we were to receive a hash of the original file, we could run the hashing on the copy of the file we have to see if the file is the same. Remember, <strong>hashing algorithms produce the same output for a given input<\/strong>. Lastly, hashing is used in blockchain technology as proof of work.<\/p>\n<h3>MD5<\/h3>\n<p>Message-digest algorithm, or MD5, is a widely used hashing algorithm. This algorithm is widely used, but it should be recognized that <strong>MD5 is cryptographically broken<\/strong>. This means there is a way to manipulate the algorithm to violate the three properties of hashing algorithms we discussed earlier. <strong>MD5 can still be used for checksum purposes to verify integrity<\/strong>, but only for unintentional corruption. It is still preferred in some cases where the added security of SHA-family hashing is less important a factor. This hash function is less computationally expensive than the SHA family of functions:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">var strStreamOne = new MemoryStream(Encoding.UTF8.GetBytes(\"This is my password! Dont read me!\"));\r\n\r\nbyte[] hashOne;\r\nusing (var hasher = MD5.Create())\r\n{\r\n    hashOne = await hasher.ComputeHashAsync(strStreamOne);\r\n}\r\n\r\nvar hashAsString = Convert.ToHexString(hashOne);\r\n\r\nConsole.WriteLine(\"Hash Value:\\n\" + hashAsString)<\/pre>\n<p>We use the <code>Create()<\/code> to make a default instance of the <code>MD5<\/code> class. We provide a <code>byte[]<\/code> created from our data payload.<\/p>\n<p>The hash is truly an array of bytes but we can convert it to a string format for readability. To visualize the data we convert it to a hex string by calling <code>Convert.ToHexString()<\/code>:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">Hash Value:\r\n5347BC359818CD57401561F4FBF5B0BF<\/pre>\n<h3>SHA Family<\/h3>\n<p>Secure Hashing Algorithm, or SHA, is a family of hashing algorithms that the National Institute of Standards and Technology (NIST) developed. Currently, the most recent version of SHA in use is SHA-3 which succeeds SHA-1 and SHA-2.<\/p>\n<p>SHA-2 is more popular as it has been in use safely for many years. There are multiple flavors of SHA-2. We will discuss SHA-256 and SHA-512. Although they have different block and output sizes, both follow the same algorithmic steps.<\/p>\n<p>Released in 2015, SHA-3 is still in the process of adoption. <strong>New cryptographic algorithms take time to be widely adopted as they must be studied for long periods<\/strong>. SHA-3 is fundamentally different than its predecessors as it is based on an algorithm formerly called Keccak. SHA-1 and SHA-2 were based on the MD-5 algorithm. <strong>SHA-3 is not available in .NET<\/strong>.<\/p>\n<p>Let&#8217;s look at an example of the SHA-256:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">var strStreamOne = new MemoryStream(Encoding.UTF8.GetBytes(\"This is my password! Dont read me!\"));\r\n\r\nbyte[] hashOne;\r\nusing (var sha256 = SHA256.Create())\r\n{\r\n    hashOne = await sha256.ComputeHashAsync(strStreamOne);\r\n}\r\n\r\nvar hashAsString = Convert.ToHexString(hashOne);\r\n\r\nConsole.WriteLine(\"Hash Value:\\n\" + hashAsString)<\/pre>\n<p>And then visualize the data:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">Hash Value:\r\n9F4C6711B093950EBA527F0AD86F17C6F64F956A341A2CD321213F0536245774<\/pre>\n<h3>HMAC<\/h3>\n<p><strong>HMAC is a hashing function that requires a secret key to hash data<\/strong>. We can use a hash-based message authentication code (HMAC) to verify the integrity of the data and the authentication of a message. Like any hashing algorithm, we can hash the data ourselves to verify there was no change in the data. Since HMAC requires a secret key, we can confirm that an authorized person made the hash.<\/p>\n<p>Let&#8217;s perform an HMAC hash:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">var strStreamOne = new MemoryStream(Encoding.UTF8.GetBytes(\"This is my password! Dont read me!\"));\r\n\r\nbyte[] hashOne;\r\nbyte[] key = Encoding.UTF8.GetBytes(\"superSecretH4shKey1!\");\r\nusing (var hmac = new HMACSHA256(key))\r\n{\r\n    hashOne = await hmac.ComputeHashAsync(strStreamOne);\r\n}\r\n\r\nvar\u00a0hashAsString\u00a0=\u00a0Convert.ToHexString(hashOne);\r\n\r\nConsole.WriteLine(\"Hash\u00a0Value:\\n\"\u00a0+\u00a0hashAsString)<\/pre>\n<p>And visualize the converted hex:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">Hash Value:\r\n1B24C3C6AC68397C8C58076C77C85DC6CB4713153369F1B92C183BCBF9DB8927\r\n<\/pre>\n<p>For more information about hashing visit this Code Maze <a href=\"https:\/\/code-maze.com\/csharp-hashing-salting-passwords-best-practices\/\" target=\"_blank\" rel=\"noopener\">article<\/a>.<\/p>\n<h2>Symmetric Encryption in Cryptography<\/h2>\n<p>Encrypting involves changing data in a way that obscures it. <strong>Decryption is the reversal of encryption<\/strong>.<\/p>\n<p>The first of two ways to encrypt data is Symmetric encryption. <strong>Symmetric encryption is a class of algorithms that can encrypt data with a private key and can decrypt data with the same key<\/strong>. This means we need a secure way to share this private key, or else anyone with the key can decrypt our data. <span style=\"color: #000000;\">We can refer to symmetric encryption as private key encryption.<\/span><\/p>\n<h3>AES<\/h3>\n<p>AES is the most popular symmetric encryption algorithm. This algorithm is <strong>fast and can encrypt data of any size<\/strong>. This is very important because we need to encrypt a large amount of data often. A slow algorithm can make a task like this very time-consuming.<\/p>\n<p><strong>AES features a 128-bit block size and 128, 192, and 256-bit key sizes<\/strong>. The variable key size is essential because we cannot tell the size by looking at the cipher text. Additionally, the key sizes are long when we consider other algorithms. This all makes for a safer, harder-to-break algorithm.<\/p>\n<p>Let&#8217;s perform an AES encryption:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">var dataStr = \"This is corporate research! Dont read me!\";\r\nvar data = Encoding.UTF8.GetBytes(dataStr);\r\nvar key = GenerateAESKey();\r\n\r\nvar encryptedData = Encrypt(data, key, out var iv);\r\n\r\nvar encryptedDataAsString = Convert.ToHexString(encryptedData);\r\n\r\nConsole.WriteLine(\"Encrypted Value:\\n\" + encryptedDataAsString);\r\n\r\npublic static byte[] Encrypt(byte[] data, byte[] key, out byte[] iv)\r\n{\r\n    using (var aes = Aes.Create())\r\n    {\r\n        aes.Mode = CipherMode.CBC; \/\/ better security\r\n        aes.Key = key;\r\n        aes.GenerateIV(); \/\/ IV = Initialization Vector\r\n\r\n        using (var encryptor = aes.CreateEncryptor())\r\n        {\r\n            iv = aes.IV;\r\n            return encryptor.TransformFinalBlock(data, 0, data.Length);\r\n        }\r\n    }\r\n}\r\n\r\npublic static byte[] Decrypt(byte[] data, byte[] key, byte[] iv)\r\n{\r\n    using (var aes = Aes.Create())\r\n    {\r\n        aes.Key = key;\r\n        aes.IV = iv;\r\n        aes.Mode = CipherMode.CBC; \/\/ same as for encryption\r\n\r\n        using (var decryptor = aes.CreateDecryptor())\r\n        {\r\n            return decryptor.TransformFinalBlock(data, 0, data.Length);\r\n        }\r\n    }\r\n}\r\n\r\npublic static byte[] GenerateAESKey()\r\n{\r\n    var rnd = new RNGCryptoServiceProvider();\r\n    var b = new byte[16];\r\n    rnd.GetNonZeroBytes(b);\r\n\r\n    return b;\r\n}<\/pre>\n<p>Finally, let&#8217;s visualize the data:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">Encrypted Value:\r\n46180CD8EF50860880F7D5885FF1B96A3ABA3A319FBB2374CF14705C64D66EEB050551E2E799D6300DB4F3E654A56F6D<\/pre>\n<p>In this example, we use AES to encrypt our data. It is important to note that if we provide a key of the wrong size, .NET throws an exception. In this case, we are using a 16-byte <code>key<\/code>. We are using the CBC cipher mode. We use <code>GenerateIV()<\/code> to create an IV.<strong> IV stands for the initialization vector<\/strong>. The vector is a random value. The purpose of the IV is to produce different cipher text for the same input. It is also required for decryption. <code>Aes.CreateEncryptor()<\/code> returns an object on which we can call <code>TransformFinalBlock()<\/code> to perform the encryption.<\/p>\n<h3>Cryptography Cipher Modes<\/h3>\n<p>We can set the cipher mode we want to use by setting <code>aes.Mode<\/code> to one of four values. The cipher modes are enumerated by the <code>CipherMode<\/code> enum.\u00a0<\/p>\n<p><strong>The Cipher Block Chaining<\/strong> mode utilizes feedback from a previous block when encrypting the current block. The current block&#8217;s encryption includes a part of the cipher text of the previous block. This method assures that identical blocks result in different encrypted values.<\/p>\n<p><strong>The Electronic Codebook<\/strong> mode encrypts each block individually. If we use the same key, identical blocks of plain text will have the same encryption value. This mode is not practical as it introduces vulnerabilities to the encryption.<\/p>\n<p><strong>The Cipher Feedback<\/strong> mode encrypts small increments of data smaller than the block size.<\/p>\n<p><strong>The Cipher Text Stealing<\/strong> mode can produce an encrypted output the same size as the original unencrypted data set. CTS behaves exactly like CBC for all blocks except the last two blocks.<\/p>\n<p>In this example, we used CBC because it is a safer option to use when encrypting. It closes off opportunities someone can exploit to retrieve our secret data.<\/p>\n<h3>DES and Triple DES<\/h3>\n<p>.NET also offers support for DES and Triple DES. Data Encryption Standard (DES) is a symmetric encryption algorithm that preceded AES. Triple DES applies DES three times to each block. DES was once a formidable encryption, but by today&#8217;s standards, <strong>it is no longer practical to use DES<\/strong>. This is because DES uses a 56-bit key length which makes it easier to break when compared to other algorithms with longer keys.<\/p>\n<h2>Asymmetric Encryption in Cryptography<\/h2>\n<p><span style=\"color: #000000;\">Asymmetric encryption is an encryption method where data is <strong>encrypted by a publicly available key and decrypted by a private key<\/strong>. The public and private keys are mathematically related. This means only a private key can decrypt data that the related public key encrypts. These algorithms typically have a fixed buffer size. We can refer to asymmetric encryption as public key encryption. <\/span><\/p>\n<p><span style=\"color: #000000;\">Typically asymmetric encryption is much slower than symmetric encryption.<strong> It is not practical to use asymmetric encryption on a large data set.\u00a0<\/strong><\/span><\/p>\n<h3>RSA<\/h3>\n<p>RSA is a popular public key algorithm that is a standard algorithm for asymmetric encryption. It is old for a secure cryptographic algorithm. <strong>Given a long enough key, there is no proven method for breaking RSA<\/strong>. This means it has withstood the test of time and is still a secure option for encryption. RSA can only handle encrypting blocks of data the size of the key used in the encryption. Data sets larger than the key size must be split into multiple blocks. It is traditional to use the power of two key sizes, but this is not a requirement.<\/p>\n<p>We commonly use RSA for key exchange and digital signing. This is because encryption of large data sets would be slow using RSA. Practical uses of RSA include digital signing, secure messaging applications, and secure connections such as SSL and VPN connections:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">var dataStr = \"This is corporate research! Dont read me!\";\r\nvar data = Encoding.UTF8.GetBytes(dataStr);\r\nvar keyLength = 2048; \/\/ size in bits\r\n\r\nGenerateKeys(keyLength , out var publicKey, out var privateKey);\r\n\r\nvar encryptedData = Encrypt(data, publicKey);\r\n\r\nvar encryptedDataAsString = Convert.ToHexString(encryptedData);\r\n\r\nConsole.WriteLine(\"Encrypted\u00a0Value:\\n\" + encryptedDataAsString);\r\n\r\npublic void GenerateKeys(int keyLength, out RSAParameters publicKey, out RSAParameters privateKey)\r\n{\r\n    using (var rsa = RSA.Create())\r\n    {\r\n        rsa.KeySize = keyLength;\r\n        publicKey = rsa.ExportParameters(includePrivateParameters: false);\r\n        privateKey = rsa.ExportParameters(includePrivateParameters: true);\r\n    }\r\n}\r\n\r\npublic byte[] Encrypt(byte[] data, RSAParameters publicKey)\r\n{\r\n    using (var rsa = RSA.Create())\r\n    {\r\n        rsa.ImportParameters(publicKey);\r\n\r\n        var result = rsa.Encrypt(data, RSAEncryptionPadding.OaepSHA256);\r\n        return result;\r\n    }\r\n}\r\n\r\npublic byte[] Decrypt(byte[] data, RSAParameters privateKey)\r\n{\r\n    using (var rsa = RSA.Create())\r\n    {\r\n        rsa.ImportParameters(privateKey);\r\n        return rsa.Decrypt(data, RSAEncryptionPadding.OaepSHA256);\r\n    }\r\n}<\/pre>\n<p>In this example, we set <code>rsa.KeySize<\/code> and <code>ExportParameters()<\/code> to generate a public and private key. We can use the public key to encrypt our data in the <code>Encrypt()<\/code> method. Similarly, we can decrypt data using our private key in the <code>Decrypt()<\/code> method. It is important to note that if the key size is too small we will not be able to create a set of cryptographic keys. <strong>A standard and safe key size is 2048 bits<\/strong>.<\/p>\n<p>Let&#8217;s check how the encrypted data looks in a hex string format:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">Encrypted Value:\r\n5B63ADA5C1ADC2E2E4884C6B7157BE7A5C2F562CF089E1DE9225968F4F0226E0234A685E7CD47D02B4AD1653100F7F3B5A8050B5\r\n0B5CCBD162EAF62C65A3C61E284C7337352173403A71FF395843A0F3B9A50E85BF25E5344285EFF054B82B29B56BF9AC77A33D8C\r\nDCBDD6FD055B2E843B2FC467F64D879D974CC8CBDEC9B78714F3286B27CD316175F6FB453E6C2264BC45645FBCAB1D6844E83B3D\r\n37FE2DA00732EA5F11F45BF40BE16810BA1FE15B88C267D90C6164A960A2690CB095F0D2A8AF816DB337E67A30882EAD94B68DF3\r\n2F9B1A27FDC2A5467ED8810A95239D7D73CACF9AD0C5FE1D717D7C730328BB191D743DE04549CA0C165D600ACD57FF60<\/pre>\n<h3>DSA<\/h3>\n<p>The Digital Signing Algorithm, or DSA, is another public key algorithm. We use DSA for <strong>key generation and digital signing<\/strong>. We can use DSA for encryption, but it is not typical. Compared to RSA, DSA is faster at key generation, digital signing, and decryption. On the other hand, RSA is faster at encrypting, and digital signature verification:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">var dsa = DSA.Create();\r\nvar dataStr = \"This is corporate research! Dont read me!\";\r\nvar data = Encoding.UTF8.GetBytes(dataStr);\r\n\r\nvar signedData = Sign(dsa, data);\r\n\r\ndsa.Dispose();\r\n\r\npublic byte[] Sign(DSA dsa, byte[] data)\r\n{\r\n    if(dsa is null)\r\n        throw new NullReferenceException(nameof(dsa));\r\n\r\n    var result = dsa.SignData(data, HashAlgorithmName.SHA256);\r\n\r\n    return result;\r\n}\r\n\r\npublic bool VerifySignature(DSA dsa, byte[] data, byte[] signedData)\r\n{\r\n    if (dsa is null)\r\n        throw new NullReferenceException(nameof(dsa));\r\n\r\n    return dsa.VerifyData(data, signedData, HashAlgorithmName.SHA256);\r\n}\r\n<\/pre>\n<p>Here, we use DSA to digitally sign data. This data can be a text file, executable, or email. We sign the data by calling <code>SignData()<\/code>. We can verify the validity of signed data by calling <code>VerifyData()<\/code>.\u00a0<\/p>\n<h2>Choosing the Right Algorithm in Cryptography<\/h2>\n<p>When deciding which encryption method and implementation to use, it is a good idea to base our decision on the parameters of our application of the encryption. What does our data look like? Do we have a lot of data to encrypt? Is performance a factor? What level of security must we ensure? How can we share or transmit this data? Do we need to transfer data or just prove the data is authentic? All of these answers will lead us to make an appropriate choice of which algorithm to employ.\u00a0<\/p>\n<p><strong>Security must always be a top priority in these decisions<\/strong>. We must strive to choose the safest method considering our use case.\u00a0<\/p>\n<h3>Asymmetric Encryption<\/h3>\n<p>If a data set is not large, asymmetric encryption would be the safest choice as long as we can safely share the private key with the party needing to decrypt the data. Asymmetric encryption is not always applicable to all cases. This is true for large data sets, as they will take much longer to encrypt. Asymmetric encryption is also not a good choice if we only need to validate data is authentic rather than a secret. This would be a job more suited for a hashing algorithm.<\/p>\n<h3>Symmetric Encryption<\/h3>\n<p><strong>Symmetric encryption is useful when the data set is large<\/strong>. This is because symmetric encryption is typically much faster than asymmetric. This, of course, offers less security than asymmetric encryption. This is a trade-off we must consider. If the situation calls for performant encryption, the loss of security may not be as important. Of course, symmetric encryption is still safe, but it is less safe than asymmetric.<\/p>\n<h3>Hash Function<\/h3>\n<p>Sometimes the issue we want to solve is not to keep our data secret but rather that the data transmission is accurate. We must verify that there are no changes between the data we sent and the data received. We can employ hashing algorithms in a situation like this. Hashing algorithms are typically faster than encryption. <strong>We should use hashing rather than encryption when the goal is to validate data rather than hide it.<\/strong><\/p>\n<h3>Hybrid Encryption<\/h3>\n<p>Ideally, we can take advantage of public and private key encryption in unison to achieve performant and safe encryption. <strong>We can encrypt a large data set with the speed of symmetric encryption and the security of asymmetric encryption<\/strong>. First, a symmetric algorithm encrypts the data. Next, an asymmetric algorithm encrypts the private key. This way, we can share the original private key while not taking a hit on performance.<\/p>\n<p>We have covered many topics on encryption, but for further reading, visit the official <a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/standard\/security\/encrypting-data\" target=\"_blank\" rel=\"nofollow noopener\">Microsoft .NET documentation<\/a>.<\/p>\n<h2>Cryptographic Random Numbers<\/h2>\n<p>It is important to note the difference between random values and cryptographic random values that we use in cryptography. This is important because the security of a system is based on the assumption that it is generating random numbers. Using <code>System.Random<\/code> is a performant easy way to get random values, but this implementation is <strong>not truly random<\/strong>. <code>System.Random<\/code> uses a seed to generate values from. The same seed will produce the same order of values. Similar seeds produce similar values. This could lead to vulnerabilities a bad actor can use.<\/p>\n<p><code>RandomNumberGenerator<\/code> provides a great way to safely generate random numbers. Using this class will increase the security of our code:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">var randomNumGenerator = RandomNumberGenerator.Create();\r\nvar data = new byte[length];\r\nrandomNumGenerator.GetBytes(data);\r\n    \r\nreturn data ;<\/pre>\n<p>In this snippet, we use the <code>Create<\/code> method from a <code>RandomNumberGenerator<\/code> and use it to generate random bytes, a <code>length<\/code> number of times. We can convert these bytes to other types like <code>int<\/code>, <code>char<\/code>, and <code>string<\/code>.<\/p>\n<p>Find further reading on Code Maze about random numbers in this <a href=\"https:\/\/code-maze.com\/csharp-generate-random-numbers-range\/\" target=\"_blank\" rel=\"noopener\">article<\/a>.<\/p>\n<h2>Conclusion<\/h2>\n<p>In conclusion, there are many real-world applications for cryptography. <strong>Many current technologies use encryption security benefits to protect our information<\/strong>. Much of which we do not see, such as HTTPS and SSL. There are many algorithms in cryptography with varying levels of security and performance. Choosing the right algorithm for an application can provide great security for the user and ensure that we utilize cryptography in the right way.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Cryptography is the security backbone of what our modern society is built upon. This science has enabled us to keep data and communication safe from being compromised or stolen. We can find examples of its use in personal password managers up to the security aspect of HTTPS communication. In this article, we will learn more [&hellip;]<\/p>\n","protected":false},"author":42,"featured_media":62191,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","footnotes":""},"categories":[12],"tags":[1582,1577,1581,1579,1578,1580],"class_list":["post-79884","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-csharp","tag-asymmetric-encryption-in-cryptography","tag-cryptography","tag-hash-functions-in-cryptography","tag-hmac-alghoritm","tag-sha-alghoritm","tag-symmetric-encryption-in-cryptography","et-has-post-format-content","et_post_format-et-post-format-standard"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Cryptography Implementations in .NET - Code Maze<\/title>\n<meta name=\"description\" content=\"In this article we learn about different cryptography implementations in .NET. We see how to use different approaches for specific use cases.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/code-maze.com\/dotnet-cryptography-implementations\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Cryptography Implementations in .NET - Code Maze\" \/>\n<meta property=\"og:description\" content=\"In this article we learn about different cryptography implementations in .NET. We see how to use different approaches for specific use cases.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/code-maze.com\/dotnet-cryptography-implementations\/\" \/>\n<meta property=\"og:site_name\" content=\"Code Maze\" \/>\n<meta property=\"article:published_time\" content=\"2023-01-12T07:00:29+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-01-12T16:51:06+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-dotnet-core.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1100\" \/>\n\t<meta property=\"og:image:height\" content=\"620\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Alvaro Montoya\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@CodeMazeBlog\" \/>\n<meta name=\"twitter:site\" content=\"@CodeMazeBlog\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Alvaro Montoya\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"14 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":[\"Article\",\"BlogPosting\"],\"@id\":\"https:\/\/code-maze.com\/dotnet-cryptography-implementations\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/code-maze.com\/dotnet-cryptography-implementations\/\"},\"author\":{\"name\":\"Alvaro Montoya\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/78b0d58c590fbaaac88b126b3c588f4e\"},\"headline\":\"Cryptography Implementations in .NET\",\"datePublished\":\"2023-01-12T07:00:29+00:00\",\"dateModified\":\"2023-01-12T16:51:06+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/code-maze.com\/dotnet-cryptography-implementations\/\"},\"wordCount\":2540,\"commentCount\":2,\"publisher\":{\"@id\":\"https:\/\/code-maze.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/dotnet-cryptography-implementations\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-dotnet-core.png\",\"keywords\":[\"Asymmetric Encryption in Cryptography\",\"Cryptography\",\"Hash Functions in Cryptography\",\"HMAC alghoritm\",\"SHA alghoritm\",\"Symmetric Encryption in Cryptography\"],\"articleSection\":[\"C#\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/code-maze.com\/dotnet-cryptography-implementations\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/code-maze.com\/dotnet-cryptography-implementations\/\",\"url\":\"https:\/\/code-maze.com\/dotnet-cryptography-implementations\/\",\"name\":\"Cryptography Implementations in .NET - Code Maze\",\"isPartOf\":{\"@id\":\"https:\/\/code-maze.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/code-maze.com\/dotnet-cryptography-implementations\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/dotnet-cryptography-implementations\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-dotnet-core.png\",\"datePublished\":\"2023-01-12T07:00:29+00:00\",\"dateModified\":\"2023-01-12T16:51:06+00:00\",\"description\":\"In this article we learn about different cryptography implementations in .NET. We see how to use different approaches for specific use cases.\",\"breadcrumb\":{\"@id\":\"https:\/\/code-maze.com\/dotnet-cryptography-implementations\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/code-maze.com\/dotnet-cryptography-implementations\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/dotnet-cryptography-implementations\/#primaryimage\",\"url\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-dotnet-core.png\",\"contentUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-dotnet-core.png\",\"width\":1100,\"height\":620,\"caption\":\".NET (Core)\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/code-maze.com\/dotnet-cryptography-implementations\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/code-maze.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Cryptography Implementations in .NET\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/code-maze.com\/#website\",\"url\":\"https:\/\/code-maze.com\/\",\"name\":\"Code Maze\",\"description\":\"Learn. Code. Succeed.\",\"publisher\":{\"@id\":\"https:\/\/code-maze.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/code-maze.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/code-maze.com\/#organization\",\"name\":\"Code Maze\",\"url\":\"https:\/\/code-maze.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png\",\"contentUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png\",\"width\":3511,\"height\":3510,\"caption\":\"Code Maze\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/CodeMazeBlog\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/78b0d58c590fbaaac88b126b3c588f4e\",\"name\":\"Alvaro Montoya\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2023\/12\/Alvaro-Montoya-400.jpg-150x150.png\",\"contentUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2023\/12\/Alvaro-Montoya-400.jpg-150x150.png\",\"caption\":\"Alvaro Montoya\"},\"description\":\"Alvaro has been professionally developing solutions in .NET for over eight years. He has worked in simulations, flight planning, and most recently a cloud-based solution for healthcare prior authorization. Over the years, He has worked on developing desktop applications, web applications, .NET APIs, and cloud-based distributed solutions in Azure and AWS. Outside of work, Alvaro enjoys playing video games and playing basketball.\",\"sameAs\":[\"https:\/\/www.linkedin.com\/in\/alvaro-montoya-096589108\/\"],\"url\":\"https:\/\/code-maze.com\/author\/alvaromontoya\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Cryptography Implementations in .NET - Code Maze","description":"In this article we learn about different cryptography implementations in .NET. We see how to use different approaches for specific use cases.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/code-maze.com\/dotnet-cryptography-implementations\/","og_locale":"en_US","og_type":"article","og_title":"Cryptography Implementations in .NET - Code Maze","og_description":"In this article we learn about different cryptography implementations in .NET. We see how to use different approaches for specific use cases.","og_url":"https:\/\/code-maze.com\/dotnet-cryptography-implementations\/","og_site_name":"Code Maze","article_published_time":"2023-01-12T07:00:29+00:00","article_modified_time":"2023-01-12T16:51:06+00:00","og_image":[{"width":1100,"height":620,"url":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-dotnet-core.png","type":"image\/png"}],"author":"Alvaro Montoya","twitter_card":"summary_large_image","twitter_creator":"@CodeMazeBlog","twitter_site":"@CodeMazeBlog","twitter_misc":{"Written by":"Alvaro Montoya","Est. reading time":"14 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["Article","BlogPosting"],"@id":"https:\/\/code-maze.com\/dotnet-cryptography-implementations\/#article","isPartOf":{"@id":"https:\/\/code-maze.com\/dotnet-cryptography-implementations\/"},"author":{"name":"Alvaro Montoya","@id":"https:\/\/code-maze.com\/#\/schema\/person\/78b0d58c590fbaaac88b126b3c588f4e"},"headline":"Cryptography Implementations in .NET","datePublished":"2023-01-12T07:00:29+00:00","dateModified":"2023-01-12T16:51:06+00:00","mainEntityOfPage":{"@id":"https:\/\/code-maze.com\/dotnet-cryptography-implementations\/"},"wordCount":2540,"commentCount":2,"publisher":{"@id":"https:\/\/code-maze.com\/#organization"},"image":{"@id":"https:\/\/code-maze.com\/dotnet-cryptography-implementations\/#primaryimage"},"thumbnailUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-dotnet-core.png","keywords":["Asymmetric Encryption in Cryptography","Cryptography","Hash Functions in Cryptography","HMAC alghoritm","SHA alghoritm","Symmetric Encryption in Cryptography"],"articleSection":["C#"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/code-maze.com\/dotnet-cryptography-implementations\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/code-maze.com\/dotnet-cryptography-implementations\/","url":"https:\/\/code-maze.com\/dotnet-cryptography-implementations\/","name":"Cryptography Implementations in .NET - Code Maze","isPartOf":{"@id":"https:\/\/code-maze.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/code-maze.com\/dotnet-cryptography-implementations\/#primaryimage"},"image":{"@id":"https:\/\/code-maze.com\/dotnet-cryptography-implementations\/#primaryimage"},"thumbnailUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-dotnet-core.png","datePublished":"2023-01-12T07:00:29+00:00","dateModified":"2023-01-12T16:51:06+00:00","description":"In this article we learn about different cryptography implementations in .NET. We see how to use different approaches for specific use cases.","breadcrumb":{"@id":"https:\/\/code-maze.com\/dotnet-cryptography-implementations\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/code-maze.com\/dotnet-cryptography-implementations\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/dotnet-cryptography-implementations\/#primaryimage","url":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-dotnet-core.png","contentUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-dotnet-core.png","width":1100,"height":620,"caption":".NET (Core)"},{"@type":"BreadcrumbList","@id":"https:\/\/code-maze.com\/dotnet-cryptography-implementations\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/code-maze.com\/"},{"@type":"ListItem","position":2,"name":"Cryptography Implementations in .NET"}]},{"@type":"WebSite","@id":"https:\/\/code-maze.com\/#website","url":"https:\/\/code-maze.com\/","name":"Code Maze","description":"Learn. Code. Succeed.","publisher":{"@id":"https:\/\/code-maze.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/code-maze.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/code-maze.com\/#organization","name":"Code Maze","url":"https:\/\/code-maze.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/#\/schema\/logo\/image\/","url":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png","contentUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png","width":3511,"height":3510,"caption":"Code Maze"},"image":{"@id":"https:\/\/code-maze.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/CodeMazeBlog"]},{"@type":"Person","@id":"https:\/\/code-maze.com\/#\/schema\/person\/78b0d58c590fbaaac88b126b3c588f4e","name":"Alvaro Montoya","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/#\/schema\/person\/image\/","url":"https:\/\/code-maze.com\/wp-content\/uploads\/2023\/12\/Alvaro-Montoya-400.jpg-150x150.png","contentUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2023\/12\/Alvaro-Montoya-400.jpg-150x150.png","caption":"Alvaro Montoya"},"description":"Alvaro has been professionally developing solutions in .NET for over eight years. He has worked in simulations, flight planning, and most recently a cloud-based solution for healthcare prior authorization. Over the years, He has worked on developing desktop applications, web applications, .NET APIs, and cloud-based distributed solutions in Azure and AWS. Outside of work, Alvaro enjoys playing video games and playing basketball.","sameAs":["https:\/\/www.linkedin.com\/in\/alvaro-montoya-096589108\/"],"url":"https:\/\/code-maze.com\/author\/alvaromontoya\/"}]}},"_links":{"self":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/79884","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/users\/42"}],"replies":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/comments?post=79884"}],"version-history":[{"count":8,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/79884\/revisions"}],"predecessor-version":[{"id":79961,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/79884\/revisions\/79961"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/media\/62191"}],"wp:attachment":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/media?parent=79884"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/categories?post=79884"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/tags?post=79884"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}