代码改变世界

C#实现ASE加密解密

2019-10-18 16:45  石吴玉  阅读(1013)  评论(0编辑  收藏  举报
/// <summary>
        /// AES 加密(高级加密标准,是下一代的加密算法标准,速度快,安全级别高,目前 AES 标准的一个实现是 Rijndael 算法)
        /// </summary>
        /// <param name="encryptString">待加密明文</param>
        /// <param name="encryptKey">加密密钥</param>
        /// <returns></returns>
        [HttpPost("test")]
        public string AesEncrypt([FromBody]Request request)
        {
            string encryptString = request.encryptString;
            string encryptKey = request.encryptKey;
            if (string.IsNullOrEmpty(encryptString))
            {
                throw (new Exception("明文不得为空"));
            }

            if (string.IsNullOrEmpty(encryptKey))
            {
                throw (new Exception("密钥不得为空"));
            }

            var mStrEncrypt = "";

            var mBtIv = Convert.FromBase64String("Rkb4jvUy/ye7Cd7k89QQgQ==");

            var mAesProvider = Rijndael.Create();

            try
            {
                // 待加密明文串转为Bytes格式
                var mBtEncryptString = Encoding.UTF8.GetBytes(encryptString);

                var mStream = new MemoryStream();

                var mCsstream = new CryptoStream(mStream,
                    mAesProvider.CreateEncryptor(Encoding.UTF8.GetBytes(encryptKey), mBtIv), CryptoStreamMode.Write);

                mCsstream.Write(mBtEncryptString, 0, mBtEncryptString.Length);
                mCsstream.FlushFinalBlock();

                mStrEncrypt = Convert.ToBase64String(mStream.ToArray());

                mStream.Close();
                mStream.Dispose();

                mCsstream.Close();
                mCsstream.Dispose();
            }
            catch (IOException ex)
            {
                throw ex;
            }
            catch (CryptographicException ex)
            {
                throw ex;
            }
            catch (ArgumentException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                mAesProvider.Clear();
            }

            return mStrEncrypt;
        }

        /// <summary>
        /// AES 解密(高级加密标准,是下一代的加密算法标准,速度快,安全级别高,目前 AES 标准的一个实现是 Rijndael 算法)
        /// </summary>
        /// <param name="decryptString">待解密密文</param>
        /// <param name="decryptKey">解密密钥</param>
        /// <returns></returns>
        [HttpPost("Decrypt")]
        public string AesDecrypt([FromBody]Request request)
        {
            string decryptString = request.encryptString;
            string decryptKey = request.encryptKey;
            if (string.IsNullOrEmpty(decryptString))
            {
                throw (new Exception("密文不得为空"));
            }

            if (string.IsNullOrEmpty(decryptKey))
            {
                throw (new Exception("密钥不得为空"));
            }

            var mStrDecrypt = "";

            var mBtIv = Convert.FromBase64String("Rkb4jvUy/ye7Cd7k89QQgQ==");

            var mAesProvider = Rijndael.Create();

            try
            {
                var mBtDecryptString = Convert.FromBase64String(decryptString);

                var mStream = new MemoryStream();

                var mCsstream = new CryptoStream(mStream,
                    mAesProvider.CreateDecryptor(Encoding.UTF8.GetBytes(decryptKey), mBtIv), CryptoStreamMode.Write);

                mCsstream.Write(mBtDecryptString, 0, mBtDecryptString.Length);
                mCsstream.FlushFinalBlock();

                mStrDecrypt = Encoding.Default.GetString(mStream.ToArray());

                mStream.Close();
                mStream.Dispose();

                mCsstream.Close();
                mCsstream.Dispose();
            }
            catch (IOException ex)
            {
                throw ex;
            }
            catch (CryptographicException ex)
            {
                throw ex;
            }
            catch (ArgumentException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                mAesProvider.Clear();
            }

            return mStrDecrypt;
        }