Encrypt string with DESCryptoServiceProvider - CSharp System.Security.Cryptography

CSharp examples for System.Security.Cryptography:DES

Description

Encrypt string with DESCryptoServiceProvider

Demo Code


using System.Web;
using System.Text.RegularExpressions;
using System.IO;//from   ww w  . ja va  2 s . c  o  m
using System.Security.Cryptography;
using System.Diagnostics;
using System.Collections.Generic;
using System;

public class Main{
        public static string Encrypt(string value)
        {
            string str = "";
            if (!string.IsNullOrEmpty(value))
            {
                DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, cryptoProvider.CreateEncryptor(KEY_64, IV_64), CryptoStreamMode.Write);
                StreamWriter sw = new StreamWriter(cs);

                sw.Write(value);
                sw.Flush();
                cs.FlushFinalBlock();
                ms.Flush();

                //convert back to a string 
                str = Convert.ToBase64String(ms.GetBuffer(), 0, Convert.ToInt32(ms.Length));
            }
            return str;
        }
}

Related Tutorials