CSharp examples for System.Security.Cryptography:DES
Encrypt input string by DES algorithm.
/*************************************************************************** Copyright (C) 2010 Eunge (Email: eunge.liu@gmail.com, Legal Name: Jian Liu) This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.// ww w . ja v a 2 s . c o m This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. ***************************************************************************/ using System.Security.Cryptography; using System.Collections.Generic; using System.Text; using System.IO; using System; public class Main{ /// <summary> /// Encrypt input string by DES algorithm. /// </summary> /// <param name="inputString">Input string is for encryption.</param> /// <returns>The encrypted string.</returns> public static string DESEncrypt(string inputString) { MemoryStream ms = null; CryptoStream cs = null; StreamWriter sw = null; DESCryptoServiceProvider des = new DESCryptoServiceProvider(); try { ms = new MemoryStream(); cs = new CryptoStream(ms, des.CreateEncryptor(key, iv), CryptoStreamMode.Write); sw = new StreamWriter(cs); sw.Write(inputString); sw.Flush(); cs.FlushFinalBlock(); return Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length); } finally { if (sw != null) { sw.Close(); } if (cs != null) { cs.Close(); } if (ms != null) { ms.Close(); } } } }