Encrypt DES - CSharp Security

CSharp examples for Security:MD5

Description

Encrypt DES

Demo Code


using System.Drawing.Imaging;
using System.Drawing;
using System.IO;/*from w w  w .  j av  a 2  s . c o m*/
using System.Security.Cryptography;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;

public class Main{
        public static string EncryptDES(string encryptString, string encryptKey)
        {
            try
            {
                byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
                byte[] rgbIV = Keys;
                byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
                DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
                MemoryStream mStream = new MemoryStream();
                CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
                cStream.Write(inputByteArray, 0, inputByteArray.Length);
                cStream.FlushFinalBlock();
                return Convert.ToBase64String(mStream.ToArray());
            }
            catch
            {
                return encryptString;
            }
        }
}

Related Tutorials