Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Encrypt text

Encrypt text

Scheduled Pinned Locked Moved ASP.NET
csharpasp-nettutorialquestion
8 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kavinnagarajan
    wrote on last edited by
    #1

    How to encrypt and decrypt text in asp.net? I can able to encrypt text from textbox but i don't how to decrypt it.

    C L 2 Replies Last reply
    0
    • K kavinnagarajan

      How to encrypt and decrypt text in asp.net? I can able to encrypt text from textbox but i don't how to decrypt it.

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      Well, if you encrypted it, you should know how to decrypt it, you reverse the same method. Hashes like MD5 are not encryption, and cannot be reversed. Look at the Cryptography namespace to see what your options are.

      Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

      K 2 Replies Last reply
      0
      • C Christian Graus

        Well, if you encrypted it, you should know how to decrypt it, you reverse the same method. Hashes like MD5 are not encryption, and cannot be reversed. Look at the Cryptography namespace to see what your options are.

        Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

        K Offline
        K Offline
        kavinnagarajan
        wrote on last edited by
        #3

        Is there any method to encrypt & decrypt text?

        C 1 Reply Last reply
        0
        • K kavinnagarajan

          How to encrypt and decrypt text in asp.net? I can able to encrypt text from textbox but i don't how to decrypt it.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Hi Refer to the below article for encryption/decryption Cryptography 101 for the .NET Framework[^]

          Regards Aman Bhullar www.arlivesupport.com[^]

          K 1 Reply Last reply
          0
          • K kavinnagarajan

            Is there any method to encrypt & decrypt text?

            C Offline
            C Offline
            Christian Graus
            wrote on last edited by
            #5

            There are many. They are all in that namespace I told you to look into.

            Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

            1 Reply Last reply
            0
            • C Christian Graus

              Well, if you encrypted it, you should know how to decrypt it, you reverse the same method. Hashes like MD5 are not encryption, and cannot be reversed. Look at the Cryptography namespace to see what your options are.

              Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

              K Offline
              K Offline
              kavinnagarajan
              wrote on last edited by
              #6

              I tried below code for encryption

                  MemoryStream ms = new MemoryStream();
                  Rijndael alg = Rijndael.Create();
                  System.Text.Encoding enc = System.Text.Encoding.ASCII;
                  byte\[\] input = enc.GetBytes(TextBox1.Text.ToString());
                  CryptoStream cs = new CryptoStream(ms, alg.CreateEncryptor(), CryptoStreamMode.Write);
                  cs.Write(input, 0, input.Length);
                  cs.Close();
                  byte\[\] encrypt = ms.ToArray();
                  TextBox2.Text = enc.GetString(encrypt);
              

              its work fine. to decrypt this i used the below code

                      MemoryStream ms = new MemoryStream();
                      Rijndael alg = Rijndael.Create();
                      System.Text.Encoding enc = System.Text.Encoding.ASCII;
                      byte\[\] input = enc.GetBytes(TextBox2.Text.ToString());
                      CryptoStream cs = new CryptoStream(ms, alg.CreateDecryptor(), CryptoStreamMode.Write);
                      cs.Write(input, 0, input.Length);
                      cs.Close();
                      byte\[\] decrypt = ms.ToArray();
                      TextBox3.Text = enc.GetString(decrypt);
              

              but this throws exception as Padding is invalid and cannot be removed. how can i come out of this problem?

              P 1 Reply Last reply
              0
              • K kavinnagarajan

                I tried below code for encryption

                    MemoryStream ms = new MemoryStream();
                    Rijndael alg = Rijndael.Create();
                    System.Text.Encoding enc = System.Text.Encoding.ASCII;
                    byte\[\] input = enc.GetBytes(TextBox1.Text.ToString());
                    CryptoStream cs = new CryptoStream(ms, alg.CreateEncryptor(), CryptoStreamMode.Write);
                    cs.Write(input, 0, input.Length);
                    cs.Close();
                    byte\[\] encrypt = ms.ToArray();
                    TextBox2.Text = enc.GetString(encrypt);
                

                its work fine. to decrypt this i used the below code

                        MemoryStream ms = new MemoryStream();
                        Rijndael alg = Rijndael.Create();
                        System.Text.Encoding enc = System.Text.Encoding.ASCII;
                        byte\[\] input = enc.GetBytes(TextBox2.Text.ToString());
                        CryptoStream cs = new CryptoStream(ms, alg.CreateDecryptor(), CryptoStreamMode.Write);
                        cs.Write(input, 0, input.Length);
                        cs.Close();
                        byte\[\] decrypt = ms.ToArray();
                        TextBox3.Text = enc.GetString(decrypt);
                

                but this throws exception as Padding is invalid and cannot be removed. how can i come out of this problem?

                P Offline
                P Offline
                PauloCastilho
                wrote on last edited by
                #7

                Use this class...

                public class Rijndael
                {
                public static string Encrypt(string text, string key)
                {
                RijndaelManaged rijndael = new RijndaelManaged();

                    byte\[\] plainText = System.Text.Encoding.Unicode.GetBytes(text);
                
                    byte\[\] salt = Encoding.ASCII.GetBytes(key.Length.ToString());
                
                    PasswordDeriveBytes secretKey = new PasswordDeriveBytes(key, salt);
                
                    ICryptoTransform Encryptor = rijndael.CreateEncryptor(secretKey.GetBytes(32), secretKey.GetBytes(16));
                
                    MemoryStream memoryStream = new MemoryStream();
                
                    CryptoStream cryptoStream = new CryptoStream(memoryStream, Encryptor, CryptoStreamMode.Write);
                
                    cryptoStream.Write(plainText, 0, plainText.Length);
                
                    cryptoStream.FlushFinalBlock();
                
                    byte\[\] cipherBytes = memoryStream.ToArray();
                
                    memoryStream.Close();
                    cryptoStream.Close();
                
                    return Convert.ToBase64String(cipherBytes);
                }
                
                public static string Decrypt(string text, string key)
                {
                    RijndaelManaged rijndael = new RijndaelManaged();
                
                    byte\[\] encryptedText = Convert.FromBase64String(text);
                
                    byte\[\] salt = Encoding.ASCII.GetBytes(key.Length.ToString());
                
                    PasswordDeriveBytes secretKey = new PasswordDeriveBytes(key, salt);
                
                    ICryptoTransform Decryptor = rijndael.CreateDecryptor(secretKey.GetBytes(32), secretKey.GetBytes(16));
                
                    MemoryStream memoryStream = new MemoryStream(encryptedText);
                
                    CryptoStream cryptoStream = new CryptoStream(memoryStream, Decryptor, CryptoStreamMode.Read);
                
                    byte\[\] plainText = new byte\[encryptedText.Length\];
                
                    int DecryptedCount = cryptoStream.Read(plainText, 0, plainText.Length);
                
                    memoryStream.Close();
                    cryptoStream.Close();
                
                    return Encoding.Unicode.GetString(plainText, 0, DecryptedCount);
                }
                

                }

                e.g.

                TextBox2.Text = Rijndael.Encrypt(TextBox1.Text, "SecurityKey");
                TextBox3.Text = Rijndael.Decrypt(TextBox2.Text, "SecurityKey");

                modified on Wednesday, August 26, 2009 10:46 AM

                1 Reply Last reply
                0
                • L Lost User

                  Hi Refer to the below article for encryption/decryption Cryptography 101 for the .NET Framework[^]

                  Regards Aman Bhullar www.arlivesupport.com[^]

                  K Offline
                  K Offline
                  kavinnagarajan
                  wrote on last edited by
                  #8

                  Thanks a lot its working supereb. Thanks............

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups