zoukankan      html  css  js  c++  java
  • 简单的加密解密

    加密解密随记

            private void demo(HttpContext context)
            {
                string key = "xxxxx";//key值
    string body="xxxx";
    try { //body加密 string body = Decrypt.Encrypt3DESToBase64(body, key); //sign加密 string sign = Decrypt.sign("xxxx"); string str = "{ "sign": "" + sign + "", "body": "" + body +"" }"; string paraUrlCoded = System.Web.HttpUtility.UrlEncode("json"); paraUrlCoded += "=" + System.Web.HttpUtility.UrlEncode(str); // 第三方接口获取数据 //拼接URL string serviceUrl = "xxxxxx";//第三方接口 HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(serviceUrl); //post请求 myRequest.Method = "POST"; //utf-8编码 byte[] buf = System.Text.Encoding.GetEncoding("UTF-8").GetBytes(paraUrlCoded); myRequest.ContentLength = buf.Length; myRequest.Timeout = 5000; //指定为json否则会出错 myRequest.ContentType = "application/x-www-form-urlencoded"; myRequest.MaximumAutomaticRedirections = 1; myRequest.AllowAutoRedirect = true; Stream newStream = myRequest.GetRequestStream(); newStream.Write(buf, 0, buf.Length); newStream.Close(); //获得接口返回值 HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse(); StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8); string strResult = reader.ReadToEnd(); reader.Close(); myResponse.Close(); JObject jo = (JObject)JsonConvert.DeserializeObject(strResult); string zone = jo["body"].ToString(); //body解密 string bodyDecrypt = Encrypt.Decrypt3DESFromBase64(zone, key); context.Response.Write(bodyDecrypt); return; } catch (Exception ex) { string str = "{ " + ex + ",失败 }"; context.Response.Write(str); return; } }
     public class Decrypt
        {
            public static string Encrypt3DESToBase64(string strEncrypt, string strKey)
            {
                return ToBase64(Encrypt3DES(Encoding.UTF8.GetBytes(strEncrypt), strKey));
            }
    
            public static byte[] Encrypt3DES(byte[] arrEncrypt, string strKey)
            {
                ICryptoTransform DESEncrypt = null;
                try
                {
                    TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider();
    
                    DES.Key = ASCIIEncoding.ASCII.GetBytes(strKey);
                    DES.Mode = CipherMode.ECB;
    
                    DESEncrypt = DES.CreateEncryptor();
                }
                catch (System.Exception e)
                {
                    return null;
                }
    
                return DESEncrypt.TransformFinalBlock(arrEncrypt, 0, arrEncrypt.Length);
            }
    
            public static string ToBase64(byte[] str)
            {
                return Convert.ToBase64String(str);
            }
    
    
        }
    

      

    public class Encrypt
        {
    
            public static string Decrypt3DESFromBase64(string strDecrypt, string strKey)
            {
                return Encoding.UTF8.GetString(Decrypt3DES(FromBase64(strDecrypt), strKey));
            }
            public static byte[] FromBase64(string str)
            {
                return Convert.FromBase64String(str);
            }
    
            public static byte[] Decrypt3DES(byte[] arrDecrypt, string strKey)
            {
                ICryptoTransform DESDecrypt = null;
                try
                {
                    TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider();
    
                    DES.Key = ASCIIEncoding.ASCII.GetBytes(strKey);
                    DES.Mode = CipherMode.ECB;
                    DES.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
    
                    DESDecrypt = DES.CreateDecryptor();
                }
                catch (System.Exception e)
                {
                    return null;
                }
    
                return DESDecrypt.TransformFinalBlock(arrDecrypt, 0, arrDecrypt.Length);
            }
    
    
    
        }
    

      

  • 相关阅读:
    EasyUI
    EasyUI
    EasyUI
    django MTV架构下的网站开发步骤
    Centos7下安装python3
    python 线程间事件通知
    hadoop MapReduce
    hadoop文件配置
    DataFrame 数据去重
    用selenium获取cookies
  • 原文地址:https://www.cnblogs.com/Pinapple/p/9356128.html
Copyright © 2011-2022 走看看