Encrypt text
-
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.
-
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.
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.
-
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.
Is there any method to encrypt & decrypt text?
-
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.
Hi Refer to the below article for encryption/decryption Cryptography 101 for the .NET Framework[^]
Regards Aman Bhullar www.arlivesupport.com[^]
-
Is there any method to encrypt & decrypt text?
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.
-
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.
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?
-
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?
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
-
Hi Refer to the below article for encryption/decryption Cryptography 101 for the .NET Framework[^]
Regards Aman Bhullar www.arlivesupport.com[^]
Thanks a lot its working supereb. Thanks............