public static string Encrypt(string plainText, string aesKey, string aesIv) { byte[] encryptBytes = null; ICryptoTransform encryptor = AesCryptoService.GetAesEncryptor(aesKey, aesIv); // Create the streams used for encryption. using (MemoryStream msEncrypt = new MemoryStream()) { using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)) { using (StreamWriter swEncrypt = new StreamWriter(csEncrypt, Encoding.UTF8)) { //Write all data to the stream. swEncrypt.Write(plainText); } encryptBytes = msEncrypt.ToArray(); } } return(Convert.ToBase64String(encryptBytes)); }