decrypt String - CSharp System.Security.Cryptography

CSharp examples for System.Security.Cryptography:Encrypt Decrypt

Description

decrypt String

Demo Code


using System.Security.Cryptography;
using System.IO;/*w w  w . ja  va  2s  .  c o  m*/
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;

public class Main{
        //key and IV should be base64
        public static string decryptString(string ciphtext, string key, string iv) {
            var textEncoder = new UTF8Encoding();

            var aes = new AesManaged();
            aes.Key = Convert.FromBase64String(key);
            aes.IV = Convert.FromBase64String(iv);
            aes.Padding = PaddingMode.Zeros;
            aes.Mode = CipherMode.CBC;

            var decryptor = aes.CreateDecryptor();
            var cipher = Convert.FromBase64String(ciphtext);
            var text_bytes = decryptor.TransformFinalBlock(cipher, 0, cipher.Length);

            var text = textEncoder.GetString(text_bytes);
            //Console.WriteLine(text);
            return text;
        }
}

Related Tutorials