如何在C#中使用AES算法加密包含大文件的文件夹 [英] How to encrypt a folder with large files using AES algorithm in C#

查看:194
本文介绍了如何在C#中使用AES算法加密包含大文件的文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨代码项目伙伴,



加密和解密问题严重。我有一个文件夹,可以有不同的大小,但可以有100 GB左右。



我的项目需要:

需要的是当我注销系统时,我创建的Windows服务应该加密文件夹(这是100 GB数据的文件夹)。当我再次登录时,它应解密它并使文件夹可用。



所以这就是我的问题:

我现在有一种方法可以循环格式加密和解密文件夹中的文件。因此,想象一下,当我们注销或登录时,文件夹中是否有100 GB或更高版本。系统可能会挂起如此大量的数据。



< b>我想要你们这些人:

我不是要求你们提供完整的代码。我只需要一个帮助。



实际上我想要一些代码示例或者某些东西可以帮助我摆脱这个问题。此外,我正在搜索整个文件夹加密而不是内部的单个文件。



我正在寻找使这个软件类似于BITLOCKER。我的服务应该轻松处理大文件。



我尝试过:



我试过以下代码:



Hi Code project mates,

I have a serious problem with encryption and decryption. What I have is a folder which can be in varied sizes but can have an amount 100 GB or so.

My project need:
The need is when I log off the system the windows service I have created should encrypt the folder(which is the folder with 100 GB data). And when I log in again it should decrypt it and make the folder usable.

So here comes my problem:
I am right now have a method which encrypts and decrypts files inside a folder in a loop format. So just imagine if I have a 100 GB or above in a folder while we log off or log in. The system may hang with such large amount of data.

What I want from you guys:
I am not asking for complete code from you guys. I need just an help.

Actually I want some code samples or something which helps me get rid of the problem. Also I am searching for the encrypting the folder on the whole instead of individual files inside.

I am looking for making this software similar to BITLOCKER. My service should handle the large files with ease.

What I have tried:

I have tried the below code:

protected void enCrypt(string filePath, string passWord)
        {
            //File.SetAttributes(filePath, FileAttributes.Normal);

            byte[] filestobeEncrypted = File.ReadAllBytes(filePath);
            byte[] passwordBytes = Encoding.UTF8.GetBytes(passWord);
            passwordBytes = SHA256.Create().ComputeHash(passwordBytes);

            byte[] bytesEncrypted = AES_Encrypt(filestobeEncrypted, passwordBytes);
            File.WriteAllBytes(filePath, bytesEncrypted);
        }

        protected void deCrypt(string filePath, string passWord)
        {
            //File.SetAttributes(filePath, FileAttributes.Normal);

            byte[] filestobeDecrypted = File.ReadAllBytes(filePath);
            byte[] passwordBytes = Encoding.UTF8.GetBytes(passWord);
            passwordBytes = SHA256.Create().ComputeHash(passwordBytes);

            byte[] bytesDecrypted = AES_Decrypt(filestobeDecrypted, passwordBytes);
            File.WriteAllBytes(filePath, bytesDecrypted);
        }

        public static byte[] AES_Encrypt(byte[] bytesToBeEncrypted, byte[] passwordBytes)
        {
            byte[] encryptedBytes = null;

            // Set your salt here, change it to meet your flavor:
            // The salt bytes must be at least 8 bytes.
            byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };

            using (MemoryStream ms = new MemoryStream())
            {
                using (RijndaelManaged AES = new RijndaelManaged())
                {
                    AES.KeySize = 256;
                    AES.BlockSize = 128;

                    var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000);
                    AES.Key = key.GetBytes(AES.KeySize / 8);
                    AES.IV = key.GetBytes(AES.BlockSize / 8);

                    AES.Mode = CipherMode.CBC;

                    using (var cs = new CryptoStream(ms, AES.CreateEncryptor(), CryptoStreamMode.Write))
                    {
                        cs.Write(bytesToBeEncrypted, 0, bytesToBeEncrypted.Length);
                        cs.Close();
                    }
                    encryptedBytes = ms.ToArray();
                }
            }

            return encryptedBytes;
        }

        public static byte[] AES_Decrypt(byte[] bytesToBeDecrypted, byte[] passwordBytes)
        {
            byte[] decryptedBytes = null;

            // Set your salt here, change it to meet your flavor:
            // The salt bytes must be at least 8 bytes.
            byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };

            using (MemoryStream ms = new MemoryStream())
            {
                using (RijndaelManaged AES = new RijndaelManaged())
                {
                    AES.KeySize = 256;
                    AES.BlockSize = 128;

                    var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000);
                    AES.Key = key.GetBytes(AES.KeySize / 8);
                    AES.IV = key.GetBytes(AES.BlockSize / 8);

                    AES.Mode = CipherMode.CBC;

                    using (var cs = new CryptoStream(ms, AES.CreateDecryptor(), CryptoStreamMode.Write))
                    {
                        cs.Write(bytesToBeDecrypted, 0, bytesToBeDecrypted.Length);
                        cs.Close();
                    }
                    decryptedBytes = ms.ToArray();
                }
            }

            return decryptedBytes;
        }

推荐答案

您可以使用多线程来处理加密(一次启动几个线程,以便不要绑定CPU)。



你不能加密文件夹。您必须加密文件夹中的文件。这是一篇关于AES加密的CP文章:



[ ^ ]



您可以创建加密包含所有文件的ZIP文件,但这需要很长时间。



我认为更好的解决方案是将服务器添加到具有bitlocker的网络上它,将文件夹复制到该服务器,然后添加适当的用户访问权限。不要问我细节 - 我只是在墙上扔东西看看有什么东西。
You can use multi-threading to handle the encryption (fire off a few threads at a time so as not to bind the CPU up).

You cannot encrypt a folder. You have to encrypt the files inside the folder. Here's a CP article that talks about AES encryption:

FIPS Encryption Algorithms and Implementation of AES in C# and SQL Server 2008[^]

You could create an encrypted ZIP file containing all of the files, but that would take a LONG time.

I think a better solution would be to add a server to the network that has bitlocker on it, copy the folder to that server, and then add appropriate user permissions for access. Don't ask me for details - I'm just throwin' stuff at the wall to see what sticks.


这篇关于如何在C#中使用AES算法加密包含大文件的文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆