Introduction
Encrypt and decrypting are essential data with C# play. For security purposes, we are storing some valuable things in Encrypt format.

I wrote two code examples for best practices for encrypting a string; the second is again Decrypting the value.
Microsoft has a simple method to convert strings in Encrypt and Decrypt at any time.
Encrypt and Decrypt Simple Method
string encryptedString = SomeStaticClass.Encrypt(sourceString);
string decryptedString = SomeStaticClass.Decrypt(encryptedString);
To use the code sample, you can copy the CryptorEngine to your project, start playing, copy method bodies, and paste them into your application projects.
I wrote a class to access the Encrypt and Decrypt Method. Because I wanted to use this method in many places, and this made my programming simple.

Encrypt and Decrypt Using Class
public string DecryptString(string encrString)
{
byte[] b;
string decrypted;
try
{
b = Convert.FromBase64String(encrString);
decrypted = System.Text.ASCIIEncoding.ASCII.GetString(b);
}
catch (FormatException fe)
{
decrypted = "";
}
return decrypted;
}
public string EnryptString(string strEncrypted)
{
byte[] b = System.Text.ASCIIEncoding.ASCII.GetBytes(strEncrypted);
string encrypted = Convert.ToBase64String(b);
return encrypted;
}
Still, The above code is not enough for me because I want to do something with my choice. I have created two method classes. Then I implemented the code with a specific charter to use in Encrypt method.

Encrypt and Decrypt with Character Choice
public string encrypt(string encryptString)
{
string EncryptionKey = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
byte[] clearBytes = Encoding.Unicode.GetBytes(encryptString);
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();
}
encryptString = Convert.ToBase64String(ms.ToArray());
}
}
return encryptString;
}
public string Decrypt(string cipherText)
{
string EncryptionKey = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
cipherText = cipherText.Replace(" ", "+");
byte[] cipherBytes = Convert.FromBase64String(cipherText);
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();
}
cipherText = Encoding.Unicode.GetString(ms.ToArray());
}
}
return cipherText;
}
At last, the above code is perfect for making such a good application, and now I'm filling well because I did what I wanted.
Conclusion
This article taught us Encrypt and Decrypt a String in ASP.NET with code examples.
Thanks for more code visit stupidcodes.com

khetha mchunuPosted Dec 8, 2020, 3:02 AM
Hey, the code works fine when I am encrypting but throw an error when I decrypt due to the length.
Subhishek ShuklaPosted Nov 6, 2020, 5:36 AM
I m using storing GUID as a string i m getting issue-- "FormatException: The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters."
none nonePosted Aug 1, 2019, 1:50 AM
Why this code 'cipherText = cipherText.Replace(" ", "+");' is needed (line 27) ?
vishal adyanthayaPosted Jul 25, 2019, 3:53 AM
Great stuff, will try it out .. was looking at the bytes == Ivan Medvedev, I guess any other text would do too, I guess any other text would also do.
Aswathy gopanPosted Jul 9, 2019, 11:50 PM
I used this code but am getting an exception saying padding is invalid in the decryption code ..why so?? please rectify it.
Aakash NPosted Jan 16, 2019, 9:50 AM
Really useful for me. Great Post!!
KOTRA RAGHAVENDRAPosted Oct 23, 2018, 5:55 AM
Is there any opening pls notify me in .net as a fresher location any where
Dharmraj ThakurPosted Feb 22, 2018, 11:35 PM
Thanks for sharing...
Mike PattynPosted Feb 1, 2018, 4:17 PM
Thanks alot :) for this
Sunil AcharyaPosted Jun 7, 2016, 10:31 AM
It's obvious increase the size of data but it secures your data.
ashwini patilPosted Jun 7, 2016, 2:38 AM
Good share...How about application performance as encrypted data size will be more than original data size?
Leena DeepPosted Jul 8, 2015, 2:48 AM
Its really useful informative content.Thanks for shring about dotnet..http://www.trainingintambaram.in/dot-net-training-in-chennai.html
Leena DeepPosted Jul 8, 2015, 2:48 AM
Its really useful informative content.Thanks for shring about dotnet..
RakeshPosted Jul 1, 2015, 2:51 AM
Good one
Santhakumar MunuswamyPosted Jun 30, 2015, 2:38 PM
Good one
Upendra Pratap ShahiPosted Jun 30, 2015, 12:40 AM
good one