C#常用字符串加密解密方法封装代码

论坛 期权论坛 脚本     
niminba   2021-5-23 02:58   907   0

复制代码 代码如下:

//方法一
//须添加对System.Web的引用
//using System.Web.Security;
/// <summary>
/// SHA1加密字符串
/// </summary>
/// <param name="source">源字符串</param>
/// <returns>加密后的字符串</returns>
public string SHA1(string source)
{
    return FormsAuthentication.HashPasswordForStoringInConfigFile(source, "SHA1");
}
/// <summary>
/// MD5加密字符串
/// </summary>
/// <param name="source">源字符串</param>
/// <returns>加密后的字符串</returns>
public string MD5(string source)
{
    return FormsAuthentication.HashPasswordForStoringInConfigFile(source, "MD5");;
}


//方法二(可逆加密解密):
//using System.Security.Cryptography;
public string Encode(string data)
{
    byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64);
    byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64);
    DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
    int i = cryptoProvider.KeySize;
    MemoryStream ms = new MemoryStream();
    CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateEncryptor(byKey, byIV), CryptoStreamMode.Write);
    StreamWriter sw = new StreamWriter(cst);
    sw.Write(data);
    sw.Flush();
    cst.FlushFinalBlock();
    sw.Flush();
    return Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length);
}
public string Decode(string data)
{
    byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64);
    byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64);
    byte[] byEnc;
    try
    {
        byEnc = Convert.FromBase64String(data);
    }
    catch
    {
        return null;
    }
    DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
    MemoryStream ms = new MemoryStream(byEnc);
    CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateDecryptor(byKey, byIV), CryptoStreamMode.Read);
    StreamReader sr = new StreamReader(cst);


//方法三(MD5不可逆):
//using System.Security.Cryptography;
//MD5不可逆加密
//32位加密
public string GetMD5_32(string s, string _input_charset)
{
    MD5 md5 = new MD5CryptoServiceProvider();
    byte[] t = md5.ComputeHash(Encoding.GetEncoding(_input_charset).GetBytes(s));
    StringBuilder sb = new StringBuilder(32);
    for (int i = 0; i < t.Length; i++)
    {
        sb.Append(t[i].ToString("x").PadLeft(2, '0'));
    }
    return sb.ToString();
}
//16位加密
public static string GetMd5_16(string ConvertString)
{
    MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
    string t2 = BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(ConvertString)), 4, 8);
    t2 = t2.Replace("-", "");
    return t2;
}


//方法四(对称加密):
//using System.IO;
//using System.Security.Cryptography;
private SymmetricAlgorithm mobjCryptoService;
private string Key;
/// <summary>  
/// 对称加密类的构造函数  
/// </summary>  
public SymmetricMethod()
{
    mobjCryptoService = new RijndaelManaged();
    Key = "Guz(%&hj7x89H$yuBI0456FtmaT5&fvHUFCy76*h%(HilJ$lhj!y6&(*jkP87jH7";
}
/// <summary>  
/// 获得密钥  
/// </summary>  
/// <returns>密钥</returns>  
private byte[] GetLegalKey()
{
    string sTemp = Key;
    mobjCryptoService.GenerateKey();
    byte[] bytTemp = mobjCryptoService.Key;
    int KeyLength = bytTemp.Length;
    if (sTemp.Length > KeyLength)
        sTemp = sTemp.Substring(0, KeyLength);
    else if (sTemp.Length < KeyLength)
        sTemp = sTemp.PadRight(KeyLength, ' ');
    return ASCIIEncoding.ASCII.GetBytes(sTemp);
}
/// <summary>  
/// 获得初始向量IV  
/// </summary>  
/// <returns>初试向量IV</returns>  
private byte[] GetLegalIV()
{
    string sTemp = "E4ghj*Ghg7!rNIfb&95GUY86GfghUb#er57HBh(u%g6HJ($jhWk7&!hg4ui%$hjk";
    mobjCryptoService.GenerateIV();
    byte[] bytTe[[YK[[YK]V\^K]V\OX]HH[HX[\[HH[][][\[TX[H[H][TX[J[[YK[S[K[[PX\XY N[TX[HH][TX[J[YK[S[K[X]K[PX\]JN [ NX]H\XX\[XY[]K]V[H]]V N\\[\YYX]HYH[\[[H \\H[\]\][[H[[\\H[[H[][K[[\\H\]\H][]H[YKT\H]T\\XY\ N\X[H[X[HH]\X[J \X]Q[\\^K\K\X[S[K]JNXYH[][K[[\[]HH][K[H [ [O[H[XY [  L N[X[K]J[ [N[H[ [O[X[KJ N J N[J NO)]]H]XX\]J[[[YK[[YK]V\^K]V\OX]HH[HX[\[HH[][][\[TX[H[H][TX[J[[YK[S[K[[PX\XY N[TX[HH][TX[J[YK[S[K[X]K[PX\]JN [ NX]H\XX\[XY[]K]V[H]]V N\\[\YYX]HYH[\[[H \\H[\]\][[H[[\\H[[H[][K[[\\H\]\H][]H[YKT\H]T\\XY\ N\X[H[X[HH]\X[J \X]QX\\^K\K\X[S[K]JNXYH[][K[[\[]HH][K[H [ [O[H[XY [  L N[X[K]J[ [N[H[ [O[X[KJ N J N[J N]\XY NO ]

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:1060120
帖子:212021
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP