by
0
9
10k
75
Top 1% !
Popular
Famous
Pearl of Wisdom
Tagged
Easy-to-find
Specified
Refined
OpenSource
Popularity: 448th place

Published on:

.NETFramework4.0
Architecturex86_64
ClassNameEncryptor
Extension.cs
InputName0strToEncrypt
InputType0string
LanguageCSharp
LicenseMIT_X11
MethodNameEncrypt
OSWindows
OutputTypestring

How to encrypt and decrypt strings in C#

This class can be used to encrypt and decrypt the given string. In the current implementation the passwrod is directly saved inside the code; depending on the infrastructure and architecture of the code sometimes it can be passed as parameter
Copy Embed Code
<iframe id="embedFrame" style="width:600px; height:300px;"
src="https://www.snip2code.com/Embed/25614/How-to-encrypt-and-decrypt-strings-in-C-?startLine=0"></iframe>
Click on the embed code to copy it into your clipboard Width Height
Leave empty to retrieve all the content Start End
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Security.Cryptography; public static class Encryptor { static private string _key = "myVeryStrongPsw"; /// <summary> /// Encrypt the given string using the default key. /// </summary> /// <param name="strToEncrypt">The string to be encrypted.</param> /// <returns>The encrypted string.</returns> public static string Encrypt(string strToEncrypt) { try { return Encrypt(strToEncrypt, _key); } catch (Exception ex) { return "Wrong Input. " + ex.Message; } } /// <summary> /// Decrypt the given string using the default key. /// </summary> /// <param name="strEncrypted">The string to be decrypted.</param> /// <returns>The decrypted string.</returns> public static string Decrypt(string strEncrypted) { try { return Decrypt(strEncrypted, _key); } catch (Exception ex) { return "Wrong Input. " + ex.Message; } } /// <summary> /// Encrypt the given string using the specified key. /// </summary> /// <param name="strToEncrypt">The string to be encrypted.</param> /// <param name="strKey">The encryption key.</param> /// <returns>The encrypted string.</returns> public static string Encrypt(string strToEncrypt, string strKey) { try { TripleDESCryptoServiceProvider objDESCrypto = new TripleDESCryptoServiceProvider(); MD5CryptoServiceProvider objHashMD5 = new MD5CryptoServiceProvider(); byte[] byteHash, byteBuff; string strTempKey = strKey; byteHash = objHashMD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(strTempKey)); objHashMD5 = null; objDESCrypto.Key = byteHash; objDESCrypto.Mode = CipherMode.ECB; //CBC, CFB byteBuff = ASCIIEncoding.ASCII.GetBytes(strToEncrypt); return Convert.ToBase64String(objDESCrypto.CreateEncryptor(). TransformFinalBlock(byteBuff, 0, byteBuff.Length)); } catch (Exception ex) { return "Wrong Input. " + ex.Message; } } /// <summary> /// Decrypt the given string using the specified key. /// </summary> /// <param name="strEncrypted">The string to be decrypted.</param> /// <param name="strKey">The decryption key.</param> /// <returns>The decrypted string.</returns> public static string Decrypt(string strEncrypted, string strKey) { try { TripleDESCryptoServiceProvider objDESCrypto = new TripleDESCryptoServiceProvider(); MD5CryptoServiceProvider objHashMD5 = new MD5CryptoServiceProvider(); byte[] byteHash, byteBuff; string strTempKey = strKey; byteHash = objHashMD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(strTempKey)); objHashMD5 = null; objDESCrypto.Key = byteHash; objDESCrypto.Mode = CipherMode.ECB; //CBC, CFB byteBuff = Convert.FromBase64String(strEncrypted); string strDecrypted = ASCIIEncoding.ASCII.GetString (objDESCrypto.CreateDecryptor().TransformFinalBlock (byteBuff, 0, byteBuff.Length)); objDESCrypto = null; return strDecrypted; } catch (Exception ex) { return "Wrong Input. " + ex.Message; } } }
If you want to be updated about similar snippets, Sign in and follow our Channels

blog comments powered by Disqus