Using Rijndael encryption for large files

后端 未结 3 2102
忘掉有多难
忘掉有多难 2021-02-09 06:47

I\'m in a situation where I need to encrypt / decrypt a file of n length securely, ideally using Rijndael, but definitely at 256bit encryption.

I\'ve played around with

相关标签:
3条回答
  • 2021-02-09 07:02

    There is a CryptoStream class to encrypt/decrypt Streams

    0 讨论(0)
  • 2021-02-09 07:07

    The Stream.Read method, returns the number of bytes actually being read from the stream.

    You should use this return value as the last parameter in the Write method on the next line.

    My code would look like this:

    byte[] chunkData = new byte[chunkSize];   
    var bytesRead = 0;
    while ((bytesRead = fsInput.Read(chunkData, 0, chunkSize)) > 0)
    {
        cryptoStream.Write(chunkData, 0, bytesRead);
    }
    
    0 讨论(0)
  • 2021-02-09 07:20

    The problem is that I was using:

    passwordDB.GetBytes(256 / 8)
    

    within the constructor for the RijndaelManaged object in both the Encryption and Decryption methods, and I wasn't re-initialising the passwordDB object before attempting to decrypt.

    The resolution was to simply including the construction of the passwordDB object within the first lines of both the Enc and Dec methods, as follows:

            private static void Enc(string decryptedFileName, string encryptedFileName)
            {
                PasswordDeriveBytes passwordDB = new PasswordDeriveBytes("ThisIsMyPassword", Encoding.ASCII.GetBytes("thisIsMysalt!"), "MD5", 2);
                byte[] passwordBytes = passwordDB.GetBytes(128 / 8);
    
                using (FileStream fsOutput = File.OpenWrite(encryptedFileName))
                {
                    using(FileStream fsInput = File.OpenRead(decryptedFileName))
                    {
                        byte[] IVBytes = Encoding.ASCII.GetBytes("1234567890123456");
    
                        fsOutput.Write(BitConverter.GetBytes(fsInput.Length), 0, 8);
                        fsOutput.Write(IVBytes, 0, 16);
    
                        RijndaelManaged symmetricKey = new RijndaelManaged() { Mode = CipherMode.CBC,Padding=PaddingMode.ANSIX923};
                        ICryptoTransform encryptor = symmetricKey.CreateEncryptor(passwordBytes, IVBytes);                   
    
                        using (CryptoStream cryptoStream = new CryptoStream(fsOutput, encryptor, CryptoStreamMode.Write))
                        {
                            for (long i = 0; i < fsInput.Length; i += chunkSize)
                            {
                                byte[] chunkData = new byte[chunkSize];
                                int bytesRead = 0;
                                while ((bytesRead = fsInput.Read(chunkData, 0, chunkSize)) > 0)
                                {
                                    if (bytesRead != 16)
                                    {
                                        for (int x = bytesRead - 1; x < chunkSize; x++)
                                        {
                                            chunkData[x] = 0;
                                        }
                                    }
                                    cryptoStream.Write(chunkData, 0, chunkSize);
                                }
                            }
                            cryptoStream.FlushFinalBlock();
                        }
                    }
                }            
            }
    
            private static void Dec(string encryptedFileName, string decryptedFileName)
            {
                PasswordDeriveBytes passwordDB = new PasswordDeriveBytes("ThisIsMyPassword", Encoding.ASCII.GetBytes("thisIsMysalt!"), "MD5", 2);
                byte[] passwordBytes = passwordDB.GetBytes(128 / 8);
    
                using (FileStream fsInput = File.OpenRead(encryptedFileName))
                {
                    using (FileStream fsOutput = File.OpenWrite(decryptedFileName))
                    {
                        byte[] buffer = new byte[8];
                        fsInput.Read(buffer, 0, 8);
    
                        long fileLength = BitConverter.ToInt64(buffer, 0);
    
                        byte[] IVBytes = new byte[16];
                        fsInput.Read(IVBytes, 0, 16);
    
    
                        RijndaelManaged symmetricKey = new RijndaelManaged() { Mode = CipherMode.CBC,Padding=PaddingMode.ANSIX923};
                        ICryptoTransform decryptor = symmetricKey.CreateDecryptor(passwordBytes, IVBytes);
    
                        using (CryptoStream cryptoStream = new CryptoStream(fsOutput, decryptor, CryptoStreamMode.Write))
                        {
                            for (long i = 0; i < fsInput.Length; i += chunkSize)
                            {
                                byte[] chunkData = new byte[chunkSize];
                                int bytesRead = 0;
                                while ((bytesRead = fsInput.Read(chunkData, 0, chunkSize)) > 0)
                                {
                                    cryptoStream.Write(chunkData, 0, bytesRead);
                                }
                            }
                        }
                    }
                }
            }
    

    Knew it had to be a schoolboy error :P

    0 讨论(0)
提交回复
热议问题