How to encrypt and decrypt string in asp.net with c# - CodingPot | Programming Blog - ASP.NET, C#, VB.NET, AngularJs, SQL Server, AJAX, JQuery Tutorials.

Tuesday 17 September 2019

How to encrypt and decrypt string in asp.net with c#

In this blog we will learn “How to encrypt and decrypt string in Asp.net with C#”. I will explain how to encrypt string and then decrypt that encrypted string. The String will be first encrypting using AES Symmetric key algorithm and then Decryption will be done by encrypted string using the same key that was used for encryption. Encrypt and Decrypt are very important data when code with C#. For the security purpose, we are storing some valuable thing in Encrypted format. A Microsoft has a simple method to convert string in Encrypt and Decrypt at any time.


Encryption is the process of translating plain text data into something that appears to be random and meaningless.
Decryption is the process of translating random and meaningless data to plain text.

AES

AES Represents the abstract base class from which all implementations of the Advanced Encryption Standard (AES) must inherit. It has specific vulnerabilities with related key attacks. Related key attacks are possible when an attacker knows some data encrypted with several keys, and there is some known relation between them.

Cryptography

Cryptography is used to secure and protect data during communication. It is helpful to prevent unauthorized person or group of users from accessing any confidential data. Encryption and decryption are the two essential functionalities of cryptography. The System.Security.Cryptography namespace provides cryptographic services, including secure encoding and decoding of data.

Why use Encryption and Decryption?

  • Help to protect your confidential data such as passwords.
  • Provide confidentiality of private information data.
  • It helps you to securely protect data that you don't want anyone else to have access.
  • Types of Keys

  • Symmetric Key
  • Asymmetric Key
  • Public Key
  • Private Key
  • Pre-Shared Key
  • Namespaces

    You will need to import the following namespaces.
    
    
    using System; using System.IO; using System.Security.Cryptography; using System.Text; using System.Web.UI;

    Source Code

    
    
    namespace EncryptAndDecrypt { public partial class _Default : Page { public static string EncryptionKey = "MAKV2SPBNI99212"; public static string clearText = "Test123"; protected void Page_Load(object sender, EventArgs e) { string encryptedValue = Encrypt(clearText); string decryptedValue = Decrypt(encryptedValue); } public static string Encrypt(string clearText) { 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 Decrypt(string cipherText) { 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; } } }

    3 comments:

    1. Hi,

      I have a question. If I try to encypt date eg 2020-04-30 using the above method, I got an error like this : System.FormatException: 'Invalid length for a Base-64 char array or string.'

      Do you have any solution ?

      ReplyDelete
      Replies
      1. I will try to solve your issue or give you other solution ASAP.

        Delete