Encrypt String with DES - CSharp System.Security.Cryptography

CSharp examples for System.Security.Cryptography:DES

Description

Encrypt String with DES

Demo Code


using System.Security.Cryptography;
using System.IO;/*from   w  ww  . ja va  2 s . c o  m*/
using System.Text;
using System;

public class Main{
        private static DESCryptoServiceProvider desCSP = new DESCryptoServiceProvider();
        public static string Encrypt(string input)
        {            
            byte[] Key = Encoding.UTF8.GetBytes(strKey);
            byte[] IV = Encoding.UTF8.GetBytes(strIV);     
            byte[] byt = Encoding.UTF8.GetBytes(input); 
            ICryptoTransform ct = desCSP.CreateEncryptor(Key, IV);
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
            cs.Write(byt, 0, byt.Length);
            cs.FlushFinalBlock();
            cs.Close();
            return Convert.ToBase64String(ms.ToArray()); 
        }
}

Related Tutorials