find a location for property in a new city

Thursday, 8 September 2011

Triple DES encryption and decryption with .NET

TripleDes essentials

Start your class like this:
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace Utils
{
 public static class EncryptionHelper
 {
    static private byte[] key = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
                                            15, 16, 17, 18, 19, 20, 21, 22, 23, 24};
    static private byte[] iv8Bit = { 1,2,3,4,5,6,7,8 };

TripleDes Encryption

Add the following method for encrypting with Triple DES:
public static string TripleDesEncryption(string dataToEncrypt)
{
   var bytes = Encoding.Default.GetBytes(dataToEncrypt);
   using (var tripleDes= new TripleDESCryptoServiceProvider())
   {
    using (var ms = new MemoryStream())
    using (var encryptor = tripleDes.CreateEncryptor(key, iv8Bit))
    using (var cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
    {
     cs.Write(bytes, 0, bytes.Length);
     cs.FlushFinalBlock();
     var cipher = ms.ToArray();
     return Convert.ToBase64String(cipher);
    }
   }
}

Triple DES Decryption

The next method will allow decryption of your data
public static string TripleDesDecryption(string dataToDecrypt)
{
   var bytes = Convert.FromBase64String(dataToDecrypt);
   using (var tripleDes= new TripleDESCryptoServiceProvider())
   {
    using (var ms = new MemoryStream())
    using (var decryptor = tripleDes.CreateDecryptor(key, iv8Bit))
    using (var cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Write))
    {
     cs.Write(bytes, 0, bytes.Length);
     cs.FlushFinalBlock();
     var cipher = ms.ToArray();
     return Encoding.UTF8.GetString(cipher);
    }
   }
}

Follow britishdev on Twitter

8 comments:

  1. Motifz Designer Lawn. 1, 2 & 3 Piece Unstitched Premium Embroidered Lawn 2019, Premium embroidered Lawn, Premium lawn, Premium lawn 2019, Motifz, Premium lawn in Pakistan. Shipping worldwide. Stitching option available.

    ReplyDelete
  2. I surely getting a charge out of every single bit of it. It is an uncommon site and lovely offer.It is in motivation behind reality a mind blowing and pleasing piece of data. I'm satisfied that you basically granted this significant information to us. If its all the same to you keep awake with the most recent like this. An obligation of appreciation is all together for sharing. Visit site Cheap essay writing service

    ReplyDelete
  3. The .NET framework is backward compatible, i.e. you can use a function used in .NET 1 in 2 or you can compile and run a Master Thesis Writing Service program developed in .NET 1 in system with .NET 2 and .NET 3. Like wise you can also run programs developed in .NET 2 in system with .NET 3. This is called as Backward compatibility, i.e. support for previous versions how to write dissertation online UK.

    ReplyDelete
  4. Hi, i am Angylinca Wilson. Encryption and description is very interesting topic for Networking. Its very secure and valuable. Now, I am working coursework help at very affordable price for other company. so students orders now.

    ReplyDelete
  5. I think Encryption is a scheme where an intelligible text plaintext in crypto terms is made unintelligible ciphertext in crypto terms using a secure key. Block and stream ciphers and public key systems do this work. The security of the ciphers reside in the key length and decryption processbest cheap essay writing services in UK is a difficult without proper knowledge of the key.

    ReplyDelete
  6. Very well Excellent information If could you need some logistics services Best air freight companies is offering all logistics services the workforce is well trained and they are profession and deliver your godds one place to another timely.

    ReplyDelete
  7. Buy online wholesale printed custom cbd boxes and packaging at easy prices and shipping worldwide, with 24/7 customer support. Get a free custom quote now!!

    ReplyDelete
  8. Blackbird packaging provide you with any size, shape design, makeup boxes sooner than any other packaging competitor in the market

    ReplyDelete