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;
        }

6 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