0

I want to encrypt files after upload and decrypt before download in asp.net core 3.1, I can run this issue by this tutorial: link, but if the file is large, this process is very Time-consuming and Reduces efficiency.

is any way to encrypt only part of the file?

mahdi rahimzade
  • 111
  • 1
  • 8

2 Answers2

0

Addition:- you may use the link for binary serialization from here :-Encrypt .NET binary serialization stream

Of course, you can do but you need to aware of some Encryption/Decryption classes or algorithms available in Dotnet Or you can take help from this:-

    class TestAESENC{
    public static string EncryptString(string text, string keyString)
        {
            var key = Encoding.UTF8.GetBytes(keyString);
            using (var aesAlg = Aes.Create())
            {
                using (var encryptor = aesAlg.CreateEncryptor(key, aesAlg.IV))
                {
                    using (var msEncrypt = new MemoryStream())
                    {
                        using (var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                        using (var swEncrypt = new StreamWriter(csEncrypt))
                        {
                            swEncrypt.Write(text);
                        }
                        var iv = aesAlg.IV;
                        var decryptedContent = msEncrypt.ToArray();
                        var result = new byte[iv.Length + decryptedContent.Length];
                        Buffer.BlockCopy(iv, 0, result, 0, iv.Length);
                        Buffer.BlockCopy(decryptedContent, 0, result, iv.Length, decryptedContent.Length);
                        return Convert.ToBase64String(result);
                    }
                }
            }
        }

        public static string DecryptString(string cipherText, string keyString)
        {
            var fullCipher = Convert.FromBase64String(cipherText);
            var iv = new byte[16];
            var cipher = new byte[16];
            Buffer.BlockCopy(fullCipher, 0, iv, 0, iv.Length);
            Buffer.BlockCopy(fullCipher, iv.Length, cipher, 0, iv.Length);
            var key = Encoding.UTF8.GetBytes(keyString);
            using (var aesAlg = Aes.Create())
            {
                using (var decryptor = aesAlg.CreateDecryptor(key, iv))
                {
                    string result;
                    using (var msDecrypt = new MemoryStream(cipher))
                    {
                        using (var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                        {
                            using (var srDecrypt = new StreamReader(csDecrypt))
                            {
                                result = srDecrypt.ReadToEnd();
                            }
                        }
                    }

                    return result;
                }
            }
        }
}
Dheeraj Singh
  • 109
  • 1
  • 16
  • Thanks for your reply I did not understand how this code encrypts part of the file. Can you explain to me? – mahdi rahimzade Jul 27 '20 at 13:15
  • these are simple methods that take a string and encrypt/decrypt the same, it is up to you how you may find a partition of the file to be encrypted, btw is it a text file or binary? – Dheeraj Singh Jul 27 '20 at 13:46
  • Incidentally, my problem is how to separate a part of a file and after encryption, re-create an integrated file. My goal is that if the server is hacked, it will not be possible for the hacker to access the contents of the files and the server resources will be used properly. – mahdi rahimzade Jul 27 '20 at 15:33
  • what does your file contain text or binary data? – Dheeraj Singh Jul 27 '20 at 18:02
  • any file, such as pdf,mp4,docx,jpg and other – mahdi rahimzade Jul 27 '20 at 18:44
0

The code below you can use to split files into parts at the path being provided according to the chunk size:-

public static void SplitFile(string inputFile, int chunkSize, string path)
{
    const int BUFFER_SIZE = 20 * 1024;
    byte[] buffer = new byte[BUFFER_SIZE];

    using (Stream input = File.OpenRead(inputFile))
    {
        int index = 0;
        while (input.Position < input.Length)
        {
            using (Stream output = File.Create(path + "\\" + index))
            {
                int remaining = chunkSize, bytesRead;
                while (remaining > 0 && (bytesRead = input.Read(buffer, 0,
                        Math.Min(remaining, BUFFER_SIZE))) > 0)
                {
                    output.Write(buffer, 0, bytesRead);
                    remaining -= bytesRead;
                }
            }
            index++;
            Thread.Sleep(500); // experimental; perhaps try it
        }
    }
}

then you may apply above code to encrypt decrypt or merge the parts, I am sure this works

Dheeraj Singh
  • 109
  • 1
  • 16