DES算法入口参数
DES算法的入口参数有三个:Key、Data、Mode。其中Key为7个字节共56位,是DES算法的工作密钥。Data为8个字节64位,是要被加密或解密的数据;Mode为DES的工作方法,有两种:加密或解密。
- 加密解密文件
/// <summary>
/// Enctypt File
/// </summary>
/// <param name="sInputFilename"></param>
/// <param name="sOutputFilename"></param>
/// <param name="sKey"></param>
public void EncryptFile(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); FileStream fin = null;
FileStream fout = null;
CryptoStream cryptoStream = null;
try
{
fin = new FileStream(sInputFilename, FileMode.Open, FileAccess.Read);
fout = new FileStream(sOutputFilename, FileMode.OpenOrCreate, FileAccess.Write);
cryptoStream = new CryptoStream(fout, DES.CreateEncryptor(), CryptoStreamMode.Write); byte[] bin = new byte[]; //This is intermediate storage for the decryption.
long rdlen = ; //This is the total number of bytes written.
long totlen = fin.Length; //This is the total length of the input file.
int len; //This is the number of bytes to be written at a time. //Read from the input file, then encrypt and write to the output file.
while (rdlen < totlen)
{
len = fin.Read(bin, , );
cryptoStream.Write(bin, , len);
rdlen = rdlen + len;
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (cryptoStream != null) { cryptoStream.Close(); }
if (fout != null) { fout.Close(); }
if (fin != null) { fin.Close(); }
}
} /// <summary>
/// Decrypt File
/// </summary>
/// <param name="sInputFilename"></param>
/// <param name="sOutputFilename"></param>
/// <param name="sKey"></param>
public 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); FileStream fin = null;
FileStream fout = null;
CryptoStream cryptoStream = null;
try
{
fin = new FileStream(sInputFilename, FileMode.Open, FileAccess.Read);
fout = new FileStream(sOutputFilename, FileMode.OpenOrCreate, FileAccess.Write);
cryptoStream = new CryptoStream(fin, DES.CreateDecryptor(), CryptoStreamMode.Read); byte[] bin = new byte[]; //This is intermediate storage for the decryption.
long rdlen = ; //This is the total number of bytes written.
long totlen = fin.Length; //This is the total length of the input file.
int len; //This is the number of bytes to be written at a time. //Read from the input file, then encrypt and write to the output file.
while (rdlen < totlen)
{
len = cryptoStream.Read(bin, , );
if (len == ) { break; }
fout.Write(bin, , len);
rdlen = rdlen + len;
}
}
catch(Exception ex)
{
throw ex;
}
finally
{
if (cryptoStream != null) { cryptoStream.Close(); }
if (fout != null) { fout.Close(); }
if (fin != null) { fin.Close(); }
}
} - 加密解密文本
/// <summary>
/// Encrypt Text
/// </summary>
public string DesEncrypt(string pToEncrypt, string sKey)
{
MemoryStream ms = null;
CryptoStream ctyptoStream = null;
try
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey); byte[] inputByteArray = Encoding.ASCII.GetBytes(pToEncrypt); ms = new System.IO.MemoryStream();
ctyptoStream = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
ctyptoStream.Write(inputByteArray, , inputByteArray.Length);
ctyptoStream.FlushFinalBlock();
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (ctyptoStream != null) { ctyptoStream.Close(); }
if (ms != null) { ms.Close(); }
}
return Convert.ToBase64String(ms.ToArray());
} /// <summary>
/// Dectypt Text
/// </summary>
public string DesDecrypt(string pToDecrypt, string sKey)
{
MemoryStream ms = null;
CryptoStream ctyptoStream = null;
try
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey); byte[] inputByteArray = Convert.FromBase64String(pToDecrypt); ms = new System.IO.MemoryStream();
ctyptoStream = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
ctyptoStream.Write(inputByteArray, , inputByteArray.Length);
ctyptoStream.FlushFinalBlock();
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (ctyptoStream != null) { ctyptoStream.Close(); }
if (ms != null) { ms.Close(); }
}
return Encoding.ASCII.GetString(ms.ToArray());
}