PDA

View Full Version : How to encrypt string in c#...



svr2112
09-29-2010, 11:18 PM
You can use following code for encryption

private string encryptString(string strToEncrypt)
{
System.Text.UTF8Encoding ue = new System.Text.UTF8Encoding();
byte[] bytes = ue.GetBytes(strToEncrypt);

// encrypt bytes
System.Security.Cryptography.MD5Crypto&#82*0; der md5 = new System.Security.Cryptography.MD5CryptoSe&#82*0; der();
byte[] hashBytes = md5.ComputeHash(bytes);

// Convert the encrypted bytes back to a string (base *6)
string hashString = "";

for(int i=0;i
{
hashString += Convert.ToString(hashBytes[i],*6).PadLef&#82*0;
}

return hashString.PadLeft(*2,'0');
}

_____________

megachithu
10-11-2010, 06:46 AM
if u dont want the user to be able to see the querystring, just use action=POST instead of get in ur form tag (by default action=GET). this would hide the querystring from the user. or you could write functions in js to encrypt and decrypt the request url. (use any of the encryption algos.) but i'd still s***est using post action
............................................................

malikinam
05-23-2012, 05:15 AM
i like this forum very much because i have get a lot of required data from this forum.
now i want to buy some net tools will provide me some tools or guide me about that???
thanks..

marrilaw
06-09-2012, 07:44 AM
You can use following code for encryption

private string encryptString(string strToEncrypt)
{
System.Text.UTF8Encoding ue = new System.Text.UTF8Encoding();
byte[] bytes = ue.GetBytes(strToEncrypt);





thanks

GenevieveKelly
11-27-2013, 05:41 AM
thanks for sharing and i am good with getting those all and by the way have got a fair clue towards the proper implementation of the same method

clevelandslim
09-06-2014, 07:05 AM
You can use following code for encryption

private string encryptString(string strToEncrypt)
{
System.Text.UTF8Encoding ue = new System.Text.UTF8Encoding();
byte[] bytes = ue.GetBytes(strToEncrypt);

// encrypt bytes
System.Security.Cryptography.MD5Crypto... der md5 = new System.Security.Cryptography.MD5CryptoSe... der();
byte[] hashBytes = md5.ComputeHash(bytes);

// Convert the encrypted bytes back to a string (base *6)
string hashString = "";

for(int i=0;i
{
hashString += Convert.ToString(hashBytes[i],*6).PadLef...
}

return hashString.PadLeft(*2,'0');
}

mat1
06-05-2015, 01:41 PM
thanks for sharing.. ideas are priceless on this forum, especially in the area of programming.

darrenbang
08-13-2015, 04:04 AM
using System;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Security.Cryptography;
namespace WindowsFormsApplication*
{
public partial class Form* : Form
{
public Form*()
{
InitializeComponent();
}
private void button*_Click(object sender, EventArgs e)
{
//You should not hard code the encryption key here
string EncryptionKey = "encryptionkey";
string eStr = passwordEncrypt("yourpassword", EncryptionKey);
MessageBox.Show(eStr);
string dStr = passwordDecrypt(eStr, EncryptionKey);
MessageBox.Show(dStr);
}
//Encrypting a string
public static string passwordEncrypt(string inText, string key)
{
byte[] bytesBuff = Encoding.Unicode.GetBytes(inText);
using (Aes aes = Aes.Create())
{
Rfc28*8DeriveBytes crypto = new Rfc28*8DeriveBytes(key, new byte[] { 0x4*, 0x76, 0x6*, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
aes.Key = crypto.GetBytes(*2);
aes.IV = crypto.GetBytes(*6);
using (MemoryStream mStream = new MemoryStream())
{
using (CryptoStream cStream = new CryptoStream(mStream, aes.CreateEncryptor(), CryptoStreamMode.Write))
{
cStream.Write(bytesBuff, 0, bytesBuff.Length);
cStream.Close();
}
inText = Convert.ToBase64String(mStream.ToArray());
}
}
return inText;
}
//Decrypting a string
public static string passwordDecrypt(string cryptTxt,string key)
{
cryptTxt = cryptTxt.Replace(" ", "+");
byte[] bytesBuff = Convert.FromBase64String(cryptTxt);
using (Aes aes = Aes.Create())
{
Rfc28*8DeriveBytes crypto = new Rfc28*8DeriveBytes(key, new byte[] { 0x4*, 0x76, 0x6*, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
aes.Key = crypto.GetBytes(*2);
aes.IV = crypto.GetBytes(*6);
using (MemoryStream mStream = new MemoryStream())
{
using (CryptoStream cStream = new CryptoStream(mStream, aes.CreateDecryptor(), CryptoStreamMode.Write))
{
cStream.Write(bytesBuff, 0, bytesBuff.Length);
cStream.Close();
}
cryptTxt = Encoding.Unicode.GetString(mStream.ToArray());
}
}
return cryptTxt;
}
}
}

Source........http://net-informations.com/q/faq/encrypt.html

Bang

kaufen
08-29-2015, 03:04 PM
First of all, your application needs an entrance to let a user in, missing the Main() method is a compile-time error.



static void Main(string[] args)
{
try
{
Console.WriteLine("Original String: ");
string originalString = Console.ReadLine();
string cryptedString = Encrypt(originalString);
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("\nEncrypt Result: {0}", cryptedString);
Console.WriteLine("Decrypt Result: {0}", Decrypt(cryptedString));
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("From: {0}.\nDetail: {*}", ex.Source, ex.Message);
}
finally
{
Console.ReadLine();
}
}

You can also make it as a Windows Forms application if you like, but this is not our key point. Let's go through the code. There are two ********-defined methods using DES, Encrypt and Decrypt, both receive a string and return another string. Let's take a look at the details. BTW, the Console.ReadLine method in the finally block aims to pause the screen.

Let's see the Encrypt method first:



/// <span class="code-SummaryComment"><summary></span>
/// Encrypt a string.
/// <span class="code-SummaryComment"></summary></span>
/// <span class="code-SummaryComment"><param name="originalString">The original string.</param></span>
/// <span class="code-SummaryComment"><returns>The encrypted string.</returns></span>
/// <span class="code-SummaryComment"><exception cref="ArgumentNullException">This exception will be </span>
/// thrown when the original string is null or empty.<span class="code-SummaryComment"></exception></span>
public static string Encrypt(string originalString)
{
if (String.IsNullOrEmpty(originalString))
{
throw new ArgumentNullException
("The string which needs to be encrypted can not be null.");
}
DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
MemoryStream memoryStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(memoryStream,
cryptoProvider.CreateEncryptor(bytes, bytes), CryptoStreamMode.Write);
StreamWriter writer = new StreamWriter(cryptoStream);
writer.Write(originalString);
writer.Flush();
cryptoStream.FlushFinalBlock();
writer.Flush();
return Convert.ToBase64String(memoryStream.GetBuffer(), 0, (int)memoryStream.Length);
}

First, you should verify whether the input string is valid. Then, you need to define an instance of the DESCryptoServiceProvider class and use its methods, and you need a stream to keep the result. In my sample, I used MemoryStream. If you want to store your result into a file directly, you should use FileStream, or something similar. The CryptoStream links data streams to cryptographic transformations, and there is a variable named bytes in its parameter. The first one is used as specified key, and the second one is used as initialization vector, both of them can use the same one. I'll show you how to define it. Then, we also need a StreamWriter to write the result to the stream and keep it. After that, we can return the result to the user.



static byte[] bytes = ASCIIEncoding.ASCII.GetBytes("ZeroCool");

The ASCIIEncoding class is in the System.Text namespace.

Okay, let's see the last method Decrypt.



/// <span class="code-SummaryComment"><summary></span>
/// Decrypt a crypted string.
/// <span class="code-SummaryComment"></summary></span>
/// <span class="code-SummaryComment"><param name="cryptedString">The crypted string.</param></span>
/// <span class="code-SummaryComment"><returns>The decrypted string.</returns></span>
/// <span class="code-SummaryComment"><exception cref="ArgumentNullException">This exception will be thrown </span>
/// when the crypted string is null or empty.<span class="code-SummaryComment"></exception></span>
public static string Decrypt(string cryptedString)
{
if (String.IsNullOrEmpty(cryptedString))
{
throw new ArgumentNullException
("The string which needs to be decrypted can not be null.");
}
DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
MemoryStream memoryStream = new MemoryStream
(Convert.FromBase64String(cryptedString));
CryptoStream cryptoStream = new CryptoStream(memoryStream,
cryptoProvider.CreateDecryptor(bytes, bytes), CryptoStreamMode.Read);
StreamReader reader = new StreamReader(cryptoStream);
return reader.ReadToEnd();
}

Like Encrypt, we need to verify the input string first, and use DescryptoServiceProvider, MemoryStream, CryptoStream and StreamReader to deal with our job. Obviously, the difference is cryptoProvider.CreateDecryptor in there, and when we want to encrypt a string, we use the cryptoProvider.CreateEncryptor method. Secondly, we use StreamReader when we decrypt an encrypted string.

Ok, that's all, let your code run!


http://www.codeproject.com/Articles/**5*8/Encrypt-Decrypt-String-using-DES-in-C