Search code examples
encryption.net-coreencryption-symmetric

How do I encrypt/decrypt a file in .NetCore?


I would like to encrypt/decrypt a file to/from disk using .NetCore libraries.

Examples I have seen are account based so that only the account used to encrypt the file can decrypt it. https://learn.microsoft.com/en-us/dotnet/api/system.io.file.encrypt?view=net-5.0

I need to be able to password encrypt my files. Is there a .Net library available for that (either part of standard .NetCore libraries or 3rd party)?


Solution

  • There are lots of resources for encryption/decryption on the internet and with a simple search, you can find them.

    Encrypt:

    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);
                }
            }
        }
    }
    

    Decrypt:

    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;
            }
        }
    }
    

    Symmetric and Asymmetric Encryption in .NET Core

    Cryptography with Practical Examples in .Net Core

    Encryption And Decryption Using A Symmetric Key In C#