App_Code.UserVoiceTokenGenerator.encryptStringToBytes_AES C# (CSharp) Method

encryptStringToBytes_AES() private static method

private static encryptStringToBytes_AES ( byte textBytes, byte Key, byte IV ) : byte[]
textBytes byte
Key byte
IV byte
return byte[]
        private static byte[] encryptStringToBytes_AES(byte[] textBytes, byte[] Key, byte[] IV)
        {
            // Declare the stream used to encrypt to an in memory
            // array of bytes and the RijndaelManaged object
            // used to encrypt the data.
            using (var msEncrypt = new MemoryStream())
            using (var aesAlg = new RijndaelManaged())
            {
                // Provide the RijndaelManaged object with the specified key and IV.
                aesAlg.Mode = CipherMode.CBC;
                aesAlg.Padding = PaddingMode.PKCS7;
                aesAlg.KeySize = 128;
                aesAlg.BlockSize = 128;
                aesAlg.Key = Key;
                aesAlg.IV = IV;
                // Create an encrytor to perform the stream transform.
                ICryptoTransform encryptor = aesAlg.CreateEncryptor();

                // Create the streams used for encryption.
                using (var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                {
                    csEncrypt.Write(textBytes, 0, textBytes.Length);
                    csEncrypt.FlushFinalBlock();
                }

                byte[] encrypted = msEncrypt.ToArray();
                // Return the encrypted bytes from the memory stream.
                return encrypted;
            }
        }