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

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

    ReplyDelete
  6. Blackbird packaging provide you with any size, shape design, custom cbd bath bomb boxes sooner than any other packaging competitor in the market

    ReplyDelete
  7. """Welcome to We Buy Houses Fort Lauderdale, the Official Cash Home Buyer of Fort Lauderdale, Florida, and surrounding areas.

    We provide home sellers with a cash option to purchase their property quickly—with NO Repairs, and NO Fees or Commissions. Simply go to our website or call us at (754) 200-2005 to get a no-obligation cash home offer sent to you. It’s free and confidential.

    Avoiding foreclosure? Facing divorce? Moving? Upside down in your mortgage? Liens? It doesn’t matter whether you live in it, you’re renting it out, it’s vacant, or not even habitable. We help owners who have inherited an unwanted property, own a vacant house, are behind on payments, owe liens, downsized, and can’t sell… even if the house needs repairs that you can’t pay for… and yes, even if the house is fire damaged or has bad rental tenants. Basically, if you have a property and need to sell it… we’d like to make you a fair cash offer and close on it when you’re ready to sell.

    Just contact us today to get started!"""

    ReplyDelete
  8. Shop from a wide variety of Local and Imported hair masks and hair moisturizers available at Al-Fatah. Shop hair masks and hair moisturizers in wide variety from favorite brands today available under one roof at Al-Fatah. Shop the best for your Hair. Avail Free Delivery on order above 1000pkr in selected cities only.

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

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

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

    ReplyDelete
  12. Buy shorts men from ndure at best prices delivered at your doorstep. Get free shipping

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

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

    ReplyDelete

  15. Hi Friend,

    Just now only i joined your post. It was very informative and more knowledgeable. Your post was very interesting. Thank you for sharing this type of post. keep posting more.

    also read : read this

    ReplyDelete


  16. 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
  17. Buy Good quality baby food in Pakistan at best prices from medisense. Order Now and enjoy fast delivery at your doorstep.

    ReplyDelete
  18. Buy best quality digital blood pressure monitor price in Pakistan online at affordable prices from medisense. Delivered at your doorstep. Get fast shipping.

    ReplyDelete
  19. Buy Good quality insulin pen price in Pakistan from medisense at your door step. Fast delivery.

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

    ReplyDelete
  21. Buy watches online from Al-Fatah at best prices delivered at your doorstep. Get free shipping

    ReplyDelete
  22. 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
  23. You provide best information about Encryption and Decryption of different files. Furthermore, We provide the best Automotive locksmith Dallas services in short time.

    ReplyDelete
  24. 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
  25. 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
  26. 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
  27. 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