DES加密应该是最基础的加密算法,为了搞清楚它在.NET C#里面的用法,我费了不少功夫,希望有心人能看到。
DES一共就有4个参数参与运作:明文、密文、密钥、向量。为了初学者容易理解,可以把4个参数的关系写成:密文=明文+密钥+向量;明文=密文-密钥-向量。为什么要向量这个参数呢?因为如果有一篇文章,有几个词重复,那么这个词加上密钥形成的密文,仍然会重复,这给破解者有机可乘,破解者可以根据重复的内容,猜出是什么词,然而一旦猜对这个词,那么,他就能算出密钥,整篇文章就被破解了!加上向量这个参数以后,每块文字段都会依次加上一段值,这样,即使相同的文字,加密出来的密文,也是不一样的,算法的安全性大大提高!
using System; //这个是使用DES的基础 using System.Security.Cryptography; //这个是处理文字编码的前提 using System.Text; //以“流”的形式处理文字,也是微软DES算法要求的 using System.IO; /// <summary> /// DES加密方法 /// </summary> /// <param name="strPlain">明文</param> /// <param name="strDESKey">密钥</param> /// <param name="strDESIV">向量</param> /// <returns>密文</returns> public string DESEncrypt(string strPlain,string strDESKey,string strDESIV)
{ //把密钥转换成字节数组 byte[] bytesDESKey=ASCIIEncoding.ASCII.GetBytes(strDESKey); //把向量转换成字节数组 byte[] bytesDESIV=ASCIIEncoding.ASCII.GetBytes(strDESIV); //声明1个新的DES对象 DESCryptoServiceProvider desEncrypt=new DESCryptoServiceProvider(); //开辟一块内存流 MemoryStream msEncrypt=new MemoryStream(); //把内存流对象包装成加密流对象 CryptoStream csEncrypt=new CryptoStream(msEncrypt,desEncrypt.CreateEncryptor(bytesDESKey,bytesDESIV),CryptoStreamMode.Write); //把加密流对象包装成写入流对象 StreamWriter swEncrypt=new StreamWriter(csEncrypt); //写入流对象写入明文 swEncrypt.WriteLine(strPlain); //写入流关闭 swEncrypt.Close(); //加密流关闭 csEncrypt.Close(); //把内存流转换成字节数组,内存流现在已经是密文了 byte[] bytesCipher=msEncrypt.ToArray(); //内存流关闭 msEncrypt.Close(); //把密文字节数组转换为字符串,并返回 return UnicodeEncoding.Unicode.GetString(bytesCipher);
} /// <summary> /// DES解密方法 /// </summary> /// <param name="strCipher">密文</param> /// <param name="strDESKey">密钥</param> /// <param name="strDESIV">向量</param> /// <returns>明文</returns> public string DESDecrypt(string strCipher,string strDESKey,string strDESIV)
{ //把密钥转换成字节数组 byte[] bytesDESKey=ASCIIEncoding.ASCII.GetBytes(strDESKey); //把向量转换成字节数组 byte[] bytesDESIV=ASCIIEncoding.ASCII.GetBytes(strDESIV); //把密文转换成字节数组 byte[] bytesCipher=UnicodeEncoding.Unicode.GetBytes(strCipher); //声明1个新的DES对象 DESCryptoServiceProvider desDecrypt=new DESCryptoServiceProvider(); //开辟一块内存流,并存放密文字节数组 MemoryStream msDecrypt=new MemoryStream(bytesCipher); //把内存流对象包装成解密流对象 CryptoStream csDecrypt=new CryptoStream(msDecrypt,desDecrypt.CreateDecryptor(bytesDESKey,bytesDESIV),CryptoStreamMode.Read); //把解密流对象包装成读出流对象 StreamReader srDecrypt=new StreamReader(csDecrypt); //明文=读出流的读出内容 string strPlainText=srDecrypt.ReadLine(); //读出流关闭 srDecrypt.Close(); //解密流关闭 csDecrypt.Close(); //内存流关闭 msDecrypt.Close(); //返回明文 return strPlainText;
}
C# Desc加密算法
使用注意事项:sKey要为8位,或16位字符
using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.IO;
public class DesSecurity
{
/// <summary>
/// 加密原函数
/// </summary>
/// <param name="pToEncrypt"></param>
/// <param name="sKey"></param>
/// <returns></returns>
public string DesEncrypt(string pToEncrypt, string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
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();
//return a;
}
/// <summary>
/// 解密原函数
/// </summary>
/// <param name="pToDecrypt"></param>
/// <param name="sKey"></param>
/// <returns></returns>
public string DesDecrypt(string pToDecrypt, string sKey)
{
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(sKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
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());
}
}
|
请发表评论