0%

【C#】透過C#加密

strKey與strIV是用來加密的參數,可以替換任何字元但一定要8個字

 //加密動作
private static string strKey = "toyo1234";  
private static string strIV = "lovecode";

/// <summary>字串編碼</summary>
/// <param name="strSource">原始字串</param>
/// <returns>編碼後的結果字串</returns>
public static string enCrypt(string strSource)
{
   MemoryStream ms = new MemoryStream();
   DESCryptoServiceProvider key = new DESCryptoServiceProvider();  
   CryptoStream encStream = new CryptoStream(ms, key.CreateEncryptor(Encoding.Default.GetBytes(strKey),     Encoding.Default.GetBytes(strIV)), CryptoStreamMode.Write);
   StreamWriter sw = new StreamWriter(encStream);
   sw.WriteLine(strSource);
   sw.Close();
   encStream.Close();
   byte[] buffer = ms.ToArray();         
   ms.Close();
   return Convert.ToBase64String(buffer);   
}

/// <summary>字串解碼</summary>
/// <param name="strSource">加密過的字串</param>
/// <returns>解碼後的結果字串</returns>
public static string deCrypt(string strSource)
{
    MemoryStream ms = new MemoryStream(Convert.FromBase64String(strSource));
    DESCryptoServiceProvider key = new DESCryptoServiceProvider();
    CryptoStream encStream = new CryptoStream(ms, key.CreateDecryptor(Encoding.Default.GetBytes(strKey), Encoding.Default.GetBytes(strIV)), CryptoStreamMode.Read);
    StreamReader sr = new StreamReader(encStream);
    string val = sr.ReadLine();
    sr.Close();
    encStream.Close();
    ms.Close();

    return val;
}