C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » Encryption »

How to encrypt a string using c#


Posted Date: 30 Apr 2008    Resource Type: Code Snippets    Category: Encryption
Author: narasimhaMember Level: Gold    
Rating: 1 out of 5Points: 15



The following cose sample shows how to encrypt a string using c#
private string encryptString(string strToEncrypt)
{
System.Text.UTF8Encoding ue = new System.Text.UTF8Encoding();
byte[] bytes = ue.GetBytes(strToEncrypt);

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

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

for(int i=0;i{
hashString += Convert.ToString(hashBytes[i],16).PadLeft(2,'0');
}

return hashString.PadLeft(32,'0');
}



Responses

Author: Kapil Dhawan    17 Jun 2008Member Level: Gold   Points : 2
Hello
Nice piece of code
Thanks for sharing your knowledge with us.
I hope to see more good code from your side
This code will help lots of guys
Thanks to you
Regards,
Kapil








Author: Jelte Verbree    19 Sep 2008Member Level: Bronze   Points : 2
Hi,

Great code it was very useful to me, but I think that here is missing something in this code

for(int i=0;i
{
hashString += Convert.ToString(hashBytes[i],16).PadLeft(2,'0');
}

return hashString.PadLeft(32,'0');
}

does it have to be something like this?

for(int i=0; ...missing... ; i++)
{
code
}

Correct me if i'm wrong ;)


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Password Encryption - the Simple Way
Previous Resource: How to encript and decript a password
Return to Discussion Resource Index
Post New Resource
Category: Encryption


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use