Encrypt & Decrypt Data
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;
using System.Diagnostics;
/// <summary>
/// Summary description for EncryptDecrypt
/// </summary>
public class EncryptDecrypt
{
public EncryptDecrypt()
{
//
// TODO: Add constructor logic here
//
}
public static String Encrypt(string source)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] Key = { 12, 13, 14, 15, 16, 17, 18, 19 };
byte[] IV = { 12, 13, 14, 15, 16, 17, 18, 19 };
ICryptoTransform encryptor = des.CreateEncryptor(Key, IV);
try
{
byte[] IDToBytes = ASCIIEncoding.ASCII.GetBytes(source);
byte[] encryptedID = encryptor.TransformFinalBlock(IDToBytes, 0, IDToBytes.Length);
return Convert.ToBase64String(encryptedID);
}
catch (FormatException)
{
return null;
}
catch (Exception)
{
throw;
}
}
public static string Decrypt(string encrypted)
{
byte[] Key = { 12, 13, 14, 15, 16, 17, 18, 19 };
byte[] IV = { 12, 13, 14, 15, 16, 17, 18, 19 };
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
ICryptoTransform decryptor = des.CreateDecryptor(Key, IV);
try
{
byte[] encryptedIDToBytes = Convert.FromBase64String(encrypted);
byte[] IDToBytes = decryptor.TransformFinalBlock(encryptedIDToBytes, 0, encryptedIDToBytes.Length);
return ASCIIEncoding.ASCII.GetString(IDToBytes);
}
catch (FormatException)
{
return null;
}
catch (Exception)
{
throw;
}
}
}
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;
using System.Diagnostics;
/// <summary>
/// Summary description for EncryptDecrypt
/// </summary>
public class EncryptDecrypt
{
public EncryptDecrypt()
{
//
// TODO: Add constructor logic here
//
}
public static String Encrypt(string source)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] Key = { 12, 13, 14, 15, 16, 17, 18, 19 };
byte[] IV = { 12, 13, 14, 15, 16, 17, 18, 19 };
ICryptoTransform encryptor = des.CreateEncryptor(Key, IV);
try
{
byte[] IDToBytes = ASCIIEncoding.ASCII.GetBytes(source);
byte[] encryptedID = encryptor.TransformFinalBlock(IDToBytes, 0, IDToBytes.Length);
return Convert.ToBase64String(encryptedID);
}
catch (FormatException)
{
return null;
}
catch (Exception)
{
throw;
}
}
public static string Decrypt(string encrypted)
{
byte[] Key = { 12, 13, 14, 15, 16, 17, 18, 19 };
byte[] IV = { 12, 13, 14, 15, 16, 17, 18, 19 };
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
ICryptoTransform decryptor = des.CreateDecryptor(Key, IV);
try
{
byte[] encryptedIDToBytes = Convert.FromBase64String(encrypted);
byte[] IDToBytes = decryptor.TransformFinalBlock(encryptedIDToBytes, 0, encryptedIDToBytes.Length);
return ASCIIEncoding.ASCII.GetString(IDToBytes);
}
catch (FormatException)
{
return null;
}
catch (Exception)
{
throw;
}
}
}
Comments
Post a Comment