Encryprion and decryption the fields in asp.net or mvc.how to encryption with source code and how to decryption with source code check the full course http://aspdotnetbattu.blogspot.in/
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
How to encrypt and decrypt any fields in
1. How to Encrypt and Decrypt Any
Fields in in ASP.NET or MVC in C#
By
Battu
2. .
• How to Encrypt and Decrypt Any Fields in in
ASP.NET or MVC in C#
3. • Hi This Battu ,in this tutorial i show how to Encryption
and Decryption the given field in login form, Admin
Form or any other forms .When we use the given code
then the original field values encrypted in certain
format and when you use the value then the values is
decrypted , but the value stored in database as
encryption format. so hackers cant hack our account as
well as if you use sessions then the account gets full
tied so hackers cant hack our password or any other
data, in same way if you use stored procedure in
database site then it is impossible to hack any data of
you.
4. we have two methods in the below
code
• 1)public string Encrypt(string plainText,string e
ncryptionKey)
• 2)public string Decrypt(string encryptText, stri
ng decryptionKey)
5. • in above methods we used two parameters ,the
second paramere using for key
•
• using System.IO;
• using System.Text;
• using System.Security.Cryptography;
•
6. Encryption
• public class Utility
• {
•
• #region Protected Field
•
• protected string sqlCommandText;
•
• #endregion
•
• #region Encrypt & Decrypt Methods
•
• public string Encrypt(string plainText,string encryptionKey)
• {
• byte[] clearBytes = Encoding.Unicode.GetBytes(plainText);
• 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();
• }
•
• plainText = Convert.ToBase64String(ms.ToArray());
• }
• }
• return plainText;
• }
7. De cryption
• public string Decrypt(string encryptText, string decryptionKey)
• {
• byte[] encryptBytes = Convert.FromBase64String(encryptText);
• using (Aes encryptor = Aes.Create())
• {
• Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(decryptionKey, 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(encryptBytes, 0, encryptBytes.Length);
• cs.Close();
• }
•
• encryptText = Encoding.Unicode.GetString(ms.ToArray());
• }
• }
• return encryptText;
• }
•