Encrypts a string with Rijndael - CSharp System.Security.Cryptography

CSharp examples for System.Security.Cryptography:Encrypt Decrypt

Description

Encrypts a string with Rijndael

Demo Code


using System.Text;
using System.Security.Cryptography;
using System;// w w  w.  j  ava2s. c  o  m

public class Main{
        /// <summary>
      ///     Encrypts a string with Rijndael
      /// </summary>
      /// <param name="plainText">String to encrypt</param>
      /// <returns>Encrypted string</returns>
      public static string Encrypt(string plainText)
      {
         var rijndael = new RijndaelManaged();
         rijndael.Key = Convert.FromBase64String(key);
         rijndael.IV = Convert.FromBase64String(IV);

         byte[] buffer = Encoding.UTF8.GetBytes(plainText);

         return Convert.ToBase64String(rijndael.CreateEncryptor().TransformFinalBlock(buffer, 0, buffer.Length));
      }
}

Related Tutorials