Tuesday, January 27, 2015

Encrypt and Decrypt String in C#

In this article i provided the simple Encrypt and Decrypt methods


         Method to encrypt string value
 
        public static string EncryptToString(string value)
        {
            const string encryptionKey = "MAKV2SPBNI99212";
            byte[] clearBytes = Encoding.Unicode.GetBytes(value);
            using (Aes encryptor = Aes.Create())
            {
                Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(encryptionKey, new byte[] { 0x49,                   0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
                encryptor.Key = pdb.GetBytes(32);
                encryptor.IV = pdb.GetBytes(16);
                using (MemoryStream ms = new MemoryStream())
                {
                    using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(),                                     CryptoStreamMode.Write))
                    {
                        cs.Write(clearBytes, 0, clearBytes.Length); cs.Close();
                    }
                    value = Convert.ToBase64String(ms.ToArray());
                }
            }
            return value;
        }


        Method to decrypt the sting
     
     
        public static string DecryptToString(string value)
        {
            if (!string.IsNullOrEmpty(value))
            {
                const string encryptionKey = "MAKV2SPBNI99212";
                byte[] cipherBytes = Convert.FromBase64String(value);
                using (Aes encryptor = Aes.Create())
                {
                    Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(encryptionKey,
                        new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64,                                 0x65, 0x76 });
                    encryptor.Key = pdb.GetBytes(32);
                    encryptor.IV = pdb.GetBytes(16);
                    using (MemoryStream ms = new MemoryStream())
                    {
                        using (
                            CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(),                                               CryptoStreamMode.Write))
                        {
                            cs.Write(cipherBytes, 0, cipherBytes.Length);
                            cs.Close();
                        }
                        value = Encoding.Unicode.GetString(ms.ToArray());
                    }
                }
            }
            else
                value = string.Empty;
            return value;
        }

24 comments:

  1. Great information,i like this kind of blog information really very nice and more new skills to develop after reading that post.

    Best Dot Net Training Institutes in Chennai

    ReplyDelete
  2. Superb. I really enjoyed very much with this article here. Really it is an amazing article I had ever read. I hope it will help a lot for all. Thank you so much for this amazing posts and please keep update like this excellent article.thank you for sharing such a great blog with us. expecting for your.
    Digital Marketing Company in India
    Seo Company in India

    ReplyDelete
  3. Webtrackker technology is the best IT training institute in NCR. Webtrackker provide training on all latest technology such as dot net training. Webtrackker is not only training institute but also it also provide best IT solution to his client. Webtrackker provide training by experienced and working in the industry on same technology.Webtrackker Technology C-67 Sector-63 Noida 8802820025

    dot net training institute in indirapuram


    dot net Training institute in Noida


    Dot net Training institute in Ghaziabad


    dot net training institute in Vaishali


    dot net Training institute in Vasundhara


    dot net Training institute in Delhi South Ex

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. Really a good technical site,keep on writing good stuffs
    dot net training in chennai

    ReplyDelete


  6. very useful info, and please keep updating........

    ReplyDelete
  7. This blog gives very important info about .Net Thanks for sharing
    Dot Net Online Training Bangalore

    ReplyDelete
  8. Superb. I really enjoyed very much with this article here. Really it is an amazing article I had ever read. I hope it will help a lot for all. Thank you so much for this amazing posts and please keep update like this. best sap simple finance online training institute in hyderabad

    ReplyDelete
  9. Nice tutorial. Thanks for sharing the valuable information. it’s really helpful. Who want to learn this blog most helpful. Keep sharing on updated tutorials…
    Best Devops training in sholinganallur
    Devops training in velachery
    Devops training in annanagar
    Devops training in tambaram

    ReplyDelete
  10. Great thoughts you got there, believe I may possibly try just some of it throughout my daily life.
    python course institute in bangalore
    python Course in bangalore
    python training institute in bangalore

    ReplyDelete
  11. It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me...
    python Online training in chennai
    python Online training in bangalore
    python interview question and answers

    ReplyDelete
  12. After reading your post I understood that last week was with full of surprises and happiness for you. Congratz! Even though the website is work related, you can update small events in your life and share your happiness with us too.
    Java training in Chennai | Java training institute in Chennai | Java course in Chennai

    Java training in Bangalore | Java training in Electronic city

    Java training in Bangalore | Java training in Marathahalli

    Java training in Bangalore | Java training in Btm layout

    ReplyDelete
  13. This is most informative and also this post most user friendly and super navigation to all posts... Thank you so much for giving this information to me.. 
    Online DevOps Certification Course - Gangboard
    Best Devops Training institute in Chennai

    ReplyDelete
  14. This is an awesome post.Really very informative and creative contents. These concept is a good way to enhance the knowledge.I like it and help me to development very well.Thank you for this brief explanation and very nice information.Well, got a good knowledge.

    rpa training in bangalore | best rpa training in bangalore | rpa course in bangalore | rpa training institute in bangalore | rpa training in bangalore | rpa online training | rpa training in Chennai

    ReplyDelete

  15. When I initially commented, I clicked the “Notify me when new comments are added” checkbox and now each time a comment is added I get several emails with the same comment. Is there any way you can remove people from that service? Thanks.

    AWS Training in Bangalore | Best Amazon Web Services Training in Bangalore
    Advanced Amazon Web Services Training in Pune | Best AWS Training in Pune
    AWS Online Training | Best Online AWS Certification Course - Gangboard
    Best Top 110 plus AWS Interview Question and Answers

    ReplyDelete
  16. Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.

    rpa training in chennai
    Best rpa training in bangalore
    rpa course in bangalore
    rpa training in marathahalli
    rpa training in btm
    best rpa training in chennai

    ReplyDelete
  17. This is an best post. It is Really very informative concept.I like it and help me to development very well.Thanks alot for this brief explanation and very nice information.C#.NET Course In Hyderabad

    ReplyDelete