• C# Http Post请求接口 Des加密接口


            private static string key = "VSQAdmin";//密文 加密解密是都需要密文且保持一致
    
            private static string iv = "VSQAdmin";//加密解密时key 、 iv 中传入的key、iv值一致
    
            /// <summary>
            /// 请求方法
            /// </summary>
            /// <param name="info"></param>
            /// <returns></returns>
            public static string Route(ChangpaostartendRequestModel info)
            {
                try
                {
                    string host = string.Empty;
                    host = string.Format("{0}", "接口地址");
                    string errorMessage = string.Empty;
                    if (info == null)
                    {
                        errorMessage = "请求参数错误!";
                    }
                    info.appId = "C0BE88DC14F99A694F7A2AAF971B7C6092B5DDBA69ECFA13";
    
                    string dataJson = JsonConvert.SerializeObject(info, null, new Newtonsoft.Json.JsonSerializerSettings());
                    string datainfo = Des(dataJson);
    
    
                    ServicePointManager.ServerCertificateValidationCallback = (RemoteCertificateValidationCallback)Delegate.Combine(ServicePointManager.ServerCertificateValidationCallback, new RemoteCertificateValidationCallback(ValidateServerCertificate));
                    ServicePointManager.Expect100Continue = false;
                    ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
                    
                    
                    Encoding encoding = Encoding.UTF8;
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(host);
                    request.Method = WebRequestMethods.Http.Post;
                    request.ContentType = "application/json;charset=utf-8";
    
                    byte[] buffer = encoding.GetBytes(datainfo);
                    request.ContentLength = buffer.Length;
                    request.GetRequestStream().Write(buffer, 0, buffer.Length);
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                    StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
    
                    string result = reader.ReadToEnd();
    
                    string testbase = DecryptString(result);
    
                    return testbase;
                }
                catch (Exception e)
                {
                    ExceptionService.Current.Handle(e);
                    return string.Format("Error:{0}!", e.Message);
                }
    
            }
    
    
    
            /// <summary>
            /// Des解密
            /// </summary>
            /// <param name="pToDecrypt"></param>
            /// <returns></returns>
            public  static string DecryptString(string pToDecrypt)
            {
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
                for (int x = 0; x < pToDecrypt.Length / 2; x++)
                {
                    int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
                    inputByteArray[x] = (byte)i;
                }
    
                des.Key = UTF8Encoding.UTF8.GetBytes(key);//***************key与加密时的Key保持一致
                des.IV = UTF8Encoding.UTF8.GetBytes(iv);//*****************skey与加密时的IV保持一致
    
                MemoryStream ms = new MemoryStream();
    
                CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
    
                StringBuilder ret = new StringBuilder();
                return Encoding.UTF8.GetString(ms.ToArray());
            }
            /// <summary>
            /// Des加密
            /// </summary>
            /// <param name="encryptString"></param>
            /// <returns></returns>
            public static string Des(string encryptString)
            {
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
    
                byte[] sor = Encoding.UTF8.GetBytes(encryptString);
                //传入key、iv
                des.Key = UTF8Encoding.UTF8.GetBytes(key);
                des.IV = UTF8Encoding.UTF8.GetBytes(iv);
    
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
    
                cs.Write(sor, 0, sor.Length);
                cs.FlushFinalBlock();
    
                StringBuilder sb = new StringBuilder();
                foreach (byte b in ms.ToArray())
                {
                    sb.AppendFormat("{0:X2}", b);
                }
                return sb.ToString();
            }
    
    
    
            private static bool ValidateServerCertificate(object sender, System.Security.Cryptography.X509Certificates.X509Certificate cert, X509Chain chain, SslPolicyErrors error)
            {
                //为了通过证书验证,总是返回true  
                return true;
            }
    

      

  • 相关阅读:
    案例(2)-- 线程不安全对象(SimpleDateFormat)
    案例(1)-- OOM异常
    jvm--工具
    死锁的产生以及定位死锁
    TCP--粘包拆包,netty的解决方式
    netty--处理器
    AtomicIntegerFieldUpdater和AtomicInteger
    NIO--ByteBuf
    Pipeline
    吴恩达机器学习笔记(四) —— BP神经网络
  • 原文地址:https://www.cnblogs.com/wmm0105/p/12467375.html
Copyright © 2020-2023  润新知