最近逛论坛突然觉得自己也应该写点什么东西了,毕竟用了这么长时间带给我很多帮助,我也应该写些东西在论坛留下一些痕迹。好了多余的话不说了,直接上代码!!!!!
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace EncryptTest
{
internal class Program
{
private static void Main(string[] args)
{
EncriptionTools nn = new EncriptionTools();
string str = nn.MD5_Encript("6666", 16);
Console.WriteLine(nn.MD5_Decript("6666", str, 16));
string str64 = nn.MD5_Encript64Bit("666666666");
Console.WriteLine(str64);
string desStr = nn.DESEncript("ssss", "243i9o85");
Console.WriteLine(desStr);
string desDStr = nn.DESDnScript(desStr, "243i9o85");
Console.WriteLine(desDStr);
string CRCStr = nn.ToCRC16("sadfsla;dfjlasjdf");
Console.WriteLine(CRCStr);
Console.Read();
}
}
public class EncriptionTools
{
#region MD5加密解密
public string MD5_Encript(string str, int bit)
{
MD5CryptoServiceProvider md5Hash = new MD5CryptoServiceProvider();
byte[] dataBytes = Encoding.UTF8.GetBytes(str);
byte[] hashDataBytes = md5Hash.ComputeHash(dataBytes);
string tmpStr = "";
foreach (byte i in hashDataBytes)
{
tmpStr += i.ToString("x2");
}
if (bit == 16)
{
return tmpStr.ToString().Substring(8, 16);
}
if (bit == 32)
{
return tmpStr.ToString();
}
else
{
return string.Empty;
}
}
public bool MD5_Decript(string inputStr, string md5Content, int bit)
{
if (md5Content == MD5_Encript(inputStr, bit))
{
return true;
}
return false;
}
public string MD5_Encript64Bit(string str)
{
MD5 md5 = MD5.Create();
byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(str));
return Convert.ToBase64String(s);
}
#endregion
#region Des加密解密
public string DESEncript(string content, string key)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] byteContent = Encoding.UTF8.GetBytes(content);
des.Key = ASCIIEncoding.ASCII.GetBytes(key);
des.IV = ASCIIEncoding.ASCII.GetBytes(key);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
cs.Write(byteContent, 0, byteContent.Length);
cs.FlushFinalBlock();
StringBuilder ret = new StringBuilder();
foreach (byte b in ms.ToArray())
{
ret.AppendFormat("{0:X2}", b);
}
return ret.ToString();
}
public string DESDnScript(string content, string skey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] byteContent = new byte[content.Length / 2];
for (int x = 0; x < content.Length / 2; x++)
{
int ret = Convert.ToInt32(content.Substring(x * 2, 2), 16);
byteContent[x] = (byte)ret;
}
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(byteContent, 0, byteContent.Length);
cs.FlushFinalBlock();
string result = Encoding.Default.GetString(ms.ToArray());
return result;
}
#endregion
#region CRC校验
public string ToCRC16(string content)
{
byte[] tmpByte = Encoding.UTF8.GetBytes(content);
byte[] crc = CRC16(tmpByte);
return Encoding.UTF8.GetString(crc);
}
public byte[] CRC16(byte[] data)
{
int len = data.Length;
if (len>0)
{
ushort crc = 0xFFFF;
for (int i = 0; i < len; i++)
{
crc = (ushort) (crc ^ data[i]);
for (int j = 0; j < 8; j++)
{
if ((crc&1)!=0)
{
crc = (ushort) ((crc >> 1) ^ 0xA001);
}
else
{
crc = (ushort) (crc >> 1);
}
}
}
byte high = (byte) ((crc & 0xFF00) >> 8);
byte low = (byte) (crc & 0x00FF);
return new byte[] {high,low};
}
return new byte[]{0,0};
}
#endregion
}
}
注意:在使用 DES 进行加密时所用的密码不可以太过简单,一旦简单.Net底层就不会给你通过,就像银行不允许所设密码太过简单一样,当时这个地方花了不少时间,希望你们不要犯和我一样的错误。