Encrypt Triple DES - CSharp System.Security.Cryptography

CSharp examples for System.Security.Cryptography:DES

Description

Encrypt Triple DES

Demo Code

// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. http://www.apache.org/licenses/LICENSE-2.0..
using System.Text;
using System.Security.Cryptography;
using System.Linq;
using System.IO;//  www.j ava2s  . co  m
using System.Collections.Generic;
using System;

public class Main{
        public static string EncryptTripleDES(string text, string key)
        {
            //Expect an ANSI key of 24 chars...
            string result = "";
            if (string.IsNullOrEmpty(text)) return result;

            var crypto = getCryptoServiceProvider(key);
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, crypto.CreateEncryptor(), CryptoStreamMode.Write);
            StreamWriter sw = new StreamWriter(cs);
            sw.Write(text);
            sw.Close();
            cs.Close();
            result = Convert.ToBase64String(ms.ToArray());

            return result;
        }
}

Related Tutorials