private static byte[] Encrypt(byte[] key, byte[] plaintext) { var encryptor = new AesManaged() { Mode = CipherMode.ECB, Key = key, Padding = PaddingMode.None, }.CreateEncryptor(); return(encryptor.TransformFinalBlock(plaintext, 0, plaintext.Length)); }
public static string AesDecryptor(string str) { byte[] array = Convert.FromBase64String(str); ICryptoTransform cryptoTransform = new AesManaged { Key = Encoding.UTF8.GetBytes(GetMD5(string_0)), IV = Encoding.UTF8.GetBytes(GetMD5(string_0).Substring(8, 16)), Mode = CipherMode.CBC, Padding = PaddingMode.PKCS7 }.CreateDecryptor(); return(Encoding.UTF8.GetString(cryptoTransform.TransformFinalBlock(array, 0, array.Length))); }
/// <summary> /// Decrypts a given byte[] using AES128 in CounterMode /// </summary> /// <param name="message">the encrypted message</param> /// <param name="key">the key (32 bytes recommended)</param> /// <param name="iv">the iv (16 bytes)</param> /// <returns>returns the decrypted result as byte[]</returns> public static byte[] Decrypt(byte[] message, byte[] key, byte[] iv) { if (iv.Length != 16) { throw new ArgumentException(nameof(iv)); } if (key.Length % 16 != 0) { throw new ArgumentException(nameof(key)); } ICryptoTransform transform = new AesManaged() { Mode = CipherMode.CBC }.CreateDecryptor(key, iv); return(transform.TransformFinalBlock(message, 0, message.Length)); }
public static string DecryptAes(string encrypted, byte[] aesKey) { var decoded = Convert.FromBase64String(encrypted); var iv = new byte[IVLength]; Buffer.BlockCopy(decoded, 0, iv, 0, iv.Length); var encryptedData = new byte[decoded.Length - iv.Length]; Buffer.BlockCopy(decoded, iv.Length, encryptedData, 0, encryptedData.Length); var decryptor = new AesManaged { KeySize = 256, BlockSize = 128 }.CreateDecryptor(aesKey, iv); var decryptedBytes = decryptor.TransformFinalBlock(encryptedData, 0, encryptedData.Length); return(new UTF8Encoding().GetString(decryptedBytes, 0, decryptedBytes.Length)); }