Login Register
Code2night
  • Home
  • Blog Archive
  • Learn
    • Tutorials
    • Videos
  • Interview Q&A
  • Languages
    • Angular
    • Angular js
    • Asp.net Core
    • C
    • C#
    • DotNet
    • HTML/CSS
    • Java
    • JavaScript
    • Node.js
    • Python
    • React
    • Security
    • SQL Server
    • TypeScript
  • Post Blog
  • Tools
    • JSON Beautifier
    • HTML Beautifier
    • XML Beautifier
    • CSS Beautifier
    • JS Beautifier
    • PDF Editor
    • Word Counter
    • Base64 Encode/Decode
    • Diff Checker
    • JSON to CSV
    • Password Generator
    • SEO Analyzer
    • Background Remover
  1. Home
  2. Blog
  3. ASP.NET Core
  4. How to Encrypt and Decrypt Password in Asp.Net

How to Encrypt and Decrypt Password in Asp.Net

Date- May 15,2022

Updated Mar 2026

25957

Free Download Pay & Download
Password Encryption RFC Encryption

Hello guys and welcome to Code2Night, we all need to secure our applications against unwanted user attacks, for saving privacy and maintaining application security. So for that purpose, we have to keep our passwords Encrypted in the Database. So, we will see how to Encrypt and Decrypt Passwords in Asp.Net.

Password Encryption

There are two ways of encrypting passwords in Asp.Net. Encrypting a password is simply the form of data that the user cannot understand. So we can achieve that by converting our password to a base 64 string. And then we can decrypt the base 64 string to a normal string when needed.

For converting the password to Base 64 Encrypted Password you can do the following 

 public static string EncryptPasswordBase64(string text)
 {
     var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(text);
     return System.Convert.ToBase64String(plainTextBytes);
 }

This will encrypt your string to base 64 data.

Decrypting Base 64

For decoding or decrypting the base 64 data to a normal string, we can do this

 public static string DecryptPasswordBase64(string base64EncodedData)
 {
     var base64EncodedBytes = System.Convert.FromBase64String(base64EncodedData);
     return System.Text.Encoding.UTF8.GetString(base64EncodedBytes);
 }

RFC Encryption

Base 64 encryption is comparatively less secure encryption as that can be decoded easily. So we have to use a more secure way of encryption passwords which is RFC algorithm cryptography. You can use RFC Encryption like this.

 public static string EncryptPassword(string clearText)
        {
            string EncryptionKey = "MAKVKKKBNI99212";
            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;
        }

Decrypting RFC Encrypted Password

For decrypting the RFC Encrypted password. You can do this. We have to use the same encryption key. That we used while encrypting.

public static string DecryptPassword(string cipherText)
        {
            string EncryptionKey = "MAKVKKKBNI99212";
            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;
        }

So, this is how we can encrypt any password using Base 64 and with RFC encryption. You can check the following outputEncrypt and Decrypt

public static class Encrypt
    {
        public static string EncryptPassword(string clearText)
        {
            string EncryptionKey = "MAKVKKKBNI99212";
            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;
        }

        public static string DecryptPassword(string cipherText)
        {
            string EncryptionKey = "MAKVKKKBNI99212";
            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;
        }
        public static string EncryptPasswordBase64(string text)
        {
            var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(text);
            return System.Convert.ToBase64String(plainTextBytes);
        }
        public static string DecryptPasswordBase64(string base64EncodedData)
        {
            var base64EncodedBytes = System.Convert.FromBase64String(base64EncodedData);
            return System.Text.Encoding.UTF8.GetString(base64EncodedBytes);
        }
    }

On the controller, we will use the class like this

 [HttpPost]
        public ActionResult Index(string Password)
        {
            ViewBag.Encrypt = Encrypt.EncryptPasswordBase64(Password);
            ViewBag.RfcEncrypt=Encrypt.EncryptPassword(Password);
            ViewBag.Password=Password;
            //For Decrypt
            //  ViewBag.Base64Decrypt = Encrypt.DecryptPasswordBase64(Password);
            // ViewBag.RfcDecrypt=Encrypt.DecryptPassword(Password);
           
            return View();
        }

For the sample code, you can download the attached code and use that. Let us know if you face any issues. This is how we can encrypt and decrypt passwords in Asp.Net MVC

S
Shubham Batra
Programming author at Code2Night โ€” sharing tutorials on ASP.NET, C#, and more.
View all posts โ†’

Related Articles

Exception Handling Asp.Net Core
Aug 05, 2020
HTTP Error 500.31 Failed to load ASP NET Core runtime
Aug 23, 2022
How to implement Paypal in Asp.Net Core
Oct 30, 2022
Task Scheduler in Asp.Net core
Jul 20, 2022
Previous in ASP.NET Core
What is asp.net in web development
Next in ASP.NET Core
HttpCookies Issue with Asp.Net Core 3.1

Comments

Contents

๐ŸŽฏ

Interview Prep

Ace your ASP.NET Core interview with curated Q&As for all levels.

View ASP.NET Core Interview Q&As

More in ASP.NET Core

  • Implement Stripe Payment Gateway In ASP.NET Core 16755 views
  • Send Email With HTML Template And PDF Using ASP.Net C# 16526 views
  • How to implement Paypal in Asp.Net Core 8.0 12900 views
  • Import data from Excel in Asp.Net 12755 views
  • HTTP Error 502.5 - ANCM Out Of Process Startup Failure 12734 views
View all ASP.NET Core posts โ†’

Tags

AspNet C# programming AspNet MVC c programming AspNet Core C software development tutorial MVC memory management Paypal coding coding best practices data structures programming tutorial tutorials object oriented programming Slick Slider StripeNet
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1760
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, Haryana, India
info@code2night.com
Quick Links
  • Home
  • Blog Archive
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
  • SEO Analyzer
Free Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • Password Generator
  • QR Code Generator
  • Hash Generator
  • Diff Checker
  • Base64 Encode/Decode
  • Word Counter
  • SEO Analyzer
By Language
  • Angular
  • Angular js
  • Asp.net Core
  • C
  • C#
  • DotNet
  • HTML/CSS
  • Java
  • JavaScript
  • Node.js
  • Python
  • React
  • Security
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Made with for developers  |  Privacy  ยท  Terms
Translate Page
We use cookies to improve your experience and analyze site traffic. By clicking Accept, you consent to our use of cookies. Privacy Policy
Accessibility
Text size
High contrast
Grayscale
Dyslexia font
Highlight links
Pause animations
Large cursor