/// <summary>
        /// 加密
        /// </summary>
        /// <param name="value">待加密的值</param>
        /// <param name="key">密钥</param>
        /// <param name="iv">加密偏移量</param>
        /// <param name="salt">加盐</param>
        /// <param name="outType">输出类型,默认为<see cref="OutType.Base64"/></param>
        /// <param name="encoding">编码类型,默认为<see cref="Encoding.UTF8"/></param>
        /// <returns></returns>
        public static string Encrypt(string value, string key, string iv = null, string salt = null,
                                     OutType outType   = OutType.Base64,
                                     Encoding encoding = null)
        {
            if (string.IsNullOrEmpty(value))
            {
                throw new ArgumentNullException(nameof(value));
            }

            if (string.IsNullOrEmpty(key))
            {
                throw new ArgumentNullException(nameof(key));
            }

            if (encoding == null)
            {
                encoding = Encoding.UTF8;
            }

            var result = EncryptCore <AesCryptoServiceProvider>(encoding.GetBytes(value),
                                                                ComputeRealValueFunc()(key)(salt)(encoding)(64),
                                                                ComputeRealValueFunc()(iv)(salt)(encoding)(64));

            if (outType == OutType.Base64)
            {
                return(Convert.ToBase64String(result));
            }

            return(result.ToHexString());
        }
        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="value">待解密的值</param>
        /// <param name="key">密钥</param>
        /// <param name="iv">加密偏移量</param>
        /// <param name="salt">加盐</param>
        /// <param name="outType">输出类型,默认为<see cref="OutType.Base64"/></param>
        /// <param name="encoding">编码类型,默认为<see cref="Encoding.UTF8"/></param>
        /// <param name="keySize">密钥长度类型,默认为<see cref="TripleDESKeySizeType.L192"/></param>
        /// <returns></returns>
        public static string Decrypt(string value, string key, string iv = null, string salt = null,
                                     OutType outType   = OutType.Base64,
                                     Encoding encoding = null, TripleDESKeySizeType keySize = TripleDESKeySizeType.L192)
        {
            if (string.IsNullOrEmpty(value))
            {
                throw new ArgumentNullException(nameof(value));
            }

            if (string.IsNullOrEmpty(key))
            {
                throw new ArgumentNullException(nameof(key));
            }

            if (encoding == null)
            {
                encoding = Encoding.UTF8;
            }

            var result = DecryptCore <TripleDESCryptoServiceProvider>(value.GetEncryptBytes(outType),
                                                                      ComputeRealValueFunc()(key)(salt)(encoding)((int)keySize),
                                                                      ComputeRealValueFunc()(iv)(salt)(encoding)(64));

            return(encoding.GetString(result));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// DES decryption.
        /// </summary>
        /// <param name="data">The string to be decrypted,not null.</param>
        /// <param name="pwd">The password to derive the key for.</param>
        /// <param name="encoding">The <see cref="T:System.Text.Encoding"/>,default is Encoding.UTF8.</param>
        /// <param name="salt">The key salt to use to derive the key.</param>
        /// <param name="iv">The initialization vector (IV) to use to derive the key.</param>
        /// <returns>The decryption string.</returns>
        public static string Decrypt(string data, string pwd, string iv, string salt = null, Encoding encoding = null)
        {
            if (string.IsNullOrEmpty(data))
            {
                throw new ArgumentNullException(nameof(data));
            }

            if (string.IsNullOrEmpty(pwd))
            {
                throw new ArgumentNullException(nameof(pwd));
            }

            if (string.IsNullOrEmpty(iv))
            {
                throw new ArgumentNullException(nameof(iv));
            }

            if (encoding == null)
            {
                encoding = Encoding.UTF8;
            }

            //return DecryptCore<DESCryptoServiceProvider>(data, pwd, iv, salt, encoding, 64, 64);
            return(encoding.GetString(NiceDecryptCore <DESCryptoServiceProvider>(Convert.FromBase64String(data),
                                                                                 ComputeRealValueFunc()(pwd)(salt)(encoding)(64),
                                                                                 ComputeRealValueFunc()(iv)(salt)(encoding)(64))));
        }
        /// <summary>
        /// AES encryption.
        /// </summary>
        /// <param name="data">The string to be encrypted,not null.</param>
        /// <param name="pwd">The password to derive the key for.</param>
        /// <param name="keySize">The size only can be 128, 192, or 256</param>
        /// <param name="encoding">The <see cref="T:System.Text.Encoding"/>,default is Encoding.UTF8.</param>
        /// <param name="salt">The key salt to use to derive the key.</param>
        /// <param name="iv">The initialization iv (IV) to use to derive the key.</param>
        /// <param name="mode"></param>
        /// <returns>The encrypted string.</returns>
        public static string Encrypt(string data, string pwd, string iv = null, string salt = null, Encoding encoding = null,
                                     AESKeySizeTypes keySize            = AESKeySizeTypes.L256)
        {
            if (string.IsNullOrEmpty(data))
            {
                throw new ArgumentNullException(nameof(data));
            }

            if (string.IsNullOrEmpty(pwd))
            {
                throw new ArgumentNullException(nameof(pwd));
            }

            if (string.IsNullOrEmpty(iv))
            {
                throw new ArgumentNullException(nameof(iv));
            }

            if (encoding == null)
            {
                encoding = Encoding.UTF8;
            }

            //return EncryptCore<AesCryptoServiceProvider>(data, password, iv, salt, encoding, (int) keySize, 128, mode);
            return(Convert.ToBase64String(NiceEncryptCore <AesCryptoServiceProvider>(encoding.GetBytes(data),
                                                                                     ComputeRealValueFunc()(pwd)(salt)(encoding)((int)keySize),
                                                                                     ComputeRealValueFunc()(iv)(salt)(encoding)(128))));
        }
Ejemplo n.º 5
0
        /// <summary>
        /// DES decryption.
        /// </summary>
        /// <param name="data">The string to be decrypted,not null.</param>
        /// <param name="pwd">The password to derive the key for.</param>
        /// <param name="encoding">The <see cref="T:System.Text.Encoding"/>,default is Encoding.UTF8.</param>
        /// <param name="salt">The key salt to use to derive the key.</param>
        /// <param name="iv">The initialization vector (IV) to use to derive the key.</param>
        /// <returns>The decryption string.</returns>
        public static string Decrypt(string data, string pwd, string iv, string salt = null, Encoding encoding = null)
        {
            Checker.Data(data);
            Checker.Password(pwd);
            Checker.IV(iv);

            encoding = encoding.SafeValue();

            //return DecryptCore<DESCryptoServiceProvider>(data, pwd, iv, salt, encoding, 64, 64);
            return(encoding.GetString(NiceDecryptCore <DESCryptoServiceProvider>(Convert.FromBase64String(data),
                                                                                 ComputeRealValueFunc()(pwd)(salt)(encoding)(64),
                                                                                 ComputeRealValueFunc()(iv)(salt)(encoding)(64))));
        }
Ejemplo n.º 6
0
        /// <summary>
        /// AES decryption.
        /// </summary>
        /// <param name="data">The string to be decrypted,not null.</param>
        /// <param name="pwd">The password to derive the key for.</param>
        /// <param name="keySize">The size only can be 128, 192, or 256</param>
        /// <param name="encoding">The <see cref="T:System.Text.Encoding"/>,default is Encoding.UTF8.</param>
        /// <param name="salt">The key salt to use to derive the key.</param>
        /// <param name="iv">The initialization iv (IV) to use to derive the key.</param>
        /// <returns>The decryption string.</returns>
        public static string Decrypt(string data, string pwd = null, string iv = null, string salt = null, Encoding encoding = null,
                                     AESKeySizeTypes keySize = AESKeySizeTypes.L256)
        {
            Checker.Data(data);
            Checker.Password(pwd);
            Checker.IV(iv);

            encoding = encoding.SafeValue();

            //return DecryptCore<AesCryptoServiceProvider>(data, password, iv, salt, encoding, (int) keySize, 128, mode);
            return(encoding.GetString(NiceDecryptCore <AesCryptoServiceProvider>(Convert.FromBase64String(data),
                                                                                 ComputeRealValueFunc()(pwd)(salt)(encoding)((int)keySize),
                                                                                 ComputeRealValueFunc()(iv)(salt)(encoding)(128))));
        }
Ejemplo n.º 7
0
        /// <summary>
        /// TripleDES decryption.
        /// </summary>
        /// <param name="data">The string to be decrypted,not null.</param>
        /// <param name="pwd">The password to derive the key for.</param>
        /// <param name="encoding">The <see cref="T:System.Text.Encoding"/>,default is Encoding.UTF8.</param>
        /// <param name="salt">The key salt to use to derive the key.</param>
        /// <param name="keySize">The size only can be 128, 192.</param>
        /// <param name="iv">The initialization vector (IV) to use to derive the key.</param>
        /// <returns>The decryption string.</returns>
        public static string Decrypt(string data, string pwd       = null, string iv = null, string salt = null, Encoding encoding = null,
                                     TripleDESKeySizeTypes keySize = TripleDESKeySizeTypes.L192)
        {
            if (string.IsNullOrEmpty(data))
            {
                throw new ArgumentNullException(nameof(data));
            }

            if (string.IsNullOrEmpty(pwd))
            {
                throw new ArgumentNullException(nameof(pwd));
            }

            if (string.IsNullOrEmpty(iv))
            {
                throw new ArgumentNullException(nameof(iv));
            }

            if (encoding == null)
            {
                encoding = Encoding.UTF8;
            }


            return(encoding.GetString(NiceDecryptCore <TripleDESCryptoServiceProvider>(Convert.FromBase64String(data),
                                                                                       ComputeRealValueFunc()(pwd)(salt)(encoding)((int)keySize),
                                                                                       ComputeRealValueFunc()(iv)(salt)(encoding)(64))));
            //return ""; //NiceEncryptCore<TripleDESCryptoServiceProvider>(data, password, iv, salt, encoding, (int) keySize, 64, mode);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// TripleDES encryption.
        /// </summary>
        /// <param name="data">The string to be encrypted,not null.</param>
        /// <param name="pwd">The password to derive the key for.</param>
        /// <param name="keySize">The size only can be 128, 192.</param>
        /// <param name="encoding">The <see cref="T:System.Text.Encoding"/>,default is Encoding.UTF8.</param>
        /// <param name="salt">The key salt to use to derive the key.</param>
        /// <param name="iv">The initialization iv (IV) to use to derive the key.</param>
        /// <returns>The encrypted string.</returns>
        public static string Encrypt(string data, string pwd       = null, string iv = null, string salt = null, Encoding encoding = null,
                                     TripleDESKeySizeTypes keySize = TripleDESKeySizeTypes.L192)
        {
            Checker.Data(data);
            Checker.Password(pwd);
            Checker.IV(iv);

            encoding = encoding.SafeValue();

            //return EncryptCore<DESCryptoServiceProvider>(data, pwd, iv, salt, encoding, 64, 64);
            return(Convert.ToBase64String(NiceEncryptCore <TripleDESCryptoServiceProvider>(encoding.GetBytes(data),
                                                                                           ComputeRealValueFunc()(pwd)(salt)(encoding)((int)keySize),
                                                                                           ComputeRealValueFunc()(iv)(salt)(encoding)(64))));
            //return ""; //EncryptCore<TripleDESCryptoServiceProvider>(data, password, iv, salt, encoding, (int) keySize, 64, mode);
        }