Asp.Net 加密解密
#region DES加密解密
///
/// DES加密
///
/// 待加密字串
/// 32位Key值
///加密後的字串
public string DESEncrypt(string strSource)
{
return DESEncrypt(strSource, DESKey);
}
public string DESEncrypt(string strSource, byte[] key)
{
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());
}
///
/// DES解密
///
/// 待解密的字串
/// 32位Key值
///解密後的字串
public string DESDecrypt(string strSource)
{
return DESDecrypt(strSource, DESKey);
}
public 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();
}
#endregion
///
/// DES加密
///
/// 待加密字串
/// 32位Key值
///
public string DESEncrypt(string strSource)
{
return DESEncrypt(strSource, DESKey);
}
public string DESEncrypt(string strSource, byte[] key)
{
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());
}
///
/// DES解密
///
/// 待解密的字串
/// 32位Key值
///
public string DESDecrypt(string strSource)
{
return DESDecrypt(strSource, DESKey);
}
public 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();
}
#endregion
#region 一個用hash實現的加密解密方法
///
/// 加密
///
///
///
public static string EncryptStrByHash(string src)
{
if (src.Length == 0)
{
return "";
}
byte[] HaKey = System.Text.Encoding.ASCII.GetBytes((src + "Test").ToCharArray());
byte[] HaData = new byte[20];
HMACSHA1 Hmac = new HMACSHA1(HaKey);
CryptoStream cs = new CryptoStream(Stream.Null, Hmac, CryptoStreamMode.Write);
try
{
cs.Write(HaData, 0, HaData.Length);
}
finally
{
cs.Close();
}
string HaResult = System.Convert.ToBase64String(Hmac.Hash).Substring(0, 16);
byte[] RiKey = System.Text.Encoding.ASCII.GetBytes(HaResult.ToCharArray());
byte[] RiDataBuf = System.Text.Encoding.ASCII.GetBytes(src.ToCharArray());
byte[] EncodedBytes = { };
MemoryStream ms = new MemoryStream();
RijndaelManaged rv = new RijndaelManaged();
cs = new CryptoStream(ms, rv.CreateEncryptor(RiKey, RiKey), CryptoStreamMode.Write);
try
{
cs.Write(RiDataBuf, 0, RiDataBuf.Length);
cs.FlushFinalBlock();
EncodedBytes = ms.ToArray();
}
finally
{
ms.Close();
cs.Close();
}
return HaResult + System.Convert.ToBase64String(EncodedBytes);
}
///
/// 加密
///
///
///
public static string EncryptStrByHash(string src)
{
if (src.Length == 0)
{
return "";
}
byte[] HaKey = System.Text.Encoding.ASCII.GetBytes((src + "Test").ToCharArray());
byte[] HaData = new byte[20];
HMACSHA1 Hmac = new HMACSHA1(HaKey);
CryptoStream cs = new CryptoStream(Stream.Null, Hmac, CryptoStreamMode.Write);
try
{
cs.Write(HaData, 0, HaData.Length);
}
finally
{
cs.Close();
}
string HaResult = System.Convert.ToBase64String(Hmac.Hash).Substring(0, 16);
byte[] RiKey = System.Text.Encoding.ASCII.GetBytes(HaResult.ToCharArray());
byte[] RiDataBuf = System.Text.Encoding.ASCII.GetBytes(src.ToCharArray());
byte[] EncodedBytes = { };
MemoryStream ms = new MemoryStream();
RijndaelManaged rv = new RijndaelManaged();
cs = new CryptoStream(ms, rv.CreateEncryptor(RiKey, RiKey), CryptoStreamMode.Write);
try
{
cs.Write(RiDataBuf, 0, RiDataBuf.Length);
cs.FlushFinalBlock();
EncodedBytes = ms.ToArray();
}
finally
{
ms.Close();
cs.Close();
}
return HaResult + System.Convert.ToBase64String(EncodedBytes);
}
///
/// 解密
///
///
///
public static string DecrypStrByHash(string src)
{
if (src.Length < 40) return "";
byte[] SrcBytes = System.Convert.FromBase64String(src.Substring(16));
byte[] RiKey = System.Text.Encoding.ASCII.GetBytes(src.Substring(0, 16).ToCharArray());
byte[] InitialText = new byte[SrcBytes.Length];
RijndaelManaged rv = new RijndaelManaged();
MemoryStream ms = new MemoryStream(SrcBytes);
CryptoStream cs = new CryptoStream(ms, rv.CreateDecryptor(RiKey, RiKey), CryptoStreamMode.Read);
try
{
cs.Read(InitialText, 0, InitialText.Length);
}
finally
{
ms.Close();
cs.Close();
}
System.Text.StringBuilder Result = new System.Text.StringBuilder();
for (int i = 0; i < InitialText.Length; ++i) if (InitialText[i] > 0) Result.Append((char)InitialText[i]);
return Result.ToString();
}
/// 解密
///
///
///
public static string DecrypStrByHash(string src)
{
if (src.Length < 40) return "";
byte[] SrcBytes = System.Convert.FromBase64String(src.Substring(16));
byte[] RiKey = System.Text.Encoding.ASCII.GetBytes(src.Substring(0, 16).ToCharArray());
byte[] InitialText = new byte[SrcBytes.Length];
RijndaelManaged rv = new RijndaelManaged();
MemoryStream ms = new MemoryStream(SrcBytes);
CryptoStream cs = new CryptoStream(ms, rv.CreateDecryptor(RiKey, RiKey), CryptoStreamMode.Read);
try
{
cs.Read(InitialText, 0, InitialText.Length);
}
finally
{
ms.Close();
cs.Close();
}
System.Text.StringBuilder Result = new System.Text.StringBuilder();
for (int i = 0; i < InitialText.Length; ++i) if (InitialText[i] > 0) Result.Append((char)InitialText[i]);
return Result.ToString();
}
///
/// 對加密後的密文重新編碼,如果密文長>16,則去掉前16個字元,如果長度小於16,返回空字串
///
///
///
public string ReEncryptStrByHash(string s)
{
string e = Encrypt.EncryptStrByHash(s);
return ((e.Length > 16) ? e.Substring(16) : "");
}
#endregion
/// 對加密後的密文重新編碼,如果密文長>16,則去掉前16個字元,如果長度小於16,返回空字串
///
///
///
public string ReEncryptStrByHash(string s)
{
string e = Encrypt.EncryptStrByHash(s);
return ((e.Length > 16) ? e.Substring(16) : "");
}
#endregion
#region Md5加密,生成16位或32位,生成的密文都是大寫
public static string Md5To16(string str)
{
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
string t2 = BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(str)), 4, 8);
t2 = t2.Replace("-", "");
return t2;
}
public static string Md5To16(string str)
{
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
string t2 = BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(str)), 4, 8);
t2 = t2.Replace("-", "");
return t2;
}
////
/// MD5 32位加密
///
///
///
public static string Md5To32(string str)
{
string pwd = "";
MD5 md5 = MD5.Create();
byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(str));
for (int i = 0; i < s.Length; i++)
{
pwd = pwd + s[i].ToString("X");
}
return pwd;
}
#endregion
/// MD5 32位加密
///
///
///
public static string Md5To32(string str)
{
string pwd = "";
MD5 md5 = MD5.Create();
byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(str));
for (int i = 0; i < s.Length; i++)
{
pwd = pwd + s[i].ToString("X");
}
return pwd;
}
#endregion
#region 3DES加密解密
public string Encrypt3DES(string str)
{
//金鑰
string sKey = "wyw308";
// //向量,可為空
string sIV = "scf521";
// //構造對稱演算法
SymmetricAlgorithm mCSP = new TripleDESCryptoServiceProvider();
public string Encrypt3DES(string str)
{
//金鑰
string sKey = "wyw308";
// //向量,可為空
string sIV = "scf521";
// //構造對稱演算法
SymmetricAlgorithm mCSP = new TripleDESCryptoServiceProvider();
ICryptoTransform. ct;
MemoryStream ms;
CryptoStream cs;
byte[] byt;
mCSP.Key = Convert.FromBase64String(sKey);
mCSP.IV = Convert.FromBase64String(sIV);
mCSP.Mode = System.Security.Cryptography.CipherMode.ECB;
mCSP.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
ct = mCSP.CreateEncryptor(mCSP.Key, mCSP.IV);
byt = Encoding.UTF8.GetBytes(str);
ms = new MemoryStream();
cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
cs.Write(byt, 0, byt.Length);
cs.FlushFinalBlock();
cs.Close();
return Convert.ToBase64String(ms.ToArray());
}
///
/// 帶指定金鑰和向量的3DES加密
///
///
///
///
///
public string Encrypt3DES(string str, string sKey, string sIV)
{
SymmetricAlgorithm mCSP = new TripleDESCryptoServiceProvider();
ICryptoTransform. ct;
MemoryStream ms;
CryptoStream cs;
byte[] byt;
mCSP.Key = Convert.FromBase64String(sKey);
mCSP.IV = Convert.FromBase64String(sIV);
mCSP.Mode = System.Security.Cryptography.CipherMode.ECB;
mCSP.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
ct = mCSP.CreateEncryptor(mCSP.Key, mCSP.IV);
byt = Encoding.UTF8.GetBytes(str);
ms = new MemoryStream();
cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
cs.Write(byt, 0, byt.Length);
cs.FlushFinalBlock();
cs.Close();
return Convert.ToBase64String(ms.ToArray());
}
MemoryStream ms;
CryptoStream cs;
byte[] byt;
mCSP.Key = Convert.FromBase64String(sKey);
mCSP.IV = Convert.FromBase64String(sIV);
mCSP.Mode = System.Security.Cryptography.CipherMode.ECB;
mCSP.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
ct = mCSP.CreateEncryptor(mCSP.Key, mCSP.IV);
byt = Encoding.UTF8.GetBytes(str);
ms = new MemoryStream();
cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
cs.Write(byt, 0, byt.Length);
cs.FlushFinalBlock();
cs.Close();
return Convert.ToBase64String(ms.ToArray());
}
///
/// 帶指定金鑰和向量的3DES加密
///
///
///
///
///
public string Encrypt3DES(string str, string sKey, string sIV)
{
SymmetricAlgorithm mCSP = new TripleDESCryptoServiceProvider();
ICryptoTransform. ct;
MemoryStream ms;
CryptoStream cs;
byte[] byt;
mCSP.Key = Convert.FromBase64String(sKey);
mCSP.IV = Convert.FromBase64String(sIV);
mCSP.Mode = System.Security.Cryptography.CipherMode.ECB;
mCSP.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
ct = mCSP.CreateEncryptor(mCSP.Key, mCSP.IV);
byt = Encoding.UTF8.GetBytes(str);
ms = new MemoryStream();
cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
cs.Write(byt, 0, byt.Length);
cs.FlushFinalBlock();
cs.Close();
return Convert.ToBase64String(ms.ToArray());
}
//解密
public string Decrypt3DES(string Value)
{
string sKey = "wyw308";
string sIV = "scf521";
SymmetricAlgorithm mCSP = new TripleDESCryptoServiceProvider();
ICryptoTransform. ct;
MemoryStream ms;
CryptoStream cs;
byte[] byt;
mCSP.Key = Convert.FromBase64String(sKey);
mCSP.IV = Convert.FromBase64String(sIV);
mCSP.Mode = System.Security.Cryptography.CipherMode.ECB;
mCSP.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
ct = mCSP.CreateDecryptor(mCSP.Key, mCSP.IV);
byt = Convert.FromBase64String(Value);
ms = new MemoryStream();
cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
cs.Write(byt, 0, byt.Length);
cs.FlushFinalBlock();
cs.Close();
return Encoding.UTF8.GetString(ms.ToArray());
}
///
/// 帶指定金鑰和向量的3DES解密
///
///
///
///
///
public string Decrypt3DES(string str, string sKey, string sIV)
{
SymmetricAlgorithm mCSP = new TripleDESCryptoServiceProvider();
ICryptoTransform. ct;
MemoryStream ms;
CryptoStream cs;
byte[] byt;
mCSP.Key = Convert.FromBase64String(sKey);
mCSP.IV = Convert.FromBase64String(sIV);
mCSP.Mode = System.Security.Cryptography.CipherMode.ECB;
mCSP.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
ct = mCSP.CreateDecryptor(mCSP.Key, mCSP.IV);
byt = Convert.FromBase64String(str);
ms = new MemoryStream();
cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
cs.Write(byt, 0, byt.Length);
cs.FlushFinalBlock();
cs.Close();
return Encoding.UTF8.GetString(ms.ToArray());
}
#endregion
public string Decrypt3DES(string Value)
{
string sKey = "wyw308";
string sIV = "scf521";
SymmetricAlgorithm mCSP = new TripleDESCryptoServiceProvider();
ICryptoTransform. ct;
MemoryStream ms;
CryptoStream cs;
byte[] byt;
mCSP.Key = Convert.FromBase64String(sKey);
mCSP.IV = Convert.FromBase64String(sIV);
mCSP.Mode = System.Security.Cryptography.CipherMode.ECB;
mCSP.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
ct = mCSP.CreateDecryptor(mCSP.Key, mCSP.IV);
byt = Convert.FromBase64String(Value);
ms = new MemoryStream();
cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
cs.Write(byt, 0, byt.Length);
cs.FlushFinalBlock();
cs.Close();
return Encoding.UTF8.GetString(ms.ToArray());
}
///
/// 帶指定金鑰和向量的3DES解密
///
///
///
///
///
public string Decrypt3DES(string str, string sKey, string sIV)
{
SymmetricAlgorithm mCSP = new TripleDESCryptoServiceProvider();
ICryptoTransform. ct;
MemoryStream ms;
CryptoStream cs;
byte[] byt;
mCSP.Key = Convert.FromBase64String(sKey);
mCSP.IV = Convert.FromBase64String(sIV);
mCSP.Mode = System.Security.Cryptography.CipherMode.ECB;
mCSP.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
ct = mCSP.CreateDecryptor(mCSP.Key, mCSP.IV);
byt = Convert.FromBase64String(str);
ms = new MemoryStream();
cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
cs.Write(byt, 0, byt.Length);
cs.FlushFinalBlock();
cs.Close();
return Encoding.UTF8.GetString(ms.ToArray());
}
#endregion
#region 一個簡單的加密解密方法,只支援英文
public static string EnCryptEnStr(string str) //倒序加1加密
{
byte[] by = new byte[str.Length];
for (int i = 0;
i <= str.Length - 1;
i++)
{
by[i] = (byte)((byte)str[i] + 1);
}
str = "";
for (int i = by.Length - 1;
i >= 0;
i--)
{
str += ((char)by[i]).ToString();
}
return str;
}
public static string DeCryptEnStr(string str) //順序減1解碼
{
byte[] by = new byte[str.Length];
for (int i = 0;
i <= str.Length - 1;
i++)
{
by[i] = (byte)((byte)str[i] - 1);
}
str = "";
for (int i = by.Length - 1;
i >= 0;
i--)
{
str += ((char)by[i]).ToString();
}
return str;
}
#endregion
public static string EnCryptEnStr(string str) //倒序加1加密
{
byte[] by = new byte[str.Length];
for (int i = 0;
i <= str.Length - 1;
i++)
{
by[i] = (byte)((byte)str[i] + 1);
}
str = "";
for (int i = by.Length - 1;
i >= 0;
i--)
{
str += ((char)by[i]).ToString();
}
return str;
}
public static string DeCryptEnStr(string str) //順序減1解碼
{
byte[] by = new byte[str.Length];
for (int i = 0;
i <= str.Length - 1;
i++)
{
by[i] = (byte)((byte)str[i] - 1);
}
str = "";
for (int i = by.Length - 1;
i >= 0;
i--)
{
str += ((char)by[i]).ToString();
}
return str;
}
#endregion
#region 一個簡單的加密解密方法,在上一個的基礎上支援中文
public static string EnCryptCnStr(string str)
{
string htext = ""; // blank text
public static string EnCryptCnStr(string str)
{
string htext = ""; // blank text
for (int i = 0; i < str.Length; i++)
{
htext = htext + (char)(str[i] + 10 - 1 * 2);
}
return htext;
}
{
htext = htext + (char)(str[i] + 10 - 1 * 2);
}
return htext;
}
public static string DeCryptCnStr(string str)
{
string dtext = "";
{
string dtext = "";
for (int i = 0; i < str.Length; i++)
{
dtext = dtext + (char)(str[i] - 10 + 1 * 2);
}
return dtext;
}
#endregion
{
dtext = dtext + (char)(str[i] - 10 + 1 * 2);
}
return dtext;
}
#endregion
#region Url地址編碼解碼
///
/// 編碼Url地址
///
///
///
public static string UrlEncode(string url)
{
byte[] mByte = null;
mByte = System.Text.Encoding.GetEncoding("GB2312").GetBytes(url);
/// 編碼Url地址
///
///
///
public static string UrlEncode(string url)
{
byte[] mByte = null;
mByte = System.Text.Encoding.GetEncoding("GB2312").GetBytes(url);
return System.Web.HttpUtility.UrlEncode(mByte);
}
}
///
/// 解碼Url地址
///
///
///
public static string UrlDecode(string url)
{
return HttpUtility.UrlDecode(url, System.Text.Encoding.GetEncoding("GB2312"));
}
#endregion
/// 解碼Url地址
///
///
///
public static string UrlDecode(string url)
{
return HttpUtility.UrlDecode(url, System.Text.Encoding.GetEncoding("GB2312"));
}
#endregion
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/22392018/viewspace-733413/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Asp.net 加密解密類ASP.NET加密解密
- asp.net中URL引數加密解密過程ASP.NET加密解密
- 加密解密加密解密
- PHP加密解密PHP加密解密
- js加密解密JS加密解密
- Unity 加密解密Unity加密解密
- Java加密解密Java加密解密
- Oracle ----加密解密Oracle加密解密
- AES 加密&解密加密解密
- AES加密解密加密解密
- php mcrypt 加密 解密PHP加密解密
- RSA加密與解密加密解密
- AES CBC 加密解密加密解密
- Javascript 加密解密方法JavaScript加密解密
- iOS中加密、解密iOS加密解密
- MySQL加密和解密MySql加密解密
- 線上加密/解密工具加密解密
- Java加密解密瞭解Java加密解密
- 影像的加密與解密加密解密
- md5加密解密加密解密
- UrlBase64加密解密加密解密
- SQL Server 加密解密策略SQLServer加密解密
- php rsa長文加密解密PHP加密解密
- utf8 加密與解密加密解密
- PHP Base64 加密 & 解密PHP加密解密
- golang AES-CBC 加密解密Golang加密解密
- android加密解密完美教程Android加密解密
- RSA 非對稱加密&解密加密解密
- golang securecookie加密解密cookie值GolangCookie加密解密
- C#DES加密和解密C#加密解密
- iOS加密解密演算法iOS加密解密演算法
- Java中的加密與解密Java加密解密
- php和.net 的加密解密PHP加密解密
- JAVA中AES加密和解密Java加密解密
- 文字加密:常見的密碼加密解密方法加密密碼解密
- 加密解密技術—對稱演算法加密加密解密演算法
- ASP.Net 加密 CookieASP.NET加密Cookie
- 序列密碼的加密與解密密碼加密解密