以1mb块加密文件是否可行?

时间:2014-08-25 15:15:40

标签: c# encryption aes block filestream

我在16位的块中加密文件,但我注意到它花了多长时间,所以我尝试了1mb块,但唯一困扰我的是,例如,如果加密/解密后文件是600.5 mb,它就变成了601mb。这对文件数据有害吗?它会破坏文件或其他什么?这是通过块加密来完成块的循环:

ICryptoTransform encryptor = symmetricKey.CreateEncryptor(keyBytes, symmetricKey.IV);
CryptoStream cryptoStream = new CryptoStream(fsOutput, encryptor, CryptoStreamMode.Write);
                for (long i = 0; i < fsInput.Length; i += chunkSize)
                {
                    if (sender == "main")
                    {
                        MainWindow.valuee += 1;
                    }
                    else
                    {
                        PasswordEnter.valuee += 1;
                    }
                    byte[] chunkData = new byte[chunkSize];
                    int bytesRead = 0;
                    while ((bytesRead = fsInput.Read(chunkData, 0, chunkSize)) > 0)
                    {
                        if (bytesRead != chunkSize)
                        {
                            for (int x = bytesRead - 1; x < chunkSize; x++)
                            {
                                chunkData[x] = 0;
                            }
                        }
                        cryptoStream.Write(chunkData, 0, chunkSize);
                    }
                }
                cryptoStream.FlushFinalBlock();

1 个答案:

答案 0 :(得分:2)

在最后一个循环中,您使用chunkSize中的cryptoStream.Write

尝试使用bytesRead

cryptoStream.Write(chunkData, 0, bytesRead);