/// <summary> /// Generates an AesParameters that contains a random Key and IV /// </summary> /// <returns>AesParameters</returns> public static AesParamenters GeneratesAesParameters() { AesParamenters aesParameters; using (Aes aes = Aes.Create()) { aesParameters = new AesParamenters(); Array.Copy(aes.Key, aesParameters.Key, aesParameters.Key.Length); Array.Copy(aes.IV, aesParameters.IV, aesParameters.IV.Length); } return(aesParameters); }
/// <summary> /// Encrypt a buffer with the given key. The IV is generated dynamically /// </summary> /// <param name="vBYTE_Data">The plain buffer</param> /// <param name="STRING_Key">The encryption key</param> /// <returns></returns> public static byte[] AES_CBC_Encryption_Rand_IV(byte[] vBYTE_Data, string STRING_Key) { AesParamenters aesParameters = GeneratesAesParameters(); /* Adapts the password lenght */ string paddedKey = (STRING_Key.Length > 32) ? STRING_Key.Substring(0, 32) : STRING_Key.PadLeft(32, '*'); var vBYTE_paddedKey = Encoding.UTF8.GetBytes(paddedKey); var encryptBytes = AES_CBC_Encryption(vBYTE_Data, vBYTE_paddedKey, aesParameters.IV); if (encryptBytes == null) { return(null); } return(encryptBytes); }
/// <summary> /// Encrypt a string with the given key. The IV is generated dynamically /// </summary> /// <param name="STRING_Data">The string to be encrypt</param> /// <param name="STRING_Key">The encryption Key</param> /// <returns></returns> public static string AES_CBC_Encryption_Rand_IV(string STRING_Data, string STRING_Key) { AesParamenters aesParameter = GeneratesAesParameters(); /* Adapts the password lenght */ string paddedKey = (STRING_Key.Length > 32) ? STRING_Key.Substring(0, 32) : STRING_Key.PadLeft(32, '*'); var vBYTE_paddedKey = Encoding.UTF8.GetBytes(paddedKey); byte[] plainBytes = Encoding.UTF8.GetBytes(STRING_Data); var encryptBytes = AES_CBC_Encryption(plainBytes, vBYTE_paddedKey, aesParameter.IV); if (encryptBytes == null) { return(null); } return(Convert.ToBase64String(encryptBytes)); }