ASP.NET 암호 화 복호화 알고리즘 공유
#region DES
/// <summary>
/// DES
/// </summary>
/// <param name="strSource"> </param>
/// <param name="key">32 Key </param>
/// <returns> </returns>
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());
}
/// <summary>
/// DES
/// </summary>
/// <param name="strSource"> </param>
/// <param name="key">32 Key </param>
/// <returns> </returns>
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
/// <summary>
///
/// </summary>
/// <param name="src"></param>
/// <returns></returns>
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);
}
/// <summary>
///
/// </summary>
/// <param name="src"></param>
/// <returns></returns>
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();
}
/// <summary>
/// , >16, 16 , 16,
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
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;
}
//// <summary>
/// MD5 32
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
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();
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());
}
/// <summary>
/// 3DES
/// </summary>
/// <param name="str"></param>
/// <param name="sKey"></param>
/// <param name="sIV"></param>
/// <returns></returns>
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());
}
/// <summary>
/// 3DES
/// </summary>
/// <param name="Value"></param>
/// <param name="sKey"></param>
/// <param name="sIV"></param>
/// <returns></returns>
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
#region ,
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;
}
public static string DeCryptCnStr(string str)
{
string dtext = "";
for (int i = 0; i < str.Length; i++)
{
dtext = dtext + (char)(str[i] - 10 + 1 * 2);
}
return dtext;
}
#endregion
#region Url
/// <summary>
/// Url
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
public static string UrlEncode(string url)
{
byte[] mByte = null;
mByte = System.Text.Encoding.GetEncoding("GB2312").GetBytes(url);
return System.Web.HttpUtility.UrlEncode(mByte);
}
/// <summary>
/// Url
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
public static string UrlDecode(string url)
{
return HttpUtility.UrlDecode(url, System.Text.Encoding.GetEncoding("GB2312"));
}
#endregion
이상 에서 말 한 것 이 바로 본문의 전체 내용 이 니 여러분 들 이 좋아 하 시 기 를 바 랍 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Linux 의 가상 시디롬 과 가상 플 로 피만약 당신 이 방금 윈도 에서 리 눅 스 로 옮 겼 다 면, 윈도 아래 에 가상 드라이브 가 있 고, 리 눅 스 아래 에 있 는 지 물 어 봐 야 할 것 입 니 다.일반적으로 윈도 가 할 수 있 는 일 은 리 눅 스...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.