DES Encrypt String - CSharp System.Security.Cryptography

CSharp examples for System.Security.Cryptography:DES

Description

DES Encrypt String

Demo Code


using System.Text;
using System.Security.Cryptography;
using System.IO;//from www  .j a va  2 s  . co  m
using System;

public class Main{
        public static string Encrypt(string input)
        {
            var des = new DESCryptoServiceProvider { Key = Encoding.ASCII.GetBytes(SKey), IV = Encoding.ASCII.GetBytes(SKey) };

            byte[] inputByteArray = Encoding.Default.GetBytes(input); 

            var ms = new MemoryStream(); 
            var cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();

            var ret = new StringBuilder();
            foreach (byte b in ms.ToArray())
            {
                ret.AppendFormat("{0:X2}", b);
            }
            return ret.ToString();
        }
}

Related Tutorials