如何只加密/解密文件的前几个字节(其余的应该是未加密的)?

时间:2015-01-13 15:26:10

标签: c# encryption cryptography byte

所以,我已经实现了C#加密/解密算法,在加密/解密任何文件时都能正常工作。 我现在的问题是,我怎样才能加密(然后解密)文件的前几个(兆字节)字节。

例如:我有一个2GB的文件,我只想加密3MB的这个文件。

我尝试了一些东西,但没有任何工作,因为我想。我试图计算readed字节,如果readed字节超过限制(3MB),则停止加密并继续将正常(未加密的数据)写入文件。但解密后,"填充"异常出现,等等。另一个例子:我(用这种方法)"成功"加密了20kb的50kb .txt文件,但解密后,.txt文件中的最后一行还有一些"奇怪的" charachters - 所以这不起作用(如果我想要exaple加密图像 - 仍然在解密后#34;损坏")。

对于加密,我使用此功能:

public static bool Encrypt(string inputFilePath, string outputfilePath, string EncryptionKey)
{
try
{
    using (FileStream fsInput = new FileStream(inputFilePath, FileMode.Open))
    {

        using (FileStream fsOutput = new FileStream(outputfilePath, FileMode.Create))
        {
            fsInput.Position = 0;                       

            using (Aes encryptor = Aes.Create())
            {
                Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
                encryptor.Key = pdb.GetBytes(32);
                encryptor.IV = pdb.GetBytes(16);
                using (CryptoStream cs = new CryptoStream(fsOutput, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
                {

                    int bufferLen = 4096;
                    byte[] buffer = new byte[bufferLen];
                    int bytesRead;
                    int byteWriteCounter = 0;                           

                    do
                    {
                        bytesRead = fsInput.Read(buffer, 0, bufferLen);

                        //my method
                        if(byteWriteCounter <= 20000){ //if readed bytes <= 20kb
                            cs.Write(buffer, 0, bytesRead); // then encrypt 
                        }else{ // if bytes are over 20kb
                            fsOutput.Write(buffer, 0, bytesRead); //write normal (unecrypted)
                        }                           

                        byteWriteCounter += bytesRead;


                    } while (bytesRead != 0);

                    return true;
                }
            }
        }

    }
}
catch (SystemException se)
{
    Console.WriteLine(se);
    return false;
}
}

解密(我使用)是类似的功能/方法:

public static bool Decrypt(string inputFilePath, string outputfilePath, string EncryptionKey)
{
try
{
    using (FileStream fsInput = new FileStream(inputFilePath, FileMode.Open))
    {


        using (Aes encryptor = Aes.Create())
        {
            Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
            encryptor.Key = pdb.GetBytes(32);
            encryptor.IV = pdb.GetBytes(16);
            using (FileStream fsOutput = new FileStream(outputfilePath, FileMode.Create))
            {
                using (CryptoStream cs = new CryptoStream(fsOutput, encryptor.CreateDecryptor(), CryptoStreamMode.Write))
                {

                    int bufferLen = 4096;
                    byte[] buffer = new byte[bufferLen];
                    int bytesRead;
                    int byteWriteCounter = 0;

                    do
                    {

                        bytesRead = fsInput.Read(buffer, 0, bufferLen);

                        //my method
                        if(byteWriteCounter <= 20000){ //if readed bytes <= 20kb
                            cs.Write(buffer, 0, bytesRead); // then decrypt 
                        }else{ // if bytes are over 20kb
                            fsOutput.Write(buffer, 0, bytesRead); //write normal data
                        }

                        byteWriteCounter += bytesRead;

                    } while (bytesRead != 0);

                }
            }
        }                   
    }
    return true;
}
catch (SystemException s)
{
    Console.WriteLine(s);
    return false;
}
}

1 个答案:

答案 0 :(得分:2)

要实现此目的,您需要确切知道加密流中的字节数。在将加密流写入新的部分加密文件之前,您可以轻松地写入4个字节的整数。然后,当您解密文件时,您首先读取4个字节,并且您确切知道要使用CryptoStream读取多少字节。为此,您基本上需要按如下方式读取块中的文件:

  1. 读取前4个字节为整数
  2. 读取加密流与整数一样多的字节
  3. 解密并且不写新文件
  4. 将泵字节从部分加密文件更改为新的完全解密文件