Encrypt and Decrypt the password in MVC5

Updated : Oct 04, 2019 in Asp.Net MVC

How to encrypt and decrypt the password in Asp.net MVC

This article we will learn about how to encrypt and decrypt the password in Asp.Net MVC with the help of encrypt() and decrypt() functions

How to encrypt and decrypt the password in Asp.net MVC

Create a function as given below name as “encrypt” and pass the encrypted text as a parameter to the given function

the encrypt() function will return an encrypted password

public string encrypt(string clearText)
        {
            string EncryptionKey = "MAKV2SPBNI99212";
            byte[] clearBytes = Encoding.Unicode.GetBytes(clearText);
            using (Aes encryptor = Aes.Create())
            {
                Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
                encryptor.Key = pdb.GetBytes(32);
                encryptor.IV = pdb.GetBytes(16);
                using (MemoryStream ms = new MemoryStream())
                {
                    using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
                    {
                        cs.Write(clearBytes, 0, clearBytes.Length);
                        cs.Close();
                    }
                    clearText = Convert.ToBase64String(ms.ToArray());
                }
            }
            return clearText;
        }
Encrypt and Decrypt the password in MVC5
Encrypt and Decrypt the password in MVC5
Decrypt the password in Asp.Net MVC 5

Create a function as given below name as “Decrypt” and pass the Decrypted text as a parameter to the given function

the decrypt() function will return an decrypted password

 public string Decrypt(string cipherText)
        {
            string EncryptionKey = "MAKV2SPBNI99212";
            byte[] cipherBytes = Convert.FromBase64String(cipherText);
            using (Aes encryptor = Aes.Create())
            {
                Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
                encryptor.Key = pdb.GetBytes(32);
                encryptor.IV = pdb.GetBytes(16);
                using (MemoryStream ms = new MemoryStream())
                {
                    using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write))
                    {
                        cs.Write(cipherBytes, 0, cipherBytes.Length);
                        cs.Close();
                    }
                    cipherText = Encoding.Unicode.GetString(ms.ToArray());
                }
            }
            return cipherText;
        }
Decrypt the password in MVC5
implement Decrypt the password in MVC5
Encrypt and decrypt functions in c #

We have multiple ways to encrypt and decrypt the password based on parameters following the given functions

 public static string Encrypt(string text, string keypassword, string ivpassword)
        {
            RijndaelManaged rijndaelCipher = new RijndaelManaged();

            rijndaelCipher.Mode = CipherMode.CBC;

            rijndaelCipher.Padding = PaddingMode.PKCS7;

            rijndaelCipher.KeySize = 128;

            rijndaelCipher.BlockSize = 128;

            byte[] keyBytes = System.Text.Encoding.UTF8.GetBytes(keypassword);

            byte[] ivBytes = System.Text.Encoding.UTF8.GetBytes(ivpassword);

            rijndaelCipher.Key = keyBytes;

            rijndaelCipher.IV = ivBytes;

            ICryptoTransform transform = rijndaelCipher.CreateEncryptor();

            byte[] plainText = Encoding.UTF8.GetBytes(text);

            byte[] cipherBytes = transform.TransformFinalBlock(plainText, 0, plainText.Length);

            return Convert.ToBase64String(cipherBytes);

        }

following the given static decrypt function

 public static string Decrypt(string text, string keypassword, string ivpassword)
        {
            AesManaged rijndaelCipher = new AesManaged();

            rijndaelCipher.Mode = CipherMode.CBC;

            rijndaelCipher.Padding = PaddingMode.None;            

            rijndaelCipher.BlockSize = 128;

            byte[] encryptedData = Convert.FromBase64String(text);

            byte[] keyBytes = System.Text.Encoding.UTF8.GetBytes(keypassword);

            byte[] ivBytes = System.Text.Encoding.UTF8.GetBytes(ivpassword);

            rijndaelCipher.Key = keyBytes;

            rijndaelCipher.IV = ivBytes;

            ICryptoTransform transform = rijndaelCipher.CreateDecryptor();

            byte[] plainText = transform.TransformFinalBlock(encryptedData, 0, encryptedData.Length);

            return Encoding.UTF8.GetString(plainText);

        }

Also See following links Basic Authentication in MVC

Like
Like Love Haha Wow Sad Angry
76

7 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Joan Ganske
March 11, 2020 1:17 am

I’m very happy to uncover this website. I wanted to thank you for your time for this particularly fantastic read!! I definitely enjoyed every bit of it and I have you saved to fav to see new information on your website.

marine mom
May 1, 2020 2:38 pm

simply cool article after search and search finally got a encrypt and decrypt in different types of methods thanks for sharing

JoeFax
May 1, 2020 3:16 pm

After a long time search, I got good articles thanks for sharing Keep it up

jbutton
May 4, 2020 1:06 am

I read your all articles are good. keep post more articles I subscribed your Blog

7
0
Would love your thoughts, please comment.x
()
x