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.Introduction
Microsoft has a simple method to convert strings in Encrypt and Decrypt at any time.
Encrypt and Decrypt Simple Method
stringencryptedString=SomeStaticClass.Encrypt(sourceString);stringdecryptedString=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
publicstringDecryptString(stringencrString){byte[]b;stringdecrypted;try{b=Convert.FromBase64String(encrString);decrypted=System.Text.ASCIIEncoding.ASCII.GetString(b);}catch(FormatExceptionfe){decrypted="";}returndecrypted;}publicstringEnryptString(stringstrEncrypted){byte[]b=System.Text.ASCIIEncoding.ASCII.GetBytes(strEncrypted);stringencrypted=Convert.ToBase64String(b);returnencrypted;}
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
publicstringencrypt(stringencryptString){stringEncryptionKey="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";byte[]clearBytes=Encoding.Unicode.GetBytes(encryptString);using(Aesencryptor=Aes.Create()){Rfc2898DeriveBytespdb=newRfc2898DeriveBytes(EncryptionKey,newbyte[]{0x49,0x76,0x61,0x6e,0x20,0x4d,0x65,0x64,0x76,0x65,0x64,0x65,0x76});encryptor.Key=pdb.GetBytes(32);encryptor.IV=pdb.GetBytes(16);using(MemoryStreamms=newMemoryStream()){using(CryptoStreamcs=newCryptoStream(ms,encryptor.CreateEncryptor(),CryptoStreamMode.Write)){cs.Write(clearBytes,0,clearBytes.Length);cs.Close();}encryptString=Convert.ToBase64String(ms.ToArray());}}returnencryptString;}publicstringDecrypt(stringcipherText){stringEncryptionKey="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";cipherText=cipherText.Replace("","+");byte[]cipherBytes=Convert.FromBase64String(cipherText);using(Aesencryptor=Aes.Create()){Rfc2898DeriveBytespdb=newRfc2898DeriveBytes(EncryptionKey,newbyte[]{0x49,0x76,0x61,0x6e,0x20,0x4d,0x65,0x64,0x76,0x65,0x64,0x65,0x76});encryptor.Key=pdb.GetBytes(32);encryptor.IV=pdb.GetBytes(16);using(MemoryStreamms=newMemoryStream()){using(CryptoStreamcs=newCryptoStream(ms,encryptor.CreateDecryptor(),CryptoStreamMode.Write)){cs.Write(cipherBytes,0,cipherBytes.Length);cs.Close();}cipherText=Encoding.Unicode.GetString(ms.ToArray());}}returncipherText;}
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 usEncrypt and Decrypt a String in ASP.NET with code examples.
Thanks for more code visit stupidcodes.com