• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
胡卫军
博客园    首页    新随笔    联系   管理    订阅  订阅

加密

调用string mypwd = CommomLayer.MyEncryption.GetMD5Encording(pwd); using System; using System.Collections.Generic; using System.Security.Cryptography; using System.Text;

namespace CommomLayer {     ///   <summary>       ///   MyEncryption 自定义的加密解密类       ///   </summary>       public class MyEncryption        {         ///   <summary>           ///   转换string到Byte数组           ///   </summary>           ///   <param name="paramStr">要转换的字符串</param>           ///   <returns>转换的Byte数组</returns>           public static Byte[] StringToByteArray(String paramStr)         {             return Encoding.UTF8.GetBytes(paramStr);         }

        ///   <summary>           ///   转换Byte数组到字符串           ///   </summary>           ///   <param   name="paramArrayByte">Byte数组</param>           ///   <returns>字符串</returns>           public static string ByteArrayToString(Byte[] paramArrayByte)         {             return Encoding.UTF8.GetString(paramArrayByte);         }

        ///   <summary>           ///   3des加密字符串           ///   </summary>           ///   <param   name="paramString">要加密的字符串</param>           ///   <param   name="paramKey">密钥</param>           ///   <returns>加密后并经base64编码的字符串</returns>           ///   <remarks>静态方法,采用默认ascii编码</remarks>         public static string Encrypt3DES(string paramString, string paramKey)         {             TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider();             MD5CryptoServiceProvider hashMD5 = new MD5CryptoServiceProvider();             DES.Key = hashMD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(paramKey));             DES.Mode = CipherMode.ECB;             ICryptoTransform DESEncrypt = DES.CreateEncryptor();             byte[] Buffer = ASCIIEncoding.ASCII.GetBytes(paramString);             return Convert.ToBase64String(DESEncrypt.TransformFinalBlock(Buffer, 0, Buffer.Length));         }

        ///   <summary>           ///   3des加密字符串           ///   </summary>           ///   <param   name="a_strString">要加密的字符串</param>           ///   <param   name="a_strKey">密钥</param>           ///   <param   name="encoding">编码方式</param>           ///   <returns>加密后并经base63编码的字符串</returns>           ///   <remarks>重载,指定编码方式</remarks>           public static string Encrypt3DES(string a_strString, string a_strKey, Encoding encoding)         {             TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider();             MD5CryptoServiceProvider hashMD5 = new MD5CryptoServiceProvider();             DES.Key = hashMD5.ComputeHash(encoding.GetBytes(a_strKey));             DES.Mode = CipherMode.ECB;             ICryptoTransform DESEncrypt = DES.CreateEncryptor();             byte[] Buffer = encoding.GetBytes(a_strString);             return Convert.ToBase64String(DESEncrypt.TransformFinalBlock(Buffer, 0, Buffer.Length));         }

        ///   <summary>           ///   3des解密字符串           ///   </summary>           ///   <param   name="paramString">要解密的字符串</param>           ///   <param   name="paramKey">密钥</param>           ///   <returns>解密后的字符串</returns>            ///   <remarks>静态方法,采用默认ascii编码</remarks>           public static string Decrypt3DES(string paramString, string paramKey)         {             TripleDESCryptoServiceProvider DES = new                 TripleDESCryptoServiceProvider();             MD5CryptoServiceProvider hashMD5 = new MD5CryptoServiceProvider();             DES.Key = hashMD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(paramKey));             DES.Mode = CipherMode.ECB;             ICryptoTransform DESDecrypt = DES.CreateDecryptor();             string result = "";             try             {                 byte[] Buffer = Convert.FromBase64String(paramString);                 result = ASCIIEncoding.ASCII.GetString(DESDecrypt.TransformFinalBlock(Buffer, 0, Buffer.Length));             }             catch (Exception e)             {                 throw (new Exception("Invalid Key or input string is not a valid base64 string", e));             }

            return result;         }

        ///   <summary>           ///   3des解密字符串           ///   </summary>           ///   <param   name="paramString">要解密的字符串</param>           ///   <param   name="paramKey">密钥</param>           ///   <param   name="paramEncoding">编码方式</param>           ///   <returns>解密后的字符串</returns>           ///   <remarks>静态方法,指定编码方式</remarks>           public static string Decrypt3DES(string paramString, string paramKey, Encoding paramEncoding)         {             TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider();             MD5CryptoServiceProvider hashMD5 = new MD5CryptoServiceProvider();             DES.Key = hashMD5.ComputeHash(paramEncoding.GetBytes(paramKey));             DES.Mode = CipherMode.ECB;             ICryptoTransform DESDecrypt = DES.CreateDecryptor();             string result = "";             try             {                 byte[] Buffer = Convert.FromBase64String(paramString);                 result = paramEncoding.GetString(DESDecrypt.TransformFinalBlock                     (Buffer, 0, Buffer.Length));             }             catch (Exception e)             {                 throw (new Exception("Invalid Key or input string is not a valid base64 string", e));             }             return result;         }

        ///   <summary>           ///   字符串解析函数           ///   </summary>           public static string AnalysisOfString(string paramStr)         {             string result = "";             string s = "";             int i = paramStr.Length;             if (i == 0) return result;             for (int j = 0; j < i; j++)             {                 s = paramStr.Substring(j, 1);                 char s1;                 s1 = s[0];                 int asc = (int)s1;//得到AscII                   result = result + "&#" + asc.ToString() + ";";             }             return result;         }

        #region public static string GetMD5Encording(string str)   md5 方法加密字符串         /// <summary>         /// md5 方法加密密码         /// </summary>         /// <param name="paramStr">密码明文</param>         /// <returns>密码密文</returns>         public static string GetMD5Encording(string paramStr)         {             MD5 md5 = new MD5CryptoServiceProvider();             byte[] _byte = Encoding.Default.GetBytes(paramStr);             byte[] _byteRst = md5.ComputeHash(_byte);             StringBuilder SBuilder = new StringBuilder();             for (int i = 0; i < _byteRst.Length; i++)             {                 SBuilder.Append(_byteRst[i].ToString("x2"));             }             return SBuilder.ToString();         }         #endregion

    } }

posted @ 2014-07-31 09:42  胡卫军  阅读(176)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3