-1

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

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

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

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

4

4 回答 4

1

该功能是专有的 - 即它是您的代码库的一部分,它肯定不是 BCL 的一部分。所以我建议你找到源代码并编写一个返回字节数组的新版本。

于 2012-12-21T09:03:02.023 回答
0

这在没有所有流的情况下有效:

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

    }
于 2014-05-20T10:28:53.300 回答
0

作为变体:

    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;
    }
于 2012-12-21T09:37:37.047 回答
0

您发布的代码用于加密字符串。以下代码将加密文件并使用文件路径对其进行解密。它会将文件写入硬盘。

    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();
                        }
                    }
                }
            }
        }
    }
于 2012-12-21T09:37:56.557 回答