DES对称加解密、AES RijndaelManaged加解密、Base64加密解密、MD5加密等操作辅助类 EncodeHelper

/// <summary>

/// 使用默认加密

/// </summary>

/// <param name="strText"></param>

/// <returns></returns>

public static string DesEncrypt(string strText)

 

/// <summary>

/// 使用默认解密

/// </summary>

/// <param name="strText">解密字符串</param>

/// <returns></returns>

public static string DesDecrypt(string strText)

 

/// <summary>

/// 加密字符串,注意strEncrKey的长度为8位

/// </summary>

/// <param name="strText">待加密字符串</param>

/// <param name="strEncrKey">加密键</param>

/// <returns></returns>

public static string DesEncrypt(string strText, string strEncrKey)

 

/// <summary>

/// 解密字符串,注意strEncrKey的长度为8位

/// </summary>

/// <param name="strText">待解密的字符串</param>

/// <param name="sDecrKey">解密键</param>

/// <returns>解密后的字符串</returns>

public static string DesDecrypt(string strText, string sDecrKey)

 

/// <summary>

/// 加密数据文件,注意strEncrKey的长度为8位

/// </summary>

/// <param name="m_InFilePath">待加密的文件路径</param>

/// <param name="m_OutFilePath">输出文件路径</param>

/// <param name="strEncrKey">加密键</param>

public static void DesEncrypt(string m_InFilePath, string m_OutFilePath, string strEncrKey)

 

/// <summary>

/// 解密数据文件,注意strEncrKey的长度为8位

/// </summary>

/// <param name="m_InFilePath">待解密的文件路径</param>

/// <param name="m_OutFilePath">输出路径</param>

/// <param name="sDecrKey">解密键</param>

public static void DesDecrypt(string m_InFilePath, string m_OutFilePath, string sDecrKey)

 

#endregion

 

#region 对称加密算法AES RijndaelManaged加密解密

 

/// <summary>

/// 对称加密算法AES RijndaelManaged加密(RijndaelManaged(AES)算法是块式加密算法)

/// </summary>

/// <param name="encryptString">待加密字符串</param>

/// <returns>加密结果字符串</returns>

public static string AES_Encrypt(string encryptString)

 

/// <summary>

/// 对称加密算法AES RijndaelManaged加密(RijndaelManaged(AES)算法是块式加密算法)

/// </summary>

/// <param name="encryptString">待加密字符串</param>

/// <param name="encryptKey">加密密钥,须半角字符</param>

/// <returns>加密结果字符串</returns>

public static string AES_Encrypt(string encryptString, string encryptKey)

 

/// <summary>

/// 对称加密算法AES RijndaelManaged解密字符串

/// </summary>

/// <param name="decryptString">待解密的字符串</param>

/// <returns>解密成功返回解密后的字符串,失败返源串</returns>

public static string AES_Decrypt(string decryptString)

 

/// <summary>

/// 对称加密算法AES RijndaelManaged解密字符串

/// </summary>

/// <param name="decryptString">待解密的字符串</param>

/// <param name="decryptKey">解密密钥,和加密密钥相同</param>

/// <returns>解密成功返回解密后的字符串,失败返回空</returns>

public static string AES_Decrypt(string decryptString, string decryptKey)

 

 

/// <summary>

/// 加密文件流

/// </summary>

/// <param name="fs">文件流</param>

/// <returns></returns>

public static CryptoStream AES_EncryptStrream(FileStream fs, string decryptKey)

 

/// <summary>

/// 解密文件流

/// </summary>

/// <param name="fs">文件流</param>

/// <returns></returns>

public static CryptoStream AES_DecryptStream(FileStream fs, string decryptKey)

 

/// <summary>

/// 对指定文件加密

/// </summary>

/// <param name="InputFile">输入文件</param>

/// <param name="OutputFile">输出文件</param>

/// <returns></returns>

public static bool AES_EncryptFile(string InputFile, string OutputFile)

 

/// <summary>

/// 对指定的文件解压缩

/// </summary>

/// <param name="InputFile">输入文件</param>

/// <param name="OutputFile">输出文件</param>

/// <returns></returns>

public static bool AES_DecryptFile(string InputFile, string OutputFile)

 

#endregion

 

#region Base64加密解密

/// <summary>

/// Base64是一種使用64基的位置計數法。它使用2的最大次方來代表僅可列印的ASCII 字元。

/// 這使它可用來作為電子郵件的傳輸編碼。在Base64中的變數使用字元A-Z、a-z和0-9 ,

/// 這樣共有62個字元,用來作為開始的64個數字,最後兩個用來作為數字的符號在不同的

/// 系統中而不同。

/// Base64加密

/// </summary>

/// <param name="str">Base64方式加密字符串</param>

/// <returns></returns>

public static string Base64Encrypt(string str)

 

/// <summary>

/// Base64解密字符串

/// </summary>

/// <param name="str">待解密的字符串</param>

/// <returns></returns>

public static string Base64Decrypt(string str)

 

#endregion

 

#region MD5加密

 

/// <summary>

/// 使用MD5加密字符串

/// </summary>

/// <param name="strText">待加密的字符串</param>

/// <returns>MD5加密后的字符串</returns>

public static string MD5Encrypt(string strText)

 

/// <summary>

/// 使用MD5加密的Hash表

/// </summary>

/// <param name="input">待加密字符串</param>

/// <returns></returns>

public static string MD5EncryptHash(String input)

 

/// <summary>

/// 使用Md5加密为16进制字符串

/// </summary>

/// <param name="input">待加密字符串</param>

/// <returns></returns>

public static string MD5EncryptHashHex(String input)

 

/// <summary>

/// MD5 三次加密算法.计算过程: (QQ使用)

/// 1. 验证码转为大写

/// 2. 将密码使用这个方法进行三次加密后,与验证码进行叠加

/// 3. 然后将叠加后的内容再次MD5一下,得到最终验证码的值

/// </summary>

/// <param name="s"></param>

/// <returns></returns>

public static string EncyptMD5_3_16(string s)

 

#endregion

 

/// <summary>

/// SHA256函数

/// </summary>

/// <param name="str">原始字符串</param>

/// <returns>SHA256结果(返回长度为44字节的字符串)</returns>

public static string SHA256(string str)

 

/// <summary>

/// 加密字符串(使用MD5+Base64混合加密)

/// </summary>

/// <param name="input">待加密的字符串</param>

/// <returns></returns>

public static string EncryptString(string input)

 

/// <summary>

/// 解密加过密的字符串(使用MD5+Base64混合加密)

/// </summary>

/// <param name="input">待解密的字符串</param>

/// <param name="throwException">解密失败是否抛异常</param>

/// <returns></returns>

public static string DecryptString(string input, bool throwException)

 

 

、、、

/// <summary>

/// 对字符串加密

/// </summary>

/// <returns></returns>

private string EncodePassword(string passwordText)

{

return EncodeHelper.MD5Encrypt(passwordText);
}

 

 

、、、

private void btnEncrypt_Click(object sender, EventArgs e)

{

string original = "测试加密字符串";

Console.WriteLine("original:" + original);

 

string encrypt = EncodeHelper.SHA256(original);

Console.WriteLine("EncodeHelper.SHA256" + encrypt);

 

//DES加解密

encrypt = EncodeHelper.DesEncrypt(original);

Console.WriteLine("EncodeHelper.DesEncrypt:" + encrypt);

 

string decrypt = EncodeHelper.DesDecrypt(encrypt);

Console.WriteLine("EncodeHelper.DesDecrypt:" + decrypt);

 

//MD5加密

encrypt = EncodeHelper.MD5Encrypt(original);

Console.WriteLine("EncodeHelper.MD5Encrypt:" + encrypt);

encrypt = EncodeHelper.MD5EncryptHash(original);

Console.WriteLine("EncodeHelper.MD5EncryptHash:" + encrypt);

encrypt = EncodeHelper.MD5EncryptHashHex(original);

Console.WriteLine("EncodeHelper.MD5EncryptHashHex" + encrypt);

encrypt = EncodeHelper.EncyptMD5_3_16(original);

Console.WriteLine("EncodeHelper.EncyptMD5_3_16:" + encrypt);

 

//Base64加解密

encrypt = EncodeHelper.Base64Encrypt(original);

Console.WriteLine("EncodeHelper.Base64Encrypt" + encrypt);

decrypt = EncodeHelper.Base64Encrypt(encrypt);

Console.WriteLine("EncodeHelper.Base64Encrypt" + decrypt);

 

encrypt = EncodeHelper.AES_Encrypt(original);

Console.WriteLine("EncodeHelper.AES_Encrypt" + encrypt);

decrypt = EncodeHelper.AES_Decrypt(encrypt);
Console.WriteLine("EncodeHelper.AES_Decrypt" + decrypt);

、、、、、

posted @ 2015-07-22 18:25  applekingghfhfhbr  阅读(687)  评论(0编辑  收藏  举报