/// <summary> /// Encrypt String Method /// Example : /// var strencrypt = EncryptionHelper.AES_EncryptText("PrivateKey", "123456789") /// Result : /// cGBffELTG_R97rS_BYhNww /// </summary> /// <param name="strEncode"></param> /// <param name="txtValue"></param> /// <returns></returns> public static string AES_EncryptText(string strEncode, string txtValue) { try { // Get the bytes of the string byte[] bytesToBeEncrypted = Encoding.UTF8.GetBytes(txtValue); byte[] EncodeBytes = Encoding.UTF8.GetBytes(strEncode); // Hash the password with SHA256 EncodeBytes = SHA256.Create().ComputeHash(EncodeBytes); byte[] bytesEncrypted = AES_Encrypt(bytesToBeEncrypted, EncodeBytes); //string result = Convert.ToBase64String(bytesEncrypted); // Base64UrL Encrypt for web string result = Base64UrlHelper.Encode(bytesEncrypted); return(result); } catch (Exception ex) { return("Error :" + ex.Message); } }
/// <summary> /// Decrypt String Method /// Example : /// var strdecrypt = EncryptionHelper.AES_DecryptText("PrivateKey", "cGBffELTG_R97rS_BYhNww") /// Result : /// 123456789 /// </summary> /// <param name="strEncode"></param> /// <param name="txtValue"></param> /// <returns></returns> public static string AES_DecryptText(string strEncode, string txtValue) { try { // Get the bytes of the string // Base64UrL Decrypt for web byte[] bytesToBeDecrypted = Base64UrlHelper.Decode(txtValue); //byte[] bytesToBeDecrypted = Convert.FromBase64String(txtValue); byte[] EncodeBytes = Encoding.UTF8.GetBytes(strEncode); EncodeBytes = SHA256.Create().ComputeHash(EncodeBytes); byte[] bytesDecrypted = AES_Decrypt(bytesToBeDecrypted, EncodeBytes); string result = Encoding.UTF8.GetString(bytesDecrypted); return(result); } catch (Exception ex) { return("Error :" + ex.Message); } }