AES Encryption Decryption .NET Core 2

So if you saw my earlier post about this the code there didn't work for me.

but who know me knows i don't give up that easily, there might be a NuGet here or there that do this but i needed to know how this is done!

so after 3 hours of digging code on the internet and trying the code for a dozen of times, below is the code which did work for me.

i know that the IV should be dynamic in order to further protect this more and this is something i will work on.


public static class Crypto
    {
        public static string EncryptString(string text, string keyString)
        {
            var key = Encoding.UTF8.GetBytes(keyString);
            var textToChiper = Encoding.UTF8.GetBytes(text);

            using (var aesAlg = Aes.Create())
            {
                aesAlg.Padding = PaddingMode.PKCS7;
                aesAlg.Mode = CipherMode.CBC;
                aesAlg.KeySize = (key.Length * 8);
                aesAlg.IV = new byte[16];
                aesAlg.Key = key;
                using (var encryptor = aesAlg.CreateEncryptor())
                {
                    using (var msEncrypt = new MemoryStream())
                    {
                        using (var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                        {
                            using (StreamWriter csEncryptWriter = new StreamWriter(csEncrypt))
                            {
                                csEncryptWriter.Write(text);
                            }
                        }
                        var result = Convert.ToBase64String(msEncrypt.ToArray());
                        return result;
                    }
                }
            }
        }

        public static string DecryptString(string cipherText, string keyString)
        {
            var fullCipher = Convert.FromBase64String(cipherText);
            var key = Encoding.UTF8.GetBytes(keyString);
            string decrypted;

            using (var aesAlg = Aes.Create())
            {
                aesAlg.Padding = PaddingMode.PKCS7;
                aesAlg.Mode = CipherMode.CBC;
                aesAlg.KeySize = (key.Length * 8);
                aesAlg.IV = new byte[16];
                aesAlg.Key = key;
                using (var decryptor = aesAlg.CreateDecryptor())
                {
                    using (var msDecrypt = new MemoryStream(fullCipher))
                    {
                        using (var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                        {

                            using (StreamReader csDecryptReader = new StreamReader(csDecrypt))
                            {
                                decrypted = csDecryptReader.ReadToEnd();
                            }
                        }
                        
                        return decrypted;
                    }
                }
            }
        }
    }

Popular posts from this blog

how to copy NuGet packages from one project to another

How to load a DLL and runtime, invoke a method by name, get result object and use it as a parameter on another unkown DLL .NET Core

Blazor how to generate that SHA256 check