Question

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

Was it helpful?

Solution 2

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;
    }

OTHER TIPS

That function is proprietary - i.e. it's part of your codebase, it's certainly not part of the BCL. So I suggest you find the source code and write a new version that returns a byte array.

The code you posted is for encrypting strings. The following code will encrypt a file and decrypt it using the file path. It will write out the file to the hard disk.

    static void EncryptFile(string sInputFilename, string sOutputFilename, byte[] key, byte[] iv)
    {
        FileStream fsInput = new FileStream(sInputFilename, FileMode.Open, FileAccess.Read);

        FileStream fsEncrypted = new FileStream(sOutputFilename, FileMode.Create, FileAccess.Write);

        using (AesCryptoServiceProvider encryptor = new AesCryptoServiceProvider())
        {
            encryptor.Key = key;
            encryptor.IV = iv;

            ICryptoTransform transform = encryptor.CreateEncryptor();

            using (CryptoStream cryptostream = new CryptoStream(fsEncrypted, transform, CryptoStreamMode.Write))
            {
                byte[] bytearrayinput = new byte[fsInput.Length];
                fsInput.Read(bytearrayinput, 0, bytearrayinput.Length);
                cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length);
            }

            fsInput.Close();
            fsEncrypted.Close();
        }
    }

    static void DecryptFile(string sInputFilename, string sOutputFilename, byte[] key, byte[] iv)
    {
        using (AesCryptoServiceProvider encryptor = new AesCryptoServiceProvider())
        {
            encryptor.Key = key;
            encryptor.IV = iv;

            using (FileStream fsread = new FileStream(sInputFilename, FileMode.Open, FileAccess.Read))
            {
                using (ICryptoTransform transform = encryptor.CreateDecryptor())
                {
                    using (CryptoStream cryptostream = new CryptoStream(fsread, transform, CryptoStreamMode.Read))
                    {
                        using (BinaryWriter fsDecrypted = new BinaryWriter(File.Open(sOutputFilename, FileMode.Create)))
                        {
                            byte[] buffer = new byte[1024];
                            var read = cryptostream.Read(buffer, 0, buffer.Length);
                            while (read > 0)
                            {
                                fsDecrypted.Write(buffer, 0, read);
                                read = cryptostream.Read(buffer, 0, buffer.Length);
                            }

                            fsDecrypted.Flush();
                            cryptostream.Flush();
                        }
                    }
                }
            }
        }
    }

This works without all the streams:

    public static Byte[] Encrypt(Byte[] input, SymmetricAlgorithm crypto)
    {
        return Transform(crypto.CreateEncryptor(), input, crypto.BlockSize);
    }

    public static Byte[] Decrypt(Byte[] input, SymmetricAlgorithm crypto)
    {
        return Transform(crypto.CreateDecryptor(), input, crypto.BlockSize);
    }

    private static Byte[] Transform(ICryptoTransform cryptoTransform, Byte[] input, Int32 blockSize)
    {
        if (input.Length > blockSize)
        {
            Byte[] ret1 = new Byte[( ( input.Length - 1 ) / blockSize ) * blockSize];

            Int32 inputPos = 0;
            Int32 ret1Length = 0;
            for (inputPos = 0; inputPos < input.Length - blockSize; inputPos += blockSize)
            {
                ret1Length += cryptoTransform.TransformBlock(input, inputPos, blockSize, ret1, ret1Length);
            }

            Byte[] ret2 = cryptoTransform.TransformFinalBlock(input, inputPos, input.Length - inputPos);

            Byte[] ret = new Byte[ret1Length + ret2.Length];
            Array.Copy(ret1, 0, ret, 0, ret1Length);
            Array.Copy(ret2, 0, ret, ret1Length, ret2.Length);
            return ret;
        }
        else
        {
            return cryptoTransform.TransformFinalBlock(input, 0, input.Length); 
        }

    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top