Пример #1
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="input"></param>
        /// <param name="privateKey"></param>
        /// <param name="inputType"></param>
        /// <returns></returns>
        public static string Decrypt(string input, string privateKey, StringByteType inputType = StringByteType.Hex)
        {
            if (string.IsNullOrEmpty(input)) throw new ArgumentNullException(nameof(input));
            var arr = inputType == StringByteType.Hex ? Afx.Utils.StringUtils.HexStringToByte(input)
                : Convert.FromBase64String(input);
            var buffer = Decrypt(arr, privateKey);
            var result = Encoding.UTF8.GetString(buffer);

            return result;
        }
Пример #2
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="input"></param>
        /// <param name="publicKey"></param>
        /// <param name="resultType"></param>
        /// <returns></returns>
        public static string Encrypt(string input, string publicKey, StringByteType resultType = StringByteType.Hex)
        {
            if (string.IsNullOrEmpty(input)) throw new ArgumentNullException(nameof(input));
            var arr = Encoding.UTF8.GetBytes(input);
            var buffer = Encrypt(arr, publicKey);
            var result = resultType == StringByteType.Hex ? Afx.Utils.StringUtils.ByteToHexString(buffer)
                : Convert.ToBase64String(buffer);

            return result;
        }
Пример #3
0
        /// <summary>
        /// 获取MD5值
        /// </summary>
        /// <param name="input"></param>
        /// <param name="resultType"></param>
        /// <returns></returns>
        public static string GetMd5Hash(string input, StringByteType resultType = StringByteType.Hex)
        {
            if (input == null)
            {
                throw new ArgumentNullException("input");
            }
            byte[] data   = Encoding.UTF8.GetBytes(input);
            byte[] buffer = GetMd5Hash(data);
            var    result = resultType == StringByteType.Hex ? StringUtils.ByteToHexString(buffer)
                : Convert.ToBase64String(buffer);

            return(result);
        }
Пример #4
0
        /// <summary>
        /// 获取哈希值
        /// </summary>
        /// <param name="input"></param>
        /// <param name="key"></param>
        /// <param name="resultType"></param>
        /// <returns></returns>
        public static string GetHash(string input, string key, StringByteType resultType = StringByteType.Hex)
        {
            string result = null;

            if (input != null && !string.IsNullOrEmpty(key))
            {
                byte[] keybytes = Encoding.UTF8.GetBytes(key);
                using (var sha = new HMACSHA256(keybytes))
                {
                    byte[] buffer = sha.ComputeHash(Encoding.UTF8.GetBytes(input));
                    result = resultType == StringByteType.Hex ? StringUtils.ByteToHexString(buffer)
                        : Convert.ToBase64String(buffer);
                }
            }

            return(result);
        }
Пример #5
0
        /// <summary>
        /// 解密 string
        /// </summary>
        /// <param name="input">string</param>
        /// <param name="key">24个ASCII字符 key</param>
        /// <param name="iv">8 个ASCII字符 iv</param>
        /// <param name="mode">指定用于解密的块密码模式</param>
        /// <param name="padding">指定在消息数据块比解密操作所需的全部字节数短时应用的填充类型</param>
        /// <param name="inputType"></param>
        /// <returns>解密成功返回string</returns>
        public static string Decrypt(string input, string key, string iv, CipherMode mode, PaddingMode padding, StringByteType inputType = StringByteType.Hex)
        {
            string output = null;

            byte[] inputdata = inputType == StringByteType.Hex ? StringUtils.HexStringToByte(input)
                : Convert.FromBase64String(input);
            if (inputdata != null && inputdata.Length > 0)
            {
                byte[] outputdata = Decrypt(inputdata, key, iv, mode, padding);
                if (outputdata != null && outputdata.Length > 0)
                {
                    output = Encoding.UTF8.GetString(outputdata);
                }
            }
            else if (input == string.Empty)
            {
                output = string.Empty;
            }

            return(output);
        }
Пример #6
0
 /// <summary>
 /// 解密 string
 /// </summary>
 /// <param name="input">string</param>
 /// <param name="key">24个ASCII字符 key</param>
 /// <param name="mode">指定用于解密的块密码模式</param>
 /// <param name="padding">指定在消息数据块比解密操作所需的全部字节数短时应用的填充类型</param>
 /// <param name="inputType"></param>
 /// <returns>解密成功返回string</returns>
 public static string Decrypt(string input, string key, CipherMode mode, PaddingMode padding, StringByteType inputType = StringByteType.Hex)
 {
     return(Decrypt(input, key, DefaultIV, mode, padding, inputType));
 }
Пример #7
0
 /// <summary>
 /// 解密 string
 /// </summary>
 /// <param name="input">string</param>
 /// <param name="key">24个ASCII字符 key</param>
 /// <param name="iv">8个ASCII字符 iv</param>
 /// <param name="inputType"></param>
 /// <returns>解密成功返回string</returns>
 public static string Decrypt(string input, string key, string iv, StringByteType inputType = StringByteType.Hex)
 {
     return(Decrypt(input, key, iv, DefaultMode, DefaultPadding, inputType));
 }
Пример #8
0
 /// <summary>
 /// 解密 string
 /// </summary>
 /// <param name="input">string</param>
 /// <param name="inputType"></param>
 /// <returns>解密成功返回string</returns>
 public static string Decrypt(string input, StringByteType inputType = StringByteType.Hex)
 {
     return(Decrypt(input, DefaultKey, inputType));
 }
Пример #9
0
        /// <summary>
        /// 加密 string
        /// </summary>
        /// <param name="input">string</param>
        /// <param name="key">24个ASCII字符 key</param>
        /// <param name="iv">8个ASCII字符 iv</param>
        /// <param name="mode">指定用于加密的块密码模式</param>
        /// <param name="padding">指定在消息数据块比加密操作所需的全部字节数短时应用的填充类型</param>
        /// <param name="resultType"></param>
        /// <returns>加密成功返回string</returns>
        public static string Encrypt(string input, string key, string iv, CipherMode mode, PaddingMode padding, StringByteType resultType = StringByteType.Hex)
        {
            string output = null;

            if (!string.IsNullOrEmpty(input))
            {
                byte[] inputdata  = Encoding.UTF8.GetBytes(input);
                byte[] outputdata = Encrypt(inputdata, key, iv, mode, padding);
                output = resultType == StringByteType.Hex ? StringUtils.ByteToHexString(outputdata)
                    : Convert.ToBase64String(outputdata);
            }
            else if (input == string.Empty)
            {
                output = string.Empty;
            }

            return(output);
        }
Пример #10
0
 /// <summary>
 /// 加密 string
 /// </summary>
 /// <param name="input">string</param>
 /// <param name="key">24个ASCII字符 key</param>
 /// <param name="iv">8个ASCII字符 iv</param>
 /// <param name="resultType"></param>
 /// <returns>加密成功返回string</returns>
 public static string Encrypt(string input, string key, string iv, StringByteType resultType = StringByteType.Hex)
 {
     return(Encrypt(input, key, iv, DefaultMode, DefaultPadding, resultType));
 }
Пример #11
0
 /// <summary>
 /// 加密 string
 /// </summary>
 /// <param name="input">string</param>
 /// <param name="resultType"></param>
 /// <returns>加密成功返回string</returns>
 public static string Encrypt(string input, StringByteType resultType = StringByteType.Hex)
 {
     return(Encrypt(input, DefaultKey, resultType));
 }
Пример #12
0
        /// <summary>
        /// 解密 string
        /// </summary>
        /// <param name="input">string</param>
        /// <param name="key">8 个ASCII字符 key</param>
        /// <param name="iv">8 个ASCII字符 iv</param>
        /// <param name="mode">指定用于解密的块密码模式</param>
        /// <param name="padding">指定在消息数据块比解密操作所需的全部字节数短时应用的填充类型</param>
        /// <param name="inputType"></param>
        /// <returns>解密成功返回string</returns>
        public static string Decrypt(string input, string key, string iv, CipherMode mode, PaddingMode padding, StringByteType inputType = StringByteType.Hex)
        {
            string output = null;

            if (!string.IsNullOrEmpty(input))
            {
                if (input.Length % 2 != 0)
                {
                    throw new ArgumentException("input is error!", "input");
                }
                byte[] inputdata = inputType == StringByteType.Hex ? StringUtils.HexStringToByte(input)
                    : Convert.FromBase64String(input);
                byte[] outputdata = Decrypt(inputdata, key, iv, mode, padding);
                if (outputdata != null && outputdata.Length > 0)
                {
                    output = Encoding.UTF8.GetString(outputdata);
                }
            }
            else if (input == string.Empty)
            {
                output = string.Empty;
            }

            return(output);
        }
Пример #13
0
 /// <summary>
 /// 加密 string
 /// </summary>
 /// <param name="input">string</param>
 /// <param name="key">8 个ASCII字符 key</param>
 /// <param name="mode">指定用于加密的块密码模式</param>
 /// <param name="padding">指定在消息数据块比加密操作所需的全部字节数短时应用的填充类型</param>
 /// <param name="resultType"></param>
 /// <returns>加密成功返回string</returns>
 public static string Encrypt(string input, string key, CipherMode mode, PaddingMode padding, StringByteType resultType = StringByteType.Hex)
 {
     return(Encrypt(input, key, null, mode, padding, resultType));
 }