C # achieve ASE encryption and decryption

///  <the Summary> 
        /// AES encryption (Advanced Encryption Standard, is the next generation of encryption algorithm standard, fast, high security level, a realization that the current AES standard Rijndael algorithm)
         ///  </ the Summary> 
        // /  <param name = "encryptString"> be encrypted plaintext </ param> 
        ///  <param name = "encryptkey"> encryption key </ param> 
        ///  <Returns> </ Returns> 
        [HttpPost ( " Test " )]
         public  String AesEncrypt ([FromBody] the Request Request)
        {
            string encryptString = request.encryptString;
            string encryptKey = request.encryptKey;
            if (string.IsNullOrEmpty(encryptString))
            {
                the throw ( new new Exception ( " plaintext can not be empty " ));
            }

            if (string.IsNullOrEmpty(encryptKey))
            {
                the throw ( new new Exception ( " Key can not be null " ));
            }

            var mStrEncrypt = "";

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

            var mAesProvider = Rijndael.Create();

            try
            {
                // be encrypted string into a plaintext format 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;
        }

        ///  <the Summary> 
        /// AES decryption (Advanced Encryption Standard, is the next generation of encryption algorithm standard, fast, high security level, there is an implementation of the AES standard Rijndael algorithm)
         ///  </ the Summary> 
        // /  <param name = "decryptString"> be decrypted ciphertext </ param> 
        ///  <param name = "decryptKey"> decryption key </ param> 
        ///  <Returns> </ Returns> 
        [HttpPost ( " The decrypt " )]
         public  String AesDecrypt ([FromBody] the Request Request)
        {
            string decryptString = request.encryptString;
            string decryptKey = request.encryptKey;
            if (string.IsNullOrEmpty(decryptString))
            {
                the throw ( new new Exception ( " ciphertext can not empty " ));
            }

            if (string.IsNullOrEmpty(decryptKey))
            {
                the throw ( new new Exception ( " Key can not be null " ));
            }

            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;
        }

 

Guess you like

Origin www.cnblogs.com/yxcn/p/11699193.html