Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><a href="http://www.achadwick.com/DeveloperBlog/Result/Triple%20DES%20Encryption" rel="nofollow">Here</a> is a complete example copy paste ready..</p> <pre><code>using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Security.Cryptography; using System.Text; using System.Web; namespace MVC3JavaScript_3_2012.Utilities { public class MyTripleDES { private readonly TripleDESCryptoServiceProvider _des = new TripleDESCryptoServiceProvider(); private readonly UTF8Encoding _utf8 = new UTF8Encoding(); /// &lt;summary&gt; /// Key to use during encryption and decryption /// &lt;/summary&gt; private byte[] _keyValue; public byte[] Key { get { return _keyValue; } private set { _keyValue = value; } } /// &lt;summary&gt; /// Initialization vector to use during encryption and decryption /// &lt;/summary&gt; private byte[] _ivValue; public byte[] IV { get { return _ivValue; } private set { _ivValue = value; } } /// &lt;summary&gt; /// Constructor, allows the key and initialization vector to be provided as strings /// &lt;/summary&gt; /// &lt;param name="key"&gt;&lt;/param&gt; /// &lt;param name="iv"&gt;&lt;/param&gt; public MyTripleDES(string key, string iv) { _keyValue = Convert.FromBase64String(key); _ivValue = Convert.FromBase64String(iv); } /// &lt;summary&gt; /// Decrypt Bytes /// &lt;/summary&gt; /// &lt;param name="bytes"&gt;&lt;/param&gt; /// &lt;returns&gt;&lt;/returns&gt; public byte[] Decrypt(byte[] bytes) { return Transform(bytes, _des.CreateDecryptor(_keyValue, _ivValue)); } /// &lt;summary&gt; /// Encrypt Bytes /// &lt;/summary&gt; /// &lt;param name="bytes"&gt;&lt;/param&gt; /// &lt;returns&gt;&lt;/returns&gt; public byte[] Encrypt(byte[] bytes) { return Transform(bytes, _des.CreateEncryptor(_keyValue, _ivValue)); } /// &lt;summary&gt; /// Decrypt a string /// &lt;/summary&gt; /// &lt;param name="text"&gt;&lt;/param&gt; /// &lt;returns&gt;&lt;/returns&gt; public string Decrypt(string text) { byte[] input = HttpServerUtility.UrlTokenDecode(text); byte[] output = Transform(input, _des.CreateDecryptor(_keyValue, _ivValue)); return _utf8.GetString(output); } /// &lt;summary&gt; /// Encrypt a string and return Base64String /// &lt;/summary&gt; /// &lt;param name="text"&gt;&lt;/param&gt; /// &lt;returns&gt;&lt;/returns&gt; public string Encrypt(string text) { byte[] input = _utf8.GetBytes(text); byte[] output = Transform(input, _des.CreateEncryptor(_keyValue, _ivValue)); return HttpServerUtility.UrlTokenEncode(output); } /// &lt;summary&gt; /// Encrypt or Decrypt bytes. /// &lt;/summary&gt; private byte[] Transform(byte[] input, ICryptoTransform cryptoTransform) { // Create the necessary streams using (var memory = new MemoryStream()) { using (var stream = new CryptoStream(memory, cryptoTransform, CryptoStreamMode.Write)) { // Transform the bytes as requested stream.Write(input, 0, input.Length); stream.FlushFinalBlock(); // Read the memory stream and convert it back into byte array memory.Position = 0; var result = new byte[memory.Length]; memory.Read(result, 0, result.Length); // Return result return result; } } } public static string CreateNewVector() { using (var des = new System.Security.Cryptography.TripleDESCryptoServiceProvider()) { des.GenerateIV(); return Convert.ToBase64String(des.IV); } } public static string CreateNewKey() { using (var des = new System.Security.Cryptography.TripleDESCryptoServiceProvider()) { des.GenerateKey(); return Convert.ToBase64String(des.Key); } } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload