using System; using System.IO; using System.Security.Cryptography; using System.Text; /* Created: 07/09/2017 * Author: Tumelo * * Update: 14/03/2019 * MxoSibeko - EncryptString and DecryptString code from http://asptipsandtricks.blogspot.com/2018/08/url-friendly-encryption-and-decryption-in-csharp.html */ namespace BuddyFinance.Common.Encryption { public static class EncryptionExtentions { //private const string HashAlgorithm = "SHA1"; //private const string Salt = "P3pP3R"; //private const string InitialVector = "16CHARSLONG12345"; //private const int KeySize = 256; //private const int PasswordIterations = 5; ////private const string Password = "App@rr3l$"; // ?? $Bu66yF1nanc3$ //private const string Password = "BuddyFinance"; // ?? $Bu66yF1nanc3$ public static string Encrypt(this string value, string hash = null) { if (string.IsNullOrEmpty(value)) return ""; return EncryptString(value); //var hasher = hash ?? Password; //try //{ // byte[] InitialVectorBytes = Encoding.ASCII.GetBytes(InitialVector); // byte[] SaltValueBytes = Encoding.ASCII.GetBytes(Salt); // byte[] PlainValueBytes = Encoding.ASCII.GetBytes(value); // using (PasswordDeriveBytes DerivedPassword = new PasswordDeriveBytes( // hasher, SaltValueBytes, HashAlgorithm, PasswordIterations)) // { // byte[] KeyBytes = DerivedPassword.GetBytes(KeySize / 8); // using (RijndaelManaged SymmetricKey = new RijndaelManaged()) // { // SymmetricKey.Mode = CipherMode.CBC; // //SymmetricKey.Padding = PaddingMode.Zeros; // byte[] CipherTextBytes = null; // using (ICryptoTransform Encryptor = SymmetricKey.CreateEncryptor(KeyBytes, InitialVectorBytes)) // { // using (MemoryStream stream = new MemoryStream()) // { // using (CryptoStream CryptoStream = new CryptoStream(stream, Encryptor, CryptoStreamMode.Write)) // { // CryptoStream.Write(PlainValueBytes, 0, PlainValueBytes.Length); // CryptoStream.FlushFinalBlock(); // CipherTextBytes = stream.ToArray(); // stream.Close(); // CryptoStream.Close(); // } // } // } // var returnVal = Convert.ToBase64String(CipherTextBytes); // return returnVal.Replace("/", "-").Replace("+", " "); // } // } //} //catch (Exception ex) //{ // throw ex; //} } public static string Decrypt(this string value, string hasher = null) { if (string.IsNullOrEmpty(value)) return ""; return DecryptString(value); //value = value.Replace("-", "/").Replace(" ", "/"); //try //{ // byte[] InitialVectorBytes = Encoding.ASCII.GetBytes(InitialVector); // byte[] SaltValueBytes = Encoding.ASCII.GetBytes(Salt); // byte[] CipherValueBytes = Convert.FromBase64String(value); // using (PasswordDeriveBytes DerivedPassword = new PasswordDeriveBytes(!string.IsNullOrWhiteSpace(hasher) ? hasher : Password, SaltValueBytes, HashAlgorithm, PasswordIterations)) // { // byte[] KeyBytes = DerivedPassword.GetBytes(KeySize / 8); // using (RijndaelManaged SymmetricKey = new RijndaelManaged()) // { // SymmetricKey.Mode = CipherMode.CBC; // //SymmetricKey.Padding = PaddingMode.Zeros; // byte[] PlainTextBytes = new byte[CipherValueBytes.Length]; // int ByteCount = 0; // using (ICryptoTransform Decryptor = SymmetricKey.CreateDecryptor(KeyBytes, InitialVectorBytes)) // { // using (MemoryStream stream = new MemoryStream(CipherValueBytes)) // { // using (CryptoStream crypto = new CryptoStream(stream, Decryptor, CryptoStreamMode.Read)) // { // ByteCount = crypto.Read(PlainTextBytes, 0, CipherValueBytes.Length); // stream.Close(); // crypto.Close(); // } // } // } // return Encoding.UTF8.GetString(PlainTextBytes, 0, ByteCount); // } // } //} //catch (Exception ex) //{ // throw ex; //} } public static string ToEncryptedString(this int id) { return Encrypt(id.ToString()); } public static int ToDecryptedInt(this string encryptedString) { if (string.IsNullOrEmpty(encryptedString)) return 0; return int.Parse(Decrypt(encryptedString)); } public static string ToBase64Encrypt(this string value, string hash) { string unencrypted = value + hash; var bytes = Encoding.UTF8.GetBytes(unencrypted); return Convert.ToBase64String(bytes); } static readonly char[] padding = { '=' }; static readonly string encryptionKey = "JHK@!$RTYEWQRGH"; public static string EncryptString(string clearText) { try { byte[] clearBytes = Encoding.Unicode.GetBytes(clearText); using (Aes encryptor = Aes.Create()) { Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(encryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 }); encryptor.Key = pdb.GetBytes(32); encryptor.IV = pdb.GetBytes(16); using (MemoryStream ms = new MemoryStream()) { using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write)) { cs.Write(clearBytes, 0, clearBytes.Length); cs.Close(); } clearText = Convert.ToBase64String(ms.ToArray()).TrimEnd(padding).Replace('+', '-').Replace('/', '_'); } } return clearText; } catch { } return ""; } public static string DecryptString(string cipherText) { string incoming = cipherText.Replace('_', '/').Replace('-', '+'); switch (cipherText.Length % 4) { case 2: incoming += "=="; break; case 3: incoming += "="; break; } try { byte[] cipherBytes = Convert.FromBase64String(incoming); using (Aes encryptor = Aes.Create()) { Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(encryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 }); encryptor.Key = pdb.GetBytes(32); encryptor.IV = pdb.GetBytes(16); using (MemoryStream ms = new MemoryStream()) { using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write)) { cs.Write(cipherBytes, 0, cipherBytes.Length); cs.Close(); } cipherText = Encoding.Unicode.GetString(ms.ToArray()); } } return cipherText; } catch { } return ""; } } }