示例#1
0
        /// <summary>
        /// DES encrypt
        /// </summary>
        /// <param name="data">Raw data</param>
        /// <param name="key">Key, requires 24 bits</param>
        /// <returns>Encrypted byte array</returns>
        public static byte[] DESEncrypt(byte[] data, string key = "TThvj8G5jb6GcTFZmxVwh5Cj")
        {
            Check.Argument.IsNotEmpty(data, nameof(data));
            Check.Argument.IsNotEmpty(key, nameof(key));
            Check.Argument.IsNotOutOfRange(key.Length, 24, 24, nameof(key));

            using (MemoryStream Memory = new MemoryStream())
            {
                using (TripleDES des = TripleDES.Create())
                {
                    Byte[] plainBytes = data;
                    Byte[] bKey       = new Byte[24];
                    Array.Copy(Encoding.UTF8.GetBytes(key.PadRight(bKey.Length)), bKey, bKey.Length);

                    des.Mode    = CipherMode.ECB;
                    des.Padding = PaddingMode.PKCS7;
                    des.Key     = bKey;
                    using (CryptoStream cryptoStream = new CryptoStream(Memory, des.CreateEncryptor(), CryptoStreamMode.Write))
                    {
                        try
                        {
                            cryptoStream.Write(plainBytes, 0, plainBytes.Length);
                            cryptoStream.FlushFinalBlock();
                            return(Memory.ToArray());
                        }
                        catch (Exception ex)
                        {
                            LogerHelp.Error(ex);
                            return(null);
                        }
                    }
                }
            }
        }
示例#2
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="email"></param>
 /// <param name="code"></param>
 /// <returns></returns>
 public bool VerifByEmail(string code)
 {
     try
     {
         var        token      = SecurityHelper.AES256DEncrypt(code)?.DeserializeObject <TokenModel>();
         TokenModel tokenmodel = session.GetObjectFromJson <TokenModel>(nameof(GenerateEmailAsync));
         if (token == null)
         {
             return(false);
         }
         var uip = context.Request.GetUserIp();
         if (uip.IsNotNullOrEmpty())
         {
             if (!token.RequestIp.Equals(uip))
             {
                 return(false);
             }
         }
         return(tokenmodel.Name == token.Name && token.Sid == tokenmodel.Sid && DateTime.Now < token.ResponseTime.AddMinutes(token.ExpiryTime));
     }
     catch (Exception ex)
     {
         LogerHelp.Error(ex);
         return(false);
     }
 }
示例#3
0
        public static async void SendHealthEmailAsync(EmailSettings settings, string content)
        {
            try
            {
                dynamic list = JsonConvert.DeserializeObject(content);
                if (list != null && list.Count > 0)
                {
                    var emailBody = new StringBuilder("健康检查故障:\r\n");
                    foreach (var noticy in list)
                    {
                        emailBody.AppendLine($"--------------------------------------");
                        emailBody.AppendLine($"Node:{noticy.Node}");
                        emailBody.AppendLine($"Service ID:{noticy.ServiceID}");
                        emailBody.AppendLine($"Service Name:{noticy.ServiceName}");
                        emailBody.AppendLine($"Check ID:{noticy.CheckID}");
                        emailBody.AppendLine($"Check Name:{noticy.Name}");
                        emailBody.AppendLine($"Check Status:{noticy.Status}");
                        emailBody.AppendLine($"Check Output:{noticy.Output}");
                        emailBody.AppendLine($"--------------------------------------");
                    }

                    var message = new MimeMessage();
                    message.From.Add(new MailboxAddress(settings.FromWho, settings.FromAccount));
                    message.To.Add(new MailboxAddress(settings.ToWho, settings.ToAccount));

                    message.Subject = settings.Subject;
                    message.Body    = new TextPart("plain")
                    {
                        Text = emailBody.ToString()
                    };
                    using (var client = new SmtpClient())
                    {
                        client.ServerCertificateValidationCallback = (s, c, h, e) => true;
                        await client.ConnectAsync(settings.SmtpServer, settings.SmtpPort, false);

                        client.AuthenticationMechanisms.Remove("XOAUTH2");
                        await client.AuthenticateAsync(settings.AuthAccount, settings.AuthPassword);

                        await client.SendAsync(message);

                        await client.DisconnectAsync(true);
                    }
                }
            }
            catch (Exception ex)
            {
                LogerHelp.Error(ex);
                Console.WriteLine(ex.Message);
            }
        }
示例#4
0
        /// <summary>
        /// AES decrypt( no IV)
        /// </summary>
        /// <param name="data">Encrypted data</param>
        /// <param name="key">Key, requires 32 bits</param>
        /// <returns>Decrypted string</returns>
        public static string AESDecrypt(string data, string key)
        {
            Check.Argument.IsNotEmpty(data, nameof(data));
            Check.Argument.IsNotEmpty(key, nameof(key));
            Check.Argument.IsNotOutOfRange(key.Length, 32, 32, nameof(key));

            Byte[] encryptedBytes = Convert.FromBase64String(data);
            Byte[] bKey           = new Byte[32];
            Array.Copy(Encoding.UTF8.GetBytes(key.PadRight(bKey.Length)), bKey, bKey.Length);

            using (MemoryStream Memory = new MemoryStream(encryptedBytes))
            {
                //mStream.Write( encryptedBytes, 0, encryptedBytes.Length );
                //mStream.Seek( 0, SeekOrigin.Begin );
                using (Aes aes = Aes.Create())
                {
                    aes.Mode    = CipherMode.ECB;
                    aes.Padding = PaddingMode.PKCS7;
                    aes.KeySize = 128;
                    aes.Key     = bKey;
                    //aes.IV = _iV;
                    using (CryptoStream cryptoStream = new CryptoStream(Memory, aes.CreateDecryptor(), CryptoStreamMode.Read))
                    {
                        try
                        {
                            byte[] tmp = new byte[encryptedBytes.Length];
                            int    len = cryptoStream.Read(tmp, 0, encryptedBytes.Length);
                            byte[] ret = new byte[len];
                            Array.Copy(tmp, 0, ret, 0, len);
                            return(Encoding.UTF8.GetString(ret));
                        }
                        catch (Exception ex)
                        {
                            LogerHelp.Error(ex);
                            return(null);
                        }
                    }
                }
            }
        }
示例#5
0
        protected async Task <T> GetCacheAsync <T>(string key, CancellationToken cancellationToken = default)
        {
            if (string.IsNullOrEmpty(key))
            {
                throw new ArgumentNullEx(nameof(key), "key Is Null Or Empty");
            }
            var cacheValue = await cache.GetStringAsync(key);

            if (!string.IsNullOrEmpty(cacheValue))
            {
                try
                {
                    return(JsonConvert.DeserializeObject <T>(cacheValue));
                }
                catch (Exception ex)
                {
                    LogerHelp.Error(ex);
                    //出错时删除key
                    cache.Remove(key);
                }
            }
            return(default);
示例#6
0
        /// <summary>
        /// AES encrypt ( no IV)
        /// </summary>
        /// <param name="data">Raw data</param>
        /// <param name="key">Key, requires 32 bits</param>
        /// <returns>Encrypted string</returns>
        public static string AESEncrypt(string data, string key)
        {
            Check.Argument.IsNotEmpty(data, nameof(data));

            Check.Argument.IsNotEmpty(key, nameof(key));
            Check.Argument.IsNotOutOfRange(key.Length, 32, 32, nameof(key));

            using (MemoryStream Memory = new MemoryStream())
            {
                using (Aes aes = Aes.Create())
                {
                    byte[] plainBytes = Encoding.UTF8.GetBytes(data);
                    Byte[] bKey       = new Byte[32];
                    Array.Copy(Encoding.UTF8.GetBytes(key.PadRight(bKey.Length)), bKey, bKey.Length);

                    aes.Mode    = CipherMode.ECB;
                    aes.Padding = PaddingMode.PKCS7;
                    aes.KeySize = 128;
                    //aes.Key = _key;
                    aes.Key = bKey;
                    //aes.IV = _iV;
                    using (CryptoStream cryptoStream = new CryptoStream(Memory, aes.CreateEncryptor(), CryptoStreamMode.Write))
                    {
                        try
                        {
                            cryptoStream.Write(plainBytes, 0, plainBytes.Length);
                            cryptoStream.FlushFinalBlock();
                            return(Convert.ToBase64String(Memory.ToArray()));
                        }
                        catch (Exception ex)
                        {
                            LogerHelp.Error(ex);
                            return(null);
                        }
                    }
                }
            }
        }
示例#7
0
        /// <summary>
        /// DES decrypt
        /// </summary>
        /// <param name="data">Encrypted data</param>
        /// <param name="key">Key, requires 24 bits</param>
        /// <returns>Decrypted byte array</returns>
        public static byte[] DESDecrypt(byte[] data, string key = "TThvj8G5jb6GcTFZmxVwh5Cj")
        {
            Check.Argument.IsNotEmpty(data, nameof(data));
            Check.Argument.IsNotEmpty(key, nameof(key));
            Check.Argument.IsNotOutOfRange(key.Length, 24, 24, nameof(key));

            Byte[] encryptedBytes = data;
            Byte[] bKey           = new Byte[24];
            Array.Copy(Encoding.UTF8.GetBytes(key.PadRight(bKey.Length)), bKey, bKey.Length);

            using (MemoryStream Memory = new MemoryStream(encryptedBytes))
            {
                using (TripleDES des = TripleDES.Create())
                {
                    des.Mode    = CipherMode.ECB;
                    des.Padding = PaddingMode.PKCS7;
                    des.Key     = bKey;
                    using (CryptoStream cryptoStream = new CryptoStream(Memory, des.CreateDecryptor(), CryptoStreamMode.Read))
                    {
                        try
                        {
                            byte[] tmp = new byte[encryptedBytes.Length];
                            int    len = cryptoStream.Read(tmp, 0, encryptedBytes.Length);
                            byte[] ret = new byte[len];
                            Array.Copy(tmp, 0, ret, 0, len);
                            return(ret);
                        }
                        catch (Exception ex)
                        {
                            LogerHelp.Error(ex);
                            return(data);
                        }
                    }
                }
            }
        }