CSharp examples for System.Security.Cryptography:Encrypt Decrypt
Encrypt string with encryption Key
using System.Text; using System.Security.Cryptography; using System.Linq; using System.IO;/* w ww . j ava2s. c o m*/ using System.Collections.Generic; using System; public class Main{ public static string Encrypt(string text, string encryptionKey) { // Check for valid length encryption key if (encryptionKey.Length < 8) { throw new Exception("Encryption Key must be a minimum of 8 characters long"); } Key = System.Text.Encoding.UTF8.GetBytes(encryptionKey.Substring(0, 8)); var des = new DESCryptoServiceProvider(); // Convert our input string to a byte array var inputByteArray = Encoding.UTF8.GetBytes(text); // Now encrypt the bytearray var ms = new MemoryStream(); var cs = new CryptoStream(ms, des.CreateEncryptor(Key, IV), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); // Now return the byte array as a "safe for XMLDOM" Base64 String return Convert.ToBase64String(ms.ToArray()); } }