Expected that
Convert.FromBase64String(string); will receive the line generated by Convert.ToBase64String(byte[]); passing in an arbitrary line will not work.
The simplest solution is to replace BinaryWriter and BinaryReader with StreamWriter and a StreamReader and not do any conversion at all.
public byte[] EncryptStringToBytes_Aes(string plainText, string Key) { // Check arguments. if (plainText == null || plainText.Length <= 0) throw new ArgumentNullException("plainText"); if (Key == null || Key.Length <= 0) throw new ArgumentNullException("Key"); //Create an Aes object //with the specified key and IV. using (Aes aesAlg = Aes.Create()) { aesAlg.GenerateIV(); byte[] IV = aesAlg.IV; //The Salt will be the first 8 bytes of the IV. byte[] theSalt = new byte[8]; Array.Copy(IV,theSalt,8); //A key for AES is generated by expanding the password using the following method. Rfc2898DeriveBytes keyGen = new Rfc2898DeriveBytes(Key,theSalt); byte[] aesKey = keyGen.GetBytes(16); aesAlg.Key = aesKey; // Create a decrytor to perform the stream transform. ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, IV); // Create the streams used for encryption. using (MemoryStream msEncrypt = new MemoryStream()) { //You can write the IV here and not need to do it later. msEncrypt.Write(IV, 0, IV.Length); using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)) { using (StreamWriter swEncrypt = new StreamWriter (csEncrypt)) { //Write all data to the stream. swEncrypt.Write(plainText); } } //Move this outside of the using statement for CryptoStream so it is flushed and dipsoed. return msEncrypt.ToArray(); } } }
In addition, your decryption function is actually trying to encrypt the text a second time, you need to pass an array of bytes to the msDecrypt constructor and put it in decryption mode.
public string DecryptStringFromBytes_Aes(byte[] cipherText, string Key) { // Check arguments. if (cipherText == null || cipherText.Length <= 0) throw new ArgumentNullException("cipherText"); if (Key == null || Key.Length <= 0) throw new ArgumentNullException("Key"); // Create an Aes object // with the specified key and IV. // Create the streams used for decryption. using (Aes aesAlg = Aes.Create()) { aesAlg.Mode = CipherMode.CBC; aesAlg.Padding = PaddingMode.PKCS7; //Grab IV from ciphertext byte[] IV = new byte[16]; Array.Copy(cipherText,0,IV,0,16); //Use the IV for the Salt byte[] theSalt = new byte[8]; Array.Copy(IV,theSalt,8); Rfc2898DeriveBytes keyGen = new Rfc2898DeriveBytes(Key,theSalt); byte[] aesKey = keyGen.GetBytes(16); aesAlg.Key = aesKey; // Create a decrytor to perform the stream transform. ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, IV); //You can chain using statements like this to make the code easier to read. using (MemoryStream msDecrypt = new MemoryStream(cipherText)) using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) //Notice this is Read mode not Write mode. using (StreamReader srDecrypt = new StreamReader(csDecrypt)) { //Decrypt the ciphertext return srDecrypt.ReadToEnd(); } } }
There may be other errors in your code, but at least it will lead you to the right path.
source share