DES Encrypt string with string key - CSharp System.Security.Cryptography

CSharp examples for System.Security.Cryptography:DES

Description

DES Encrypt string with string key

Demo Code


using System.Text;
using System.Security.Cryptography;
using System.Linq;
using System.IO;//from   w ww . j av  a2 s .  co  m
using System.Collections.Generic;
using System;

public class Main{
      public static string Encrypt(string encryptString, string encryptKey)
      {
         try
         {
            var rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
            var rgbIv = CryptKeys.DesKeys;
            var inputByteArray = Encoding.UTF8.GetBytes(encryptString);
            var dcsp = new DESCryptoServiceProvider();
            var mStream = new MemoryStream();
            var 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