namespace Encrypt_And_Decrypt_PrivateKey
{
public class AesSample
{
Rijndael aes = null;public AesSample()
{
aes = Rijndael.Create();
}
public string Encrypt(string data,byte[] bKey)
{
try
{
byte[] bData = Encoding.UTF8.GetBytes(data);
aes.Key = bKey;
aes.Mode = CipherMode.ECB;
aes.Padding = PaddingMode.PKCS7; ICryptoTransform iCryptoTransform
= aes.CreateEncryptor();
byte[] bResult =
iCryptoTransform.TransformFinalBlock(bData, 0, bData.Length);return Convert.ToBase64String(bResult);
}
catch
{
throw;
}
}public string Decrypt(string data, byte[] bKey)
{
try
{
byte[] bData = Convert.FromBase64String(data);
aes.Key = bKey;
aes.Mode = CipherMode.ECB;
aes.Padding = PaddingMode.PKCS7; ICryptoTransform iCryptoTransform
= aes.CreateDecryptor();
byte[] bResult =
iCryptoTransform.TransformFinalBlock(bData, 0, bData.Length);
return Encoding.UTF8.GetString(bResult);
}
catch
{
throw;
}
} }
//class AesSample
public class DesSample
{
DES des = null;public DesSample()
{
des = new DESCryptoServiceProvider();
}public string Encrypt(string data, byte[] bKey)
{
try
{
byte[] bData = Encoding.UTF8.GetBytes(data);
des.Key = bKey; ICryptoTransform iCryptoTransform
= des.CreateEncryptor();
byte[] bResult =
iCryptoTransform.TransformFinalBlock(bData, 0, bData.Length);return Convert.ToBase64String(bResult);
}
catch
{
throw;
}
}public string Decrypt(string data, byte[] bKey)
{
try
{
byte[] bData = Convert.FromBase64String(data);
des.Key = bKey; ICryptoTransform iCryptoTransform
= des.CreateDecryptor();
byte[] bResult =
iCryptoTransform.TransformFinalBlock(bData, 0, bData.Length);
return Encoding.UTF8.GetString(bResult);
}
catch
{
throw;
}
} }
//class DesSample
public class Rc2Sample
{
RC2 rc2 = null;public Rc2Sample()
{
rc2 = new RC2CryptoServiceProvider();
}public string Encrypt(string data, byte[] bKey)
{
try
{
byte[] bData = Encoding.UTF8.GetBytes(data);
rc2.Key = bKey; ICryptoTransform iCryptoTransform
= rc2.CreateEncryptor();
byte[] bResult =
iCryptoTransform.TransformFinalBlock(bData, 0, bData.Length);return Convert.ToBase64String(bResult);
}
catch
{
throw;
}
}public string Decrypt(string data, byte[] bKey)
{
try
{
byte[] bData = Convert.FromBase64String(data);
rc2.Key = bKey; ICryptoTransform iCryptoTransform
= rc2.CreateDecryptor();
byte[] bResult =
iCryptoTransform.TransformFinalBlock(bData, 0, bData.Length);
return Encoding.UTF8.GetString(bResult);
}
catch
{
throw;
}
}
}public class RijndaelSample
{
Rijndael rijndael = null;public RijndaelSample()
{
rijndael = new RijndaelManaged();
}public string Encrypt(string data, byte[] bKey)
{
try
{
byte[] bData = Encoding.UTF8.GetBytes(data);
rijndael.Key = bKey; ICryptoTransform iCryptoTransform
= rijndael.CreateEncryptor();
byte[] bResult =
iCryptoTransform.TransformFinalBlock(bData, 0, bData.Length);return Convert.ToBase64String(bResult);
}
catch
{
throw;
}
}public string Decrypt(string data, byte[] bKey)
{
try
{
byte[] bData = Convert.FromBase64String(data);
rijndael.Key = bKey; ICryptoTransform iCryptoTransform
= rijndael.CreateDecryptor();
byte[] bResult =
iCryptoTransform.TransformFinalBlock(bData, 0, bData.Length);
return Encoding.UTF8.GetString(bResult);
}
catch
{
throw;
}
} }
public class TripleDESSample
{
TripleDES tDes = null;public TripleDESSample()
{
tDes = new TripleDESCryptoServiceProvider();
}public string Encrypt(string data, byte[] bKey)
{
try
{
byte[] bData = Encoding.UTF8.GetBytes(data);
tDes.Key = bKey; ICryptoTransform iCryptoTransform
= tDes.CreateEncryptor();
byte[] bResult =
iCryptoTransform.TransformFinalBlock(bData, 0, bData.Length);return Convert.ToBase64String(bResult);
}
catch
{
throw;
}
}public string Decrypt(string data, byte[] bKey)
{
try
{
byte[] bData = Convert.FromBase64String(data);
tDes.Key = bKey; ICryptoTransform iCryptoTransform
= tDes.CreateDecryptor();
byte[] bResult =
iCryptoTransform.TransformFinalBlock(bData, 0, bData.Length);
return Encoding.UTF8.GetString(bResult);
}
catch
{
throw;
}
}
}}
//ns
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;namespace Encrypt_And_Decrypt_PrivateKey
{
class Program
{
static void Main(string[] args)
{
byte[] bKey8 = UTF8Encoding.UTF8.GetBytes("12345678");
byte[] bKey16 = UTF8Encoding.UTF8.GetBytes("1234567890123456");
byte[] bKey32 = UTF8Encoding.UTF8.GetBytes("12345678901234567890123456789012");string data = "Hello,I am Jie.";
Console.WriteLine(
"Aes Sample");
AesSample aes = new AesSample();
string aesData = "";
aesData = aes.Encrypt(data, bKey32);
Console.WriteLine("AES Encrypt:{0}", aesData);
Console.WriteLine("AES Decrypt:{0}", aes.Decrypt(aesData, bKey32));
Console.WriteLine(); Console.WriteLine(
"Des Sample");
DesSample des = new DesSample();
string desData = "";
desData = des.Encrypt(data, bKey8);
Console.WriteLine("DES Encrypt:{0}", desData);
Console.WriteLine("DES Decrypt:{0}", des.Decrypt(desData, bKey8));
Console.WriteLine(); Console.WriteLine(
"RC2 Sample");
Rc2Sample rc2 = new Rc2Sample();
string rc2Data = "";
rc2Data = rc2.Encrypt(data, bKey8);
Console.WriteLine("RC2 Encrypt:{0}", rc2Data);
Console.WriteLine("RC2 Decrypt:{0}", rc2.Decrypt(rc2Data, bKey8));
Console.WriteLine(); Console.WriteLine(
"Rijndael Sample");
RijndaelSample rijndael = new RijndaelSample();
string rijndaelData = "";
rijndaelData = rijndael.Encrypt(data, bKey32);
Console.WriteLine("Rijndael Encrypt:{0}", rijndaelData);
Console.WriteLine("Rijndael Decrypt:{0}", rijndael.Decrypt(rijndaelData, bKey32));
Console.WriteLine(); Console.WriteLine(
"TripleDES Sample");
TripleDESSample tripleDES = new TripleDESSample();
string tripleDESData = "";
tripleDESData = tripleDES.Encrypt(data, bKey16);
Console.WriteLine("TripleDES Encrypt:{0}", tripleDESData);
Console.WriteLine("TripleDES Decrypt:{0}", tripleDES.Decrypt(tripleDESData, bKey16));
Console.WriteLine(); Console.Read();
}
}
}