コード例 #1
0
        /// <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="AESKeySizeType.L256"/></param>
        /// <returns></returns>
        public static string Encrypt(string value, string key, string iv = null, string salt = null,
                                     OutType outType   = OutType.Base64,
                                     Encoding encoding = null, AESKeySizeType keySize = AESKeySizeType.L256)
        {
            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)((int)keySize),
                                                                ComputeRealValueFunc()(iv)(salt)(encoding)(128));

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

            return(result.ToHexString());
        }
コード例 #2
0
        /// <summary>
        /// 创建 AES 密钥
        /// </summary>
        /// <param name="size">密钥长度类型,默认为<see cref="AESKeySizeType.L256"/></param>
        /// <param name="encoding">编码类型,默认为<see cref="Encoding.UTF8"/></param>
        /// <returns></returns>
        public static AESKey CreateKey(AESKeySizeType size = AESKeySizeType.L256, Encoding encoding = null)
        {
            if (encoding == null)
            {
                encoding = Encoding.UTF8;
            }

            using (var provider = new AesCryptoServiceProvider()
            {
                KeySize = (int)size
            })
            {
                return(new AESKey()
                {
                    Key = encoding.GetString(provider.Key),
                    IV = encoding.GetString(provider.IV),
                    Size = size
                });
            }
        }