find a location for property in a new city

Thursday 8 September 2011

AES encryption and decryption in .NET

AES 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[] iv16Bit = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16 };

AES Encryption

Add the following method for encrypting with AES:
public static string AesEncryption(string dataToEncrypt)
{
   var bytes = Encoding.Default.GetBytes(dataToEncrypt);
   using (var aes = new AesCryptoServiceProvider())
   {
    using (var ms = new MemoryStream())
    using (var encryptor = aes.CreateEncryptor(key, iv16Bit))
    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);
    }
   }
}

AES Decryption

The next method will allow decryption of your data
public static string AesDecryption(string dataToDecrypt)
{
   var bytes = Convert.FromBase64String(dataToDecrypt);
   using (var aes = new AesCryptoServiceProvider())
   {
    using (var ms = new MemoryStream())
    using (var decryptor = aes.CreateDecryptor(key, iv16Bit))
    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

20 comments:

  1. Recently I am working on this only but was having problem with key leng. After following this I think I have caught the problem to where I was getting wrong.

    ReplyDelete
  2. Hey really much appreciate your work really you have done a great work and providing useful information over here. Thanks for sharing

    ReplyDelete
  3. 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
  4. Blackbird packaging provide you with any size, shape design, custom e liquid boxes sooner than any other packaging competitor in the market

    ReplyDelete
  5. Buy leggings for women online from Dshred at easy prices delivered at your doorstep. Get free shipping

    ReplyDelete
  6. Buy gym trousers mens online pakistan online dshred at best prices delivered at your doorstep. Get free shipping

    ReplyDelete
  7. Buy gym trousers for men from dshred at best prices delivered at your doorstep. Get free shipping

    ReplyDelete


  8. Hello Buddy,
    I saw your forum post. It was really nice . I found its highly informative and knowledgeable forum post.Thanks for sharing this forum post. keep going. And post more forums.
    Also read: TekPatron

    ReplyDelete
  9. Buy Good quality baby food in Pakistan at best prices from medisense. Order Now and enjoy fast delivery at your doorstep.

    ReplyDelete
  10. Buy best quality certeza nebulizer price in Pakistan online from medisense. Fast Delivery.

    ReplyDelete
  11. Great post! you deliver information related to encryption and decryption files. I like your post. Moreover, garage door repair Woodstock provide the best and quality work for repairing.

    ReplyDelete
  12. You provide best information about Encryption and Decryption of different files. Furthermore, We provide the best Automotive locksmith Dallas services in short time.

    ReplyDelete
  13. Great and informative post. This post is very helpful for computer students and IT professional. This time to take advantages of Laminate wood floor Miami under professional team.

    ReplyDelete
  14. You discuss related to encryption and decryption in detail. This post is very helpful for students. Further, gutter cleaning Boston ma present the best and quality cleaning work.

    ReplyDelete
  15. Uttar Pradesh Government provides jobs for 08th Pass, 10th Pass, 12th Pass, Graduates, Engineers, etc. The job seekers who meet the recruitment board's eligibility criteria can apply for the UP Govt recruitments from here.
    also read : up sarkari job

    ReplyDelete
  16. You provide the complete method of encryption and decryption method in detail. Thank you for sharing informative post. I learn more through your post. Moreover, The air duct cleaning Denver deliver the best and affordable air duct cleaning work.

    ReplyDelete
  17. Great post! You deliver the best article related to encryption and description. This post is very helpful for IT students. Moreover, The Garage door companies in CT present the best and affordable services as your desired needs.

    ReplyDelete
  18. You provide the informative knowledge related to data security and different techniques in detail. This post is very helpful for students. Moreover, The Professional locksmith services in san Antonio deliver the best locksmith work at a low price.

    ReplyDelete
  19. Borjan offers its customers the best running shoes for men online in Pakistan. We have the highest quality running shoes for men in Pakistan for the most affordable prices. Get Borjan's amazing collection of men's running shoes online. Get your favorite shoes for men from Borjan to enjoy style and comfort at the same time.

    ReplyDelete
  20. Buy best quality comfortable ladies dri-fit training shirts online from Ndure. Fast Shipping.

    ReplyDelete