DecryptBytesFromBytes [AES] C#

本文关键字:AES DecryptBytesFromBytes | 更新日期: 2023-09-27 17:49:02

我使用这个函数来解密一个可执行文件:

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

但是,这个函数返回一个字符串输出,我可以看到对应的解密文件的ASCII输出。但是,我需要得到一个byte[]输出。

我尝试了很多事情,但我卡住了:我需要一个DecryptBytesToBytes函数

DecryptBytesFromBytes [AES] C#

该函数是专有的-也就是说,它是你的代码库的一部分,它当然不是BCL的一部分。所以我建议您找到源代码并编写一个返回字节数组的新版本。

作为一个变体:

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

您发布的代码是用于加密字符串的。下面的代码将使用文件路径对文件进行加密和解密。它将把文件写入硬盘。

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

不需要所有流也可以工作:

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