Codeclass DES
{
/// <summary>
/// Call this function to remove the key from memory after use for security
/// </summary>
/// <param name="Destination"></param>
/// <param name="Length"></param>
/// <returns></returns>
[System.Runtime.InteropServices.DllImport("KERNEL32.DLL", EntryPoint = "RtlZeroMemory")]
public static extern bool ZeroMemory(IntPtr Destination, int Length);
/// <summary>
/// Function to Generate a 64 bits Key.
/// </summary>
/// <returns>返回生成的密钥</returns>
public static string GenerateKey()
{
// Create an instance of Symetric Algorithm. Key and IV is generated automatically.
DESCryptoServiceProvider desCrypto = (DESCryptoServiceProvider)DESCryptoServiceProvider.Create();
// Use the Automatically generated key for Encryption.
return ASCIIEncoding.ASCII.GetString(desCrypto.Key);
}
#region 加密文件
/// <summary>
/// 加密文件
/// </summary>
/// <param name="sInputFilename">要加密的文件</param>
/// <param name="sOutputFilename">加密后保存的文件</param>
/// <param name="sKey">密钥</param>
public static void EncryptFile(string sInputFilename, string sOutputFilename, string sKey)
{
using (FileStream fsInput = new FileStream(sInputFilename, FileMode.Open, FileAccess.Read))
{
byte[] bytearrayinput = new byte[fsInput.Length];
fsInput.Read(bytearrayinput, 0, bytearrayinput.Length);
fsInput.Close();
FileStream fsEncrypted = new FileStream(sOutputFilename,
FileMode.OpenOrCreate,
FileAccess.Write);
DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
ICryptoTransform desencrypt = DES.CreateEncryptor();
CryptoStream cryptostream = new CryptoStream(fsEncrypted, desencrypt, CryptoStreamMode.Write);
cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length);
cryptostream.Close();
fsEncrypted.Close();
}
}
#endregion
#region 解密文件
/// <summary>
/// 解密文件
/// </summary>
/// <param name="sInputFilename">要解密的文件</param>
/// <param name="sOutputFilename">解决后保存的文件</param>
/// <param name="sKey">密钥</param>
public static void DecryptFile(string sInputFilename, string sOutputFilename, string sKey)
{
DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
//A 64 bit key and IV is required for this provider.
//Set secret key For DES algorithm.
DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
//Set initialization vector.
DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
//Create a file stream to read the encrypted file back.
using (FileStream fsread = new FileStream(sInputFilename, FileMode.Open, FileAccess.Read))
{
//Create a DES decryptor from the DES instance.
ICryptoTransform desdecrypt = DES.CreateDecryptor();
//Create crypto stream set to read and do a
//DES decryption transform on incoming bytes.
CryptoStream cryptostreamDecr = new CryptoStream(fsread, desdecrypt, CryptoStreamMode.Read);
//Print the contents of the decrypted file.
StreamWriter fsDecrypted = new StreamWriter(sOutputFilename);
fsDecrypted.Write(new StreamReader(cryptostreamDecr).ReadToEnd());
fsDecrypted.Flush();
fsDecrypted.Close();
}
}
/// <summary>
/// 解密文件并返回内容
/// </summary>
/// <param name="sInputFilename">要解密的文件路径</param>
/// <param name="sKey">密钥</param>
/// <returns>返回内容</returns>
public static string DecryptFile(string sInputFilename, string sKey)
{
try
{
DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
//A 64 bit key and IV is required for this provider.
//Set secret key For DES algorithm.
DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
//Set initialization vector.
DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
//Create a file stream to read the encrypted file back.
using (FileStream fsread = new FileStream(sInputFilename, FileMode.Open, FileAccess.Read))
{
byte[] byt = new byte[fsread.Length];
fsread.Read(byt, 0, byt.Length);
fsread.Flush();
fsread.Close();
//Create a DES decryptor from the DES instance.
ICryptoTransform desdecrypt = DES.CreateDecryptor();
MemoryStream ms = new MemoryStream();
CryptoStream cryptostreamDecr = new CryptoStream(ms, desdecrypt, CryptoStreamMode.Write);
cryptostreamDecr.Write(byt, 0, byt.Length);
cryptostreamDecr.FlushFinalBlock();
cryptostreamDecr.Close();
return Encoding.UTF8.GetString(ms.ToArray()).Trim();
}
}
catch
{
return "";
}
}
#endregion
#region DES加密
/// <summary>
/// DES加密
/// </summary>
/// <param name="pToEncrypt">需要加密的字符串</param>
/// <returns>加密后的字符串</returns>
public static string DESEncrypt(string pToEncrypt, string DesKeyStr)
{
try
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
des.Key = ASCIIEncoding.ASCII.GetBytes(DesKeyStr);
des.IV = ASCIIEncoding.ASCII.GetBytes(DesKeyStr);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
StringBuilder ret = new StringBuilder();
foreach (byte b in ms.ToArray())
{
ret.AppendFormat("{0:X2}", b);
}
ret.ToString();
return ret.ToString();
}
catch
{
System.Windows.Forms.MessageBox.Show("输入注册码错误,请重新输入!!");
return "";
}
}
#endregion
#region DES解密
/// <summary>
/// DES解密
/// </summary>
/// <param name="pToDecrypt">加密后的字符串</param>
/// <returns>解密后的字符串</returns>
public static string DESDecrypt(string pToDecrypt, string DesKeyStr)
{
try
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
for (int x = 0; x < pToDecrypt.Length / 2; x++)
{
int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
inputByteArray[x] = (byte)i;
}
des.Key = ASCIIEncoding.ASCII.GetBytes(DesKeyStr);
des.IV = ASCIIEncoding.ASCII.GetBytes(DesKeyStr);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
StringBuilder ret = new StringBuilder();
return System.Text.Encoding.Default.GetString(ms.ToArray());
}
catch
{
System.Windows.Forms.MessageBox.Show("输入注册码错误,请重新输入!!");
return "";
}
}
#endregion
}