zoukankan      html  css  js  c++  java
  • 2、C# 编码/加密工具集

    1、Base64概念

    1.标准base64只有64个字符(英文大小写、数字和+、/)以及用作后缀等号;
    2.base64是把3个字节变成4个可打印字符,所以base64编码后的字符串一定能被4整除(不算用作后缀的等号);
    3.等号一定用作后缀,且数目一定是0个、1个或2个。这是因为如果原文长度不能被3整除,base64要在后面添加凑齐3n位。为了正确还原,添加了几个就加上几个等号,
    显然添加等号的数目只能是0、1或2; 4.严格来说base64不能算是一种加密,只能说是编码转换。使用base64的初衷。是为了方便把含有不可见字符串的信息用可见字符串表示出来,以便复制粘贴。 作者:郭无心 链接:https://www.zhihu.com/question/36306744/answer/71626823 来源:知乎 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
    https://www.zhihu.com/question/36306744/answer/71626823

    (1)编码

    //Base64编码
        public static string EncodeBase64(string code_type, string code)
        {
            string encode = "";
            byte[] bytes = Encoding.GetEncoding(code_type).GetBytes(code);
            try
            {
                encode = Convert.ToBase64String(bytes);
            }
            catch
            {
                encode = code;
            }
            return encode + "";
        }

    (2)解码

     //Base64解码
        public static string DecodeBase64(string code_type, string code)
        {
            if (code == "AA==") return "";
            string decode = "";
            byte[] bytes = Convert.FromBase64String(code);
            try
            {
                decode = Encoding.GetEncoding(code_type).GetString(bytes);
            }
            catch
            {
                decode = code;
            }
            return decode + "";
        }

    2、MD5

    MD5的全称是Message-Digest Algorithm 5(信息-摘要算法),经MD2、MD3和MD4发展而来。MD5算法的使用不需要支付任何版权费用。

    (1)加密

    string pwd = FormsAuthentication.HashPasswordForStoringInConfigFile(user_psw, "MD5");
    //返回16或32位编码
    public
    static string MD5(string str, int code) { if (code == 16) { return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5").ToLower().Substring(8, 16); } if (code == 32) { return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5"); } return "00000000000000000000000000000000"; }

      

  • 相关阅读:
    list1AndCompile
    pickle序列化2
    Servlet简单例子:输出当前日期
    Servlet简单例子:前后端通信
    Java 继承的简单例子
    Java 一个简单的距离工具类
    Java 私有静态成员变量的简单使用
    mysql数据类型
    使用Bootstrap简单案例——导航条+轮播图+模态框+表单
    K-means 聚类
  • 原文地址:https://www.cnblogs.com/su1643/p/6648141.html
Copyright © 2011-2022 走看看