----------des 8位key---
/// <summary>
/// 字符串加密
/// </summary>
/// <param name="strText">要加密的字符串</param>
/// <param name="strEncrKey">密钥</param>
/// <returns>返回加密后的字符串</returns>
public static string DesEncrypt(string strText, string strEncrKey)
{
byte[] byKey = null;
byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
try
{
byKey = System.Text.Encoding.UTF8.GetBytes(strEncrKey.Substring(0, strEncrKey.Length));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = Encoding.UTF8.GetBytes(strText);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
return Convert.ToBase64String(ms.ToArray());
}
catch (System.Exception error)
{
return "error:" + error.Message + "/r";
}
}
/// <summary>
/// 字符串解密
/// </summary>
/// <param name="strText">要解密的字符串</param>
/// <param name="sDecrKey">密钥</param>
/// <returns>返回解密后的字符串</returns>
public static string DesDecrypt(string strText, string sDecrKey)
{
byte[] byKey = null;
byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
byte[] inputByteArray = new Byte[strText.Length];
try
{
byKey = System.Text.Encoding.UTF8.GetBytes(sDecrKey.Substring(0, 8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
inputByteArray = Convert.FromBase64String(strText);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
System.Text.Encoding encoding = new System.Text.UTF8Encoding();
return encoding.GetString(ms.ToArray());
}
catch (System.Exception error)
{
return "error:" + error.Message + "/r";
}
}
-------3des 16位key---------
/// <summary>
/// 3des加密字符串
/// </summary>
/// <param name= "a_strString "> 要加密的字符串 </param>
/// <param name= "a_strKey "> 密钥 </param>
/// <returns> 加密后并经base64编码的字符串 </returns>
/// <remarks> 静态方法,采用默认ascii编码 </remarks>
public static string EncryptDES16(string a_strString, string a_strKey)
{
DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
//TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider();
//MD5CryptoServiceProvider hashMD5 = new MD5CryptoServiceProvider();
byte[] b = strToToHexByte(a_strKey);
DES.Key = b;
// DES.IV = b;
// DES.Key = b;//ASCIIEncoding.ASCII.GetBytes(a_strKey);//hashMD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(a_strKey));
DES.Mode = CipherMode.ECB;
DES.Padding = PaddingMode.PKCS7;
ICryptoTransform DESEncrypt = DES.CreateEncryptor();
byte[] Buffer = ASCIIEncoding.UTF8.GetBytes(a_strString);
return Convert.ToBase64String(DESEncrypt.TransformFinalBlock(Buffer, 0, Buffer.Length));
}//end method
private static byte[] strToToHexByte(string hexString)
{
byte[] returnBytes = new byte[hexString.Length / 2];
for (int i = 0; i < returnBytes.Length; i++)
returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
return returnBytes;
}
/// <summary>
/// 3des解密字符串
/// </summary>
/// <param name= "a_strString "> 要解密的字符串 </param>
/// <param name= "a_strKey "> 密钥 </param>
/// <returns> 解密后的字符串 </returns>
/// <exception cref= " "> 密钥错误 </exception>
/// <remarks> 静态方法,采用默认ascii编码 </remarks>
public static string DecryptDES16(string a_strString, string a_strKey)
{
DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
AesCryptoServiceProvider AES = new AesCryptoServiceProvider();
DES.Key = strToToHexByte(a_strKey);
DES.Mode = CipherMode.ECB;
DES.Padding = PaddingMode.PKCS7;
ICryptoTransform DESDecrypt = DES.CreateDecryptor();
string result = " ";
try
{
byte[] Buffer = Convert.FromBase64String(a_strString);
result = ASCIIEncoding.UTF8.GetString(DESDecrypt.TransformFinalBlock(Buffer, 0, Buffer.Length));
}
catch (Exception e)
{
throw (new Exception("Invalid Key or input string is not a valid base64 string ", e));
}
return result;
}
----------AES----------
public static byte[] AESEncript(byte[] s1, byte[] key, byte[] iv, byte[] mainkey)
{
AesCryptoServiceProvider acsp = new AesCryptoServiceProvider();
acsp.Mode = CipherMode.ECB;
acsp.Padding = PaddingMode.PKCS7 ;
//acsp.Key = mainkey;
MemoryStream mstream = new MemoryStream();
CryptoStream cstream = new CryptoStream(mstream, acsp.CreateEncryptor(key, iv), CryptoStreamMode.Write);
cstream.Write(s1, 0, s1.Length);
cstream.FlushFinalBlock();
byte[] outb1 = mstream.ToArray();
cstream.Close();
mstream.Close();
return outb1;
}
private static byte[] _key1 = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
public static byte[] AESDecript(byte[] s2, byte[] key, byte[] iv, byte[] mainkey)
{
AesCryptoServiceProvider acsp = new AesCryptoServiceProvider();
acsp.Mode = CipherMode.ECB;
acsp.Padding = PaddingMode.PKCS7;
//acsp.Key = mainkey;
MemoryStream mtream2 = new MemoryStream();
CryptoStream deStreame = new CryptoStream(mtream2, acsp.CreateDecryptor(key, iv), CryptoStreamMode.Write);
deStreame.Write(s2, 0, s2.Length);
deStreame.FlushFinalBlock();
byte[] outs2 = mtream2.ToArray();
mtream2.Close();
deStreame.Close();
return outs2;
}
调用
AesCryptoServiceProvider acsp = new AesCryptoServiceProvider();
byte[] skey = Encoding.Default.GetBytes(key);
byte[] siv = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
-------------md5--------------
public static string Md5(string str, int code)
{
if (code == 16) //16位MD5加密(取32位加密的9~25字符)
{
return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5").ToLower().Substring(8, 16);
}
else//32位加密
{
return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5").ToLower();
}
}
------------------------------