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...






Forums » .NET » ASP.NET »

Encryption-Decryption


Posted Date: 04 Nov 2009      Posted By: Preet      Member Level: Gold     Points: 1   Responses: 4



How do we encrypt and decrypt data in .net? Do we use some in-build libraries for that?
Please explain with example.





Responses

Author: Ravi    04 Nov 2009Member Level: SilverRating: 2 out of 52 out of 5     Points: 2

Hello,

Please find below the methods for encrypt and decrypt.
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Security.Cryptography;
using System.Text;
using System.IO;

namespace SQLDAL
{
/// <summary>
/// Summary description for Security.
/// </summary>
public class Security
{
public Security()
{
//
// TODO: Add constructor logic here
//
}

public static string EncryptText(string strText)
{
return Encrypt(strText, "TwasBrilligsjdhdtEgfbVog9vesHadTeaWithAMadHatter");
}

public static string DecryptText(string strText)
{
return Decrypt(strText, "TwasBrilligsjdhdtEgfbVog9vesHadTeaWithAMadHatter");
}

private static string Encrypt(string strText, string strEncrKey)
{
byte[] byKey;
byte[] IV = {0, 52, 86, 120, 144, 171, 205, 239};
try
{
byKey = System.Text.Encoding.UTF8.GetBytes((strEncrKey.Substring(1, 8)));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = Encoding.UTF8.GetBytes(strText);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
return Convert.ToBase64String(ms.ToArray());
}
catch (Exception ex)
{
return ex.Message;
}
}

private static string Decrypt(string strText, string sDecrKey)
{
byte[] byKey;
byte[] IV = {0, 52, 86, 120, 144, 171, 205, 239};
byte[] inputByteArray = new byte[strText.Length];
try
{
byKey = System.Text.Encoding.UTF8.GetBytes( (sDecrKey.Substring(1, 8)));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
inputByteArray = Convert.FromBase64String(strText);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
System.Text.Encoding encoding = System.Text.ASCIIEncoding.UTF8;
return encoding.GetString(ms.ToArray());
}
catch (Exception ex)
{
return ex.Message;
}
}

}
}

Regards,
Ravi



Author: Anil Kumar Pandey    04 Nov 2009Member Level: DiamondRating: 2 out of 52 out of 5     Points: 2

check this code for the same..


http://mannpandey.blogspot.com/2009/07/code-encryption-and-decryption-using.html


Thanks & Regards
Anil Kumar Pandey



Author: Deepika Haridas    04 Nov 2009Member Level: DiamondRating: 2 out of 52 out of 5     Points: 2

Hello,

You can check resources section for this.



Thanks & Regards,
Deepika
Editor

If U want to shine like a SUN..First U have to burn like the SUN!!
Need a Guide? Join my mentor program..



Author: Nikhil Gaur    04 Nov 2009Member Level: DiamondRating: 2 out of 52 out of 5     Points: 2

look at the following links

http://niksgaur.blogspot.com/2009/07/code-encryption-and-decryption-using.html

http://niksgaur.blogspot.com/2009/08/encryption-and-decryption-without-net.html

Join this campus group
http://www.dotnetspider.com/sites/637/-ecb-aspdotnet.aspx

Thanks & Regards
NIks
My Software and Web Development Experience



Post Reply
You must Sign In to post a response.
Next : Insert values
Previous : Dropdownlist+Sqldatasource+controlparameter
Return to Discussion Forum
Post New Message
Category: ASP.NET

Related Messages



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use