Exemple #1
0
        public static HttpResponseMessage GetResponse(dynamic request, IHeaderDictionary headers,
                                                      HttpMethod httpMethod, PathString path)
        {
            ApiSafeData CriptoSafeData = new ApiSafeData()
            {
                Data      = TripleDESHelper.Encrypt(JsonSerializer.Serialize(request), out string desParameters),
                Des       = RSAHelper.Encrypt(desParameters, Env.RfiPublicKey),
                Signature = RSAHelper.Sign(desParameters, Env.PartnerPrivateKey)
            };

            if (String.IsNullOrEmpty(Env.CertificateFilePath))
            {
                using (var clientHandler = new HttpClientHandler()
                {
                    ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => { return(true); }
                })
                {
                    return(Resend(headers, httpMethod, clientHandler, CriptoSafeData, path));
                }
            }
            else
            {
                using (var clientHandler = new HttpClientHandler
                {
                    ClientCertificateOptions = ClientCertificateOption.Manual,
                    SslProtocols = SslProtocols.Tls12,
                    ClientCertificates = { new X509Certificate2(Env.CertificateFilePath) }
                })
                {
                    return(Resend(headers, httpMethod, clientHandler, CriptoSafeData, path));
                }
            }
        }
Exemple #2
0
        internal SafeData Encrypt_Data <DD>(DD decryted_data) where DD : class
        {
            TripleDESHelper des = new TripleDESHelper();

            using (RSAHelper rsa_partner = new RSAHelper(RSAType.RSA2, Encoding.UTF8, Globals.key_private, apiUser.PublicKey)) {
                response = JsonConvert.SerializeObject(decryted_data);
#if DEBUG
                Console.Out.WriteLine(response);
#endif
                string encryptedStr = des.Encrypt(response);

                string desPrms      = des.GetParameters();
                string desEncrypted = rsa_partner.Encrypt(desPrms);
                string signStr      = rsa_partner.Sign(desPrms);
                bool   signVerify   = false;

                using (RSAHelper rsa = new RSAHelper(RSAType.RSA2, Encoding.UTF8, Globals.key_private, Globals.key_public)) {
                    signVerify = rsa.Verify(desPrms, signStr);
                }

                using (SafeData sd = new SafeData()) {
                    sd.Data      = encryptedStr;
                    sd.Signature = signStr;
                    sd.Des       = desEncrypted;

                    chainLogger.Step(Tool.GetCurrentMethod());
                    return(sd);
                }
            }
        }
Exemple #3
0
        private string EncryptAuthState(AuthOrderDto order, CreateAuthOrderInput input)
        {
            var state       = $"{order.Id}_{input.AuthState}_{DateTime.Now.ToString("fff")}";
            var encryptData = TripleDESHelper.Encrypt(Encoding.UTF8.GetBytes(state), this._appSettings.GetTripleDESKeyData(), null, CipherMode.ECB, PaddingMode.PKCS7);

            return(MD5Helper.ConvertToString(encryptData));
        }
        public Dictionary <string, byte[]> Encrypt(byte[] bytesToEncrypt, RSAParameters publicKeyReceiver)
        {
            Dictionary <string, byte[]> output = new Dictionary <string, byte[]>();
            Dictionary <string, byte[]> tdes   = TripleDESHelper.Encrypt(bytesToEncrypt);

            output.Add("text", tdes["text"]);                                                                     //file 1: het origineel geencrypteerd met triple DES. Het gene wat geencrypteerd wordt is text (uit de parameter van deze functie)
            output.Add("key", RsaHelper.Encryption(tdes["key"], publicKeyReceiver, false));                       //File 2: triple des sleutel encrypteren met de public van andere persoon
            output.Add("hash", RsaHelper.SignData(md5helper.GenerateHash(bytesToEncrypt), RsaHelper.PrivateKey)); // file 3: maak een hash en encrypteer die met eigen privé sleutel
            return(output);
        }
        public void Test_TripleDESHelper()
        {
            string text   = TripleDESHelper.Encrypt(s_input, s_key);
            string result = TripleDESHelper.Decrypt(text, s_key);

            Assert.AreEqual(s_input, result);


            byte[] b1 = s_input.GetBytes();
            byte[] b2 = TripleDESHelper.Encrypt(b1, s_key);
            byte[] b3 = TripleDESHelper.Decrypt(b2, s_key);
            Assert.IsTrue(ByteTestHelper.AreEqual(b1, b3));
        }
Exemple #6
0
        /// <summary>
        /// 序列化
        /// </summary>
        /// <param name="obj">对象</param>
        /// <param name="filename">文件路径</param>
        public virtual bool Save(object obj, string filename)
        {
            if (obj == null)
            {
                return(true);
            }
            FileStream fs = null;

            try
            {
                string data;
                var    elementType   = obj.GetType();
                var    runConfigType = typeof(RunConfig);
                if (elementType != runConfigType && !runConfigType.IsAssignableFrom(elementType))
                {
                    if (RunConfig.Current.EnabledEncrypt)
                    {
                        data = JsonUtils.SerializeObject(obj, Formatting.None);
                        data = TripleDESHelper.Encrypt(data);
                    }
                    else
                    {
                        data = JsonUtils.SerializeObject(obj, Formatting.Indented);
                    }
                }
                else
                {
                    data = JsonUtils.SerializeObject(obj, Formatting.Indented);
                }
                if (string.IsNullOrEmpty(data))
                {
                    return(true);
                }
                var bytes = System.Text.Encoding.UTF8.GetBytes(data);
                fs = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
                fs.Write(bytes, 0, bytes.Length);
            }
            catch (Exception ex)
            {
                Logger.Error(string.Format("JsonFileCacheSerializater.Save Msg:{0} StackTrace:{1} Source:{2} TargetSite:{3}", ex.Message, ex.StackTrace, ex.Source, ex.TargetSite));
                return(false);
            }
            finally
            {
                if (fs != null)
                {
                    fs.Close();
                }
            }
            return(true);
        }
        public void Encrypt_Decrypt_Test()
        {
            var source      = "helloworld";
            var sourceBytes = Encoding.UTF8.GetBytes(source);
            var key         = "MTIzNDU2Nzg4NzY1NDMyMQ=="; //1234567887654321
            var iv          = "MTIzNDU2Nzg5MGFiY2RlZg=="; //1234567890abcdef

            var keyBytes = Convert.FromBase64String(key);
            var ivBytes  = Convert.FromBase64String(iv);

            var encrypted1 = TripleDESHelper.Encrypt(sourceBytes);
            var decrypted1 = Encoding.UTF8.GetString(TripleDESHelper.Decrypt(encrypted1));

            Assert.Equal(source, decrypted1);
        }
 /// <summary>
 /// 保存(序列化)指定路径下的配置文件
 /// </summary>
 /// <param name="configFilePath">指定的配置文件所在的路径(包括文件名)</param>
 /// <param name="configinfo">被保存(序列化)的对象</param>
 /// <returns></returns>
 public bool SaveConfig(string configFilePath, IConfigInfo configinfo)
 {
     if (ShouldEncrypt)
     {
         EncryptContent content   = new EncryptContent();
         var            configstr = SerializationHelper.Serialize(configinfo);
         //需要额外加密
         content.Content = TripleDESHelper.Encrypt(configstr);
         SerializationHelper.Save(content, configFilePath);
     }
     else
     {
         return(SerializationHelper.Save(configinfo, configFilePath));
     }
     return(true);
 }
Exemple #9
0
 public string Encrypt(string textToEncrypt)
 {
     return(tripleDES.Encrypt(textToEncrypt));
 }