Search code examples
c#.netaescryptoserviceprovider

DecryptBytesFromBytes [AES] C#


I use this function to decrypt a executable file :

public static string DecryptStringFromBytes_Aes(byte[] cipherText, byte[] Key, byte[] IV) ... 

But, this function return a string output I can see the ASCII output of my corresponding decrypted file. But, I need to get a byte[] output.

I have try many thing but I'm stuck: I need a DecryptBytesToBytes function


Solution

  • As a variant:

        static byte[] DecryptBytesToBytes (byte[] cipherText, byte[] Key, byte[] IV)
        {
            // Check arguments. 
            if (cipherText == null || cipherText.Length <= 0)
                throw new ArgumentNullException("cipherText");
            if (Key == null || Key.Length <= 0)
                throw new ArgumentNullException("Key");
            if (IV == null || IV.Length <= 0)
                throw new ArgumentNullException("Key");
    
            // Declare the string used to hold 
            // the decrypted text.
            byte[] encrypted = null;
    
            // Create an AesCryptoServiceProvider object 
            // with the specified key and IV. 
            using (AesCryptoServiceProvider aesAlg = new AesCryptoServiceProvider())
            {
                aesAlg.Key = Key;
                aesAlg.IV = IV;
    
                // Create a decrytor to perform the stream transform.
                ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
    
                // Create the streams used for decryption. 
                using (MemoryStream msDecrypt = new MemoryStream(cipherText))
                {
                    using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                    {
                        using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                        {
                            string encrypted_text = srDecrypt.ReadToEnd();
                            encrypted = new byte[encrypted_text.Length * sizeof(char)];
                            System.Buffer.BlockCopy(encrypted_text.ToCharArray(), 0, encrypted, 0, encrypted.Length);
                        }
                    }
                }
    
            }
    
            return encrypted;
        }