Snip2Code is shutting down.
It has been quite a ride, since 2013 when we launched our first prototype: thanks to the effort of you guys we collected more than 3 million snippets!
We are very proud to help all our users to be more efficient in their jobs, and to be the central point to share programming knowledge for everyone.
Our basic service is free, so we always survived on our own resources to give you Snip2Code.
Unfortunately, we are no more in the financial position to sustain this effort, and therefore we are announcing here our permanent shut down,
which will take place on August 1st, 2020.
Please save your private snippets using our backup function in the settings, here.
IF YOU WANT TO SAVE SNIP2CODE, PLEASE CONSIDER DOING A DONATION!
This will allow us to pay for the servers and the infrastructure. If you want to donate, Contact Us!
by
0
8
11k
75
Top 1% !
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