c#中的MD5加密与DES加解密
MD5加密
代码
using System.Security.Cryptography;
using System.Text;
#region 加密密码,UserMd5(string str1)
protected string UserMd5(string str1)
{
string cl1 = str1;
string pwd = "";
MD5 md5 = MD5.Create();
// 加密后是一个字节类型的数组
byte[] s=md5.ComputeHash(Encoding.Unicode.GetBytes(cl1));
// 通过使用循环,将字节类型的数组转换为字符串,此字符串是常规字符格式化所得
for(int i = 0 ; i < s.Length; i++)
{
// 将得到的字符串使用十六进制类型格式。格式后的字符是小写的字母,如果使用大写(X)则格式后的字符是大写字符
pwd = pwd + s[i].ToString("x");
}
return pwd;
}
#endregion
using System.Text;
#region 加密密码,UserMd5(string str1)
protected string UserMd5(string str1)
{
string cl1 = str1;
string pwd = "";
MD5 md5 = MD5.Create();
// 加密后是一个字节类型的数组
byte[] s=md5.ComputeHash(Encoding.Unicode.GetBytes(cl1));
// 通过使用循环,将字节类型的数组转换为字符串,此字符串是常规字符格式化所得
for(int i = 0 ; i < s.Length; i++)
{
// 将得到的字符串使用十六进制类型格式。格式后的字符是小写的字母,如果使用大写(X)则格式后的字符是大写字符
pwd = pwd + s[i].ToString("x");
}
return pwd;
}
#endregion
DES加解密
代码
public static string EncryptMethod(string rs) //加密
{
byte[] desKey = new byte[]{0x16, 0x09, 0x14, 0x15, 0x07, 0x01, 0x05, 0x08};
byte[] desIV = new byte[]{0x16, 0x09, 0x14, 0x15, 0x07, 0x01, 0x05, 0x08};
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
try
{
byte[] inputByteArray = Encoding.Default.GetBytes(rs);
//byte[] inputByteArray=Encoding.Unicode.GetBytes(rs);
des.Key = desKey; // ASCIIEncoding.ASCII.GetBytes(sKey);
des.IV = desIV; //ASCIIEncoding.ASCII.GetBytes(sKey);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(),
CryptoStreamMode.Write);
//Write the byte array into the crypto stream
//(It will end up in the memory stream)
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
//Get the data back from the memory stream, and into a string
StringBuilder ret = new StringBuilder();
foreach(byte b in ms.ToArray())
{
//Format as hex
ret.AppendFormat("{0:X2}", b);
}
ret.ToString();
return ret.ToString();
}
catch
{
return rs;
}
finally
{
des = null;
}
}
public static string DecryptMethod(string rs) //解密
{
byte[] desKey = new byte[]{0x16, 0x09, 0x14, 0x15, 0x07, 0x01, 0x05, 0x08};
byte[] desIV = new byte[]{0x16, 0x09, 0x14, 0x15, 0x07, 0x01, 0x05, 0x08};
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
try
{
//Put the input string into the byte array
byte[] inputByteArray = new byte[rs.Length / 2];
for(int x = 0; x < rs.Length / 2; x++)
{
int i = (Convert.ToInt32(rs.Substring(x * 2, 2), 16));
inputByteArray[x] = (byte)i;
}
des.Key = desKey; //ASCIIEncoding.ASCII.GetBytes(sKey);
des.IV = desIV; //ASCIIEncoding.ASCII.GetBytes(sKey);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(),CryptoStreamMode.Write);
//Flush the data through the crypto stream into the memory stream
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
//Get the decrypted data back from the memory stream
StringBuilder ret = new StringBuilder();
return System.Text.Encoding.Default.GetString(ms.ToArray());
}
catch
{
return rs;
}
finally
{
des = null;
}
}
{
byte[] desKey = new byte[]{0x16, 0x09, 0x14, 0x15, 0x07, 0x01, 0x05, 0x08};
byte[] desIV = new byte[]{0x16, 0x09, 0x14, 0x15, 0x07, 0x01, 0x05, 0x08};
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
try
{
byte[] inputByteArray = Encoding.Default.GetBytes(rs);
//byte[] inputByteArray=Encoding.Unicode.GetBytes(rs);
des.Key = desKey; // ASCIIEncoding.ASCII.GetBytes(sKey);
des.IV = desIV; //ASCIIEncoding.ASCII.GetBytes(sKey);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(),
CryptoStreamMode.Write);
//Write the byte array into the crypto stream
//(It will end up in the memory stream)
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
//Get the data back from the memory stream, and into a string
StringBuilder ret = new StringBuilder();
foreach(byte b in ms.ToArray())
{
//Format as hex
ret.AppendFormat("{0:X2}", b);
}
ret.ToString();
return ret.ToString();
}
catch
{
return rs;
}
finally
{
des = null;
}
}
public static string DecryptMethod(string rs) //解密
{
byte[] desKey = new byte[]{0x16, 0x09, 0x14, 0x15, 0x07, 0x01, 0x05, 0x08};
byte[] desIV = new byte[]{0x16, 0x09, 0x14, 0x15, 0x07, 0x01, 0x05, 0x08};
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
try
{
//Put the input string into the byte array
byte[] inputByteArray = new byte[rs.Length / 2];
for(int x = 0; x < rs.Length / 2; x++)
{
int i = (Convert.ToInt32(rs.Substring(x * 2, 2), 16));
inputByteArray[x] = (byte)i;
}
des.Key = desKey; //ASCIIEncoding.ASCII.GetBytes(sKey);
des.IV = desIV; //ASCIIEncoding.ASCII.GetBytes(sKey);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(),CryptoStreamMode.Write);
//Flush the data through the crypto stream into the memory stream
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
//Get the decrypted data back from the memory stream
StringBuilder ret = new StringBuilder();
return System.Text.Encoding.Default.GetString(ms.ToArray());
}
catch
{
return rs;
}
finally
{
des = null;
}
}
DES加解密1
代码
public static byte[] DESKey = new byte[] {0x82, 0xBC, 0xA1, 0x6A, 0xF5, 0x87, 0x3B, 0xE6, 0x59, 0x6A, 0x32, 0x64, 0x7F, 0x3A, 0x2A, 0xBB, 0x2B, 0x68, 0xE2, 0x5F, 0x06, 0xFB, 0xB8, 0x2D, 0x67, 0xB3, 0x55, 0x19, 0x4E, 0xB8, 0xBF, 0xDD };
//public static byte[] DESKey = new byte[32];
public static void GetKey()
{
string sKey = "1234567890abcdefghij1234567890yz";
char[] cKey = sKey.ToCharArray();
for(int i=0;i<32;i++)
DESKey[i] = (byte)cKey[i];
}
//注册表操作using Microsoft.Win32;
private void TestCode()
{
string sValue = RegistryManager.DESEncrypt(textBox1.Text.Trim());
sValue = RegistryManager.DESDecrypt(sValue);
}
/// <summary>
/// DES加密
/// </summary>
/// <param name="strSource">待加密字串</param>
/// <param name="key">32位Key值</param>
/// <returns>加密后的字符串</returns>
public static string DESEncrypt(string strSource)
{
return DESEncrypt(strSource, DESKey);
}
public static string DESEncrypt(string strSource,byte[] key)
{
GetKey();
SymmetricAlgorithm sa = Rijndael.Create();
sa.Key = key;
sa.Mode= CipherMode.ECB;
sa.Padding = PaddingMode.Zeros;
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, sa.CreateEncryptor(), CryptoStreamMode.Write);
byte[] byt = Encoding.Unicode.GetBytes(strSource);
cs.Write(byt, 0, byt.Length);
cs.FlushFinalBlock();
cs.Close();
return Convert.ToBase64String(ms.ToArray());
}
/// <summary>
/// DES解密
/// </summary>
/// <param name="strSource">待解密的字串</param>
/// <param name="key">32位Key值</param>
/// <returns>解密后的字符串</returns>
public static string DESDecrypt(string strSource)
{
return DESDecrypt(strSource, DESKey);
}
public static string DESDecrypt(string strSource,byte[] key)
{
SymmetricAlgorithm sa = Rijndael.Create();
sa.Key = key;
sa.Mode = CipherMode.ECB;
sa.Padding = PaddingMode.Zeros;
ICryptoTransform ct = sa.CreateDecryptor();
byte[] byt = Convert.FromBase64String(strSource);
MemoryStream ms = new MemoryStream(byt);
CryptoStream cs = new CryptoStream(ms, ct, CryptoStreamMode.Read);
StreamReader sr = new StreamReader(cs, Encoding.Unicode);
return sr.ReadToEnd();
}
//public static byte[] DESKey = new byte[32];
public static void GetKey()
{
string sKey = "1234567890abcdefghij1234567890yz";
char[] cKey = sKey.ToCharArray();
for(int i=0;i<32;i++)
DESKey[i] = (byte)cKey[i];
}
//注册表操作using Microsoft.Win32;
private void TestCode()
{
string sValue = RegistryManager.DESEncrypt(textBox1.Text.Trim());
sValue = RegistryManager.DESDecrypt(sValue);
}
/// <summary>
/// DES加密
/// </summary>
/// <param name="strSource">待加密字串</param>
/// <param name="key">32位Key值</param>
/// <returns>加密后的字符串</returns>
public static string DESEncrypt(string strSource)
{
return DESEncrypt(strSource, DESKey);
}
public static string DESEncrypt(string strSource,byte[] key)
{
GetKey();
SymmetricAlgorithm sa = Rijndael.Create();
sa.Key = key;
sa.Mode= CipherMode.ECB;
sa.Padding = PaddingMode.Zeros;
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, sa.CreateEncryptor(), CryptoStreamMode.Write);
byte[] byt = Encoding.Unicode.GetBytes(strSource);
cs.Write(byt, 0, byt.Length);
cs.FlushFinalBlock();
cs.Close();
return Convert.ToBase64String(ms.ToArray());
}
/// <summary>
/// DES解密
/// </summary>
/// <param name="strSource">待解密的字串</param>
/// <param name="key">32位Key值</param>
/// <returns>解密后的字符串</returns>
public static string DESDecrypt(string strSource)
{
return DESDecrypt(strSource, DESKey);
}
public static string DESDecrypt(string strSource,byte[] key)
{
SymmetricAlgorithm sa = Rijndael.Create();
sa.Key = key;
sa.Mode = CipherMode.ECB;
sa.Padding = PaddingMode.Zeros;
ICryptoTransform ct = sa.CreateDecryptor();
byte[] byt = Convert.FromBase64String(strSource);
MemoryStream ms = new MemoryStream(byt);
CryptoStream cs = new CryptoStream(ms, ct, CryptoStreamMode.Read);
StreamReader sr = new StreamReader(cs, Encoding.Unicode);
return sr.ReadToEnd();
}
有些加密的还可以起到别的作用,因为.net的mobile上不支持一些特殊的算法(支持MD5但MD5不可解密的)上面写的两种都不支持所以只好另写:
代码
private string aa(string bb)
{
byte[] by=new byte[bb.Length];
by=System.Text.Encoding.UTF8.GetBytes(bb);
string r=Convert.ToBase64String(by);
return r;
}
private string bb(string aa)
{
byte[] by=Convert.FromBase64String(aa);
string r=Encoding.UTF8.GetString(by);
return r;
}
{
byte[] by=new byte[bb.Length];
by=System.Text.Encoding.UTF8.GetBytes(bb);
string r=Convert.ToBase64String(by);
return r;
}
private string bb(string aa)
{
byte[] by=Convert.FromBase64String(aa);
string r=Encoding.UTF8.GetString(by);
return r;
}


浙公网安备 33010602011771号