{"id":185,"date":"2016-03-30T21:01:13","date_gmt":"2016-03-30T20:01:13","guid":{"rendered":"https:\/\/solidt.eu\/blog\/?p=185"},"modified":"2022-07-26T08:22:13","modified_gmt":"2022-07-26T07:22:13","slug":"c-encryption-helper","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/c-encryption-helper\/","title":{"rendered":"C# encryption helper"},"content":{"rendered":"\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"csharp\" data-theme=\"monokai\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\" data-copy=\"false\">using System;\nusing System.IO;\nusing System.Security.Cryptography;\nusing System.Text;\n\npublic static class EncryptionHelper\n{\n    static readonly string PasswordHash = \"The pAsswordHash\";\n    static readonly string SaltKey = \"tHe sAltkEy5983479857349802759837\";\n    static readonly string CustomKey = \"HR$2pIjHR$2pIj12\";\n\n    public static string Encrypt(string plainText)\n    {\n        byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);\n\n        byte[] keyBytes = new Rfc2898DeriveBytes(PasswordHash, Encoding.ASCII.GetBytes(SaltKey)).GetBytes(256 \/ 8);\n        using (var symmetricKey = Aes.Create())\n        {\n            symmetricKey.Padding = PaddingMode.Zeros;\n            var encryptor = symmetricKey.CreateEncryptor(keyBytes, Encoding.ASCII.GetBytes(CustomKey));\n\n            byte[] cipherTextBytes;\n\n            using (var memoryStream = new MemoryStream())\n            {\n                using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))\n                {\n                    cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);\n                    cryptoStream.FlushFinalBlock();\n                    cipherTextBytes = memoryStream.ToArray();\n                    cryptoStream.Close();\n                }\n                memoryStream.Close();\n            }\n            return Convert.ToBase64String(cipherTextBytes);\n        }\n    }\n\n    public static string Decrypt(string encryptedText)\n    {\n        byte[] cipherTextBytes = Convert.FromBase64String(encryptedText);\n        byte[] keyBytes = new Rfc2898DeriveBytes(PasswordHash, Encoding.ASCII.GetBytes(SaltKey)).GetBytes(256 \/ 8);\n\n        using (var symmetricKey = Aes.Create())\n        {\n            symmetricKey.Padding = PaddingMode.Zeros;\n            var decryptor = symmetricKey.CreateDecryptor(keyBytes, Encoding.ASCII.GetBytes(CustomKey));\n            var memoryStream = new MemoryStream(cipherTextBytes);\n            var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read);\n            byte[] plainTextBytes = new byte[cipherTextBytes.Length];\n\n            int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);\n            memoryStream.Close();\n            cryptoStream.Close();\n            return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount).TrimEnd(\"\\0\".ToCharArray());\n        }\n    }\n}<\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[6],"tags":[],"class_list":["post-185","post","type-post","status-publish","format-standard","hentry","category-dotnet"],"_links":{"self":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/185","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/comments?post=185"}],"version-history":[{"count":4,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/185\/revisions"}],"predecessor-version":[{"id":6418,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/185\/revisions\/6418"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=185"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=185"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=185"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}