AES加密/解密字节[]

时间:2015-01-13 00:27:30

标签: encryption aes encryption-symmetric aescryptoserviceprovider

我正在尝试使用以下方法加密byte [],但是当我解密它时,我的byte []比我开始时更大,我认为它与填充有关,但我不知道如何解决它。 / p>

这个方法还没有完成(我知道附加键+ iv就像我的例子一样糟糕但是为了测试目的而让它在我继续前工作)。

因此,当我尝试之后打开文件(使用MS Word文件测试)时,我收到一条消息,说文件已损坏,我想修复它。

加密方法

public byte[] Encrypt(byte[] dataToEncrypt) {
        // Check arguments. 
        if (dataToEncrypt == null || dataToEncrypt.Length <= 0) {
            throw new ArgumentNullException("dataToEncrypt");
        }

        byte[] encryptedData;
        byte[] key;
        byte[] iv;

        // Create an Aes object  
        using (Aes aesAlg = Aes.Create()) {
            key = aesAlg.Key;
            iv = aesAlg.IV;

            // Create a encrytor to perform the stream transform.
            ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);

            // Create the streams used for encryption. 
            using (MemoryStream memoryStream = new MemoryStream()) {
                using (CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write)) {
                    cryptoStream.Write(dataToEncrypt, 0, dataToEncrypt.Length);
                    cryptoStream.FlushFinalBlock();

                    encryptedData = memoryStream.ToArray();

                }
            }


        }

        byte[] result = new byte[encryptedData.Length + KEY_SIZE + IV_SIZE];

        Buffer.BlockCopy(key, 0, result, 0, KEY_SIZE);
        Buffer.BlockCopy(iv, 0, result, KEY_SIZE, IV_SIZE);
        Buffer.BlockCopy(encryptedData, 0, result, KEY_SIZE + IV_SIZE, encryptedData.Length);

        return result;
    }

解密方法

public byte[] Decrypt(byte[] encryptedData) {
        // Check arguments. 
        if (encryptedData == null || encryptedData.Length <= 0) {
            throw new ArgumentNullException("encryptedData");
        }

        byte[] storedKey = new byte[KEY_SIZE];
        byte[] storedIV = new byte[IV_SIZE];
        byte[] dataToDecrypt = new byte[encryptedData.Length - (KEY_SIZE + IV_SIZE)];

        Buffer.BlockCopy(encryptedData, 0, storedKey, 0, KEY_SIZE);
        Buffer.BlockCopy(encryptedData, KEY_SIZE, storedIV, 0, IV_SIZE);
        Buffer.BlockCopy(encryptedData, KEY_SIZE + IV_SIZE, dataToDecrypt, 0, encryptedData.Length - (KEY_SIZE + IV_SIZE));

        byte[] decryptedData = null;

        // Create an AesCryptoServiceProvider object 
        // with the specified key and IV. 
        using (Aes aesAlg = Aes.Create()) {
            aesAlg.Key = storedKey;
            aesAlg.IV = storedIV;

            // Create a decrytor to perform the stream transform.
            ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);

            // Create the streams used for decryption. 
            using (MemoryStream memoryStream = new MemoryStream(dataToDecrypt)) {
                using (CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read)) {
                    cryptoStream.Read(dataToDecrypt, 0, dataToDecrypt.Length);

                    decryptedData = memoryStream.ToArray();
                }
            }

        }

        return decryptedData;
    }

1 个答案:

答案 0 :(得分:0)

您假设整个缓冲区也是纯文本数据。您应该只返回包含明文数据的缓冲区的那一部分(使用Read的响应来查看返回的字节数)。加密数据通常较大,因为填充

因为单个读取方法在流处理方面不是好的做法。您需要阅读直到到达流的末尾。否则你可能会从拥有太多数据到拥有太多数据。

相关问题