Use the same key for encryption and decryption
MyCrypto obj = new
MyCrypto();
Encryption
string MyKey = ConfigurationManager.AppSettings["MYKEY"].ToString();
string MyPass =
obj.EncryptString_AES(Password.Text, MyKey);
Decryption
string MyKey = ConfigurationManager.AppSettings["MYKEY"].ToString();
string MyPass = obj.DecryptString_AES(lblpass.Text,
MyKey);
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Security.Cryptography;
using System.Text;
using System.IO;
namespace LoginAuthentication
{
public class MyCrypto
{
public byte[] key = {
};//key intialization with null
public byte[] IV = {
0x20, 0x40, 0x60, 0x80, 0x00, 0xff, 0xdd, 0xcc };//Vector
IV 8 bit used for DES
public byte[] IVI = {
0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0xad, 0xcc, 0x20, 0x40, 0x60, 0x80, 0x00,
0xff, 0xdd, 0xcc };//Vector IV 16 bit used for AES
public byte[]
Encryptdata = { };
#region DES
public string EncryptString_DES(string stringToEncrypt, string
SEncryptionKey)
{
string EncryptDES = null;
try
{
key = Encoding.UTF8.GetBytes(SEncryptionKey);//get the bytes of key use only UTF8 because DES Supports
only 8 bit
DESCryptoServiceProvider des = new DESCryptoServiceProvider();//DES tech.
byte[] inputByteArray = Encoding.UTF8.GetBytes(stringToEncrypt);//get the bytes of original text
MemoryStream ms = new MemoryStream();//Memory stream used here or use file stream for files
CryptoStream cs = new CryptoStream(ms,
des.CreateEncryptor(key, IV), CryptoStreamMode.Write);
//use cryptostream to write encrypted text
cs.Write(inputByteArray, 0,
inputByteArray.Length);
cs.FlushFinalBlock();
EncryptDES = Convert.ToBase64String(ms.ToArray());//convert byte data to string which is shown to public
}
catch (Exception e)
{
//
}
return EncryptDES;
}
public string
DecryptString_DES(string stringToDecrypt, string sEncryptionKey)
{
string plaintext = null;
byte[] inputByteArray = new
byte[stringToDecrypt.Length + 1];
try
{
key = System.Text.Encoding.UTF8.GetBytes(sEncryptionKey);//get the bytes of key
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
inputByteArray = Convert.FromBase64String(stringToDecrypt);//convert string to byte data
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms,
des.CreateDecryptor(key, IV), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
System.Text.Encoding encoding =
System.Text.Encoding.UTF8;
plaintext = encoding.GetString(ms.ToArray());
}
catch (Exception
e)
{
//
}
return plaintext;
}
#endregion
#region AES
public string
EncryptString_AES(string stringToEncrypt, string SEncryptionKey)
{
string EncryptDES = null;
try
{
// Create a new instance of the AesManaged
// class.
This generates a new key and initialization
// vector (IV).
key = Encoding.UTF32.GetBytes(SEncryptionKey);//use your key here and kept secret.Use UTF32 for 32 bit,
Unicode for 16 bit
using (AesManaged
myAes = new AesManaged())
{
// Encrypt the string to an array of bytes.
byte[] encrypted =
EncryptStringToBytes_Aes(stringToEncrypt, key, IVI);
//Display the encrypted data.
EncryptDES = Convert.ToBase64String(encrypted);//convert byte data to string which is shown to public
}
}
catch (Exception
e)
{
//
}
return EncryptDES;
}
public string
DecryptString_AES(string stringToDecrypt, string sEncryptionKey)
{
string plaintext = null;
try
{
Encryptdata = Convert.FromBase64String(stringToDecrypt);//convert string to byte data
key = Encoding.UTF32.GetBytes(sEncryptionKey);//key same as encrypted key
string roundtrip =
DecryptStringFromBytes_Aes(Encryptdata, key, IVI);
//The decrypted data.
plaintext = roundtrip;
}
catch
{
}
return plaintext;
}
private byte[]
EncryptStringToBytes_Aes(string plainText, byte[] Key, byte[]
IV)
{
// Check arguments.
if (plainText == null
|| plainText.Length <= 0)
throw new ArgumentNullException("plainText");
if (Key == null ||
Key.Length <= 0)
throw new
ArgumentNullException("Key");
if (IV == null ||
IV.Length <= 0)
throw new
ArgumentNullException("Key");
byte[] encrypted;
// Create an AesManaged object
// with the specified key and IV.
using (AesManaged
aesAlg = new AesManaged())
{
aesAlg.Key = Key;
aesAlg.IV = IV;
// Create a decrytor to perform the stream
transform.
ICryptoTransform encryptor =
aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
// Create the streams used for encryption.
using (MemoryStream
msEncrypt = new MemoryStream())
{
using (CryptoStream
csEncrypt = new CryptoStream(msEncrypt,
encryptor, CryptoStreamMode.Write))
{
using (StreamWriter swEncrypt =
new StreamWriter(csEncrypt))
{
//Write all data to the stream.
swEncrypt.Write(plainText);
}
encrypted =
msEncrypt.ToArray();
}
}
}
// Return the encrypted bytes from the memory stream.
return encrypted;
}
private string
DecryptStringFromBytes_Aes(byte[] cipherText, byte[] Key, byte[]
IV)
{
// Check arguments.
if (cipherText == null
|| cipherText.Length <= 0)
throw new
ArgumentNullException("cipherText");
if (Key == null ||
Key.Length <= 0)
throw new
ArgumentNullException("Key");
if (IV == null ||
IV.Length <= 0)
throw new
ArgumentNullException("Key");
// Declare the string used to hold
// the decrypted text.
string plaintext = null;
// Create an AesManaged object
// with the specified key and IV.
using (AesManaged
aesAlg = new AesManaged())
{
aesAlg.Key = Key;
aesAlg.IV = IV;
// Create a decrytor to perform the stream
transform.
ICryptoTransform decryptor =
aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
// Create the streams used for decryption.
using (MemoryStream
msDecrypt = new MemoryStream(cipherText))
{
using (CryptoStream
csDecrypt = new CryptoStream(msDecrypt,
decryptor, CryptoStreamMode.Read))
{
using (StreamReader srDecrypt =
new StreamReader(csDecrypt))
{
// Read the decrypted bytes from the decrypting stream
// and place them in a string.
plaintext =
srDecrypt.ReadToEnd();
}
}
}
}
return plaintext;
}
#endregion
}
}You Can Download the Working Code From here.