Exemplo n.º 1
0
        /// <summary>
        /// Decrypts the given encrypted text using the given key.
        /// </summary>
        /// <param name="cipherText">The encrypted text</param>
        /// <param name="keyBytes">The key to use</param>
        /// <returns>The decrypted text</returns>
        public static string DecryptAES(string cipherText, byte[] keyBytes)
        {
            //DO NOT DECRYPT EMPTY STRING
            if (string.IsNullOrEmpty(cipherText))
            {
                return(string.Empty);
            }

            //DO NOT DECRYPT IF THERE IS NO CRYPT KEY
            if ((keyBytes == null) || (keyBytes.Length == 0))
            {
                return(cipherText);
            }

            // STORAGE FOR DATA DECODED FROM BASE64
            byte[] ivPlusCipher;
            try
            {
                // DECODE THE BASE64 DATA
                ivPlusCipher = Convert.FromBase64String(cipherText);
            }
            catch (System.FormatException)
            {
                // DATA WAS NOT VALID BASE64, IT CANNOT BE DECRYPTED
                return(cipherText);
            }

            // THE DECRYPTED DATA MUST BE AT LEAST 17 BYTES (AND PROBABLY LONGER)
            if (ivPlusCipher.Length <= 16)
            {
                return(cipherText);
            }

            //CONVERT CIPHER TEXT TO BYTES
            try
            {
                //SPLIT THE IV (FIRST 128 BITS) AND CIPHER TEXT
                byte[] ivBytes         = new byte[16];
                byte[] cipherTextBytes = new byte[ivPlusCipher.Length - 16];
                System.Buffer.BlockCopy(ivPlusCipher, 0, ivBytes, 0, 16);
                System.Buffer.BlockCopy(ivPlusCipher, 16, cipherTextBytes, 0, cipherTextBytes.Length);

                //CONFIGURE AES
                RijndaelManaged symmetricKey = new RijndaelManaged();
                symmetricKey.Mode = CipherMode.CBC;
                symmetricKey.Key  = keyBytes;
                symmetricKey.IV   = ivBytes;

                //CREATE DECRYPTOR
                ICryptoTransform decryptor = symmetricKey.CreateDecryptor();

                //CREATE BUFFER TO HOLD DECRYPTED TEXT
                byte[] plainTextBytes = new byte[cipherTextBytes.Length];
                int    decryptedByteCount;

                //CREATE MEMORY STREAM OF DECRYPTED DATA
                using (MemoryStream memoryStream = new MemoryStream(cipherTextBytes))
                {
                    //CREATE THE CRYPTO STREAM
                    using (CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
                    {
                        //DECRYPT THE CIPHER TEXT
                        decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);

                        //CLOSE CRYPTO STREAM
                        cryptoStream.Close();
                    }
                    //CLOSE MEMORY STREAM
                    memoryStream.Close();
                }

                //CONVERT DECRYPTED BYTES TO STRING
                string plainText = Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);

                //RETURN DECRYPTED TEXT
                return(plainText);
            }
            catch (Exception ex)
            {
                //SOMETHING WENT WRONG, RETURN ORIGINAL VALUE
                string scriptName = HttpContextHelper.GetCurrentScriptName();
                Logger.Debug("Error decrypting value " + cipherText + " in script " + scriptName, ex);
                return(cipherText);
            }
        }