dotnetspider.com


 


TutorialsForumResourcesReviewsJobsInterviewVideosCommunitiesProjectsTraining

Subscribe to Subscribers


Online MembersRavindran
abhishek
chirag jaguwala
R.Jaya kumar (JK)
More...




Forums » .NET » ASP.NET »

Encrypt/Decrypt the password in C# .net


Posted Date: 16 Dec 2008      Posted By:: sabitha     Member Level: Bronze    Member Rank: 0     Points: 1   Responses: 3



Hi Friends,
My task is( using byte [])
1)Taking string value from textbox and
2)converting each character into ASCII and adding plus 2 to that individual ASCII value for individual character.
3)afterwards converting that whole ASCII value into a new string.
4)and storing the new string into one document.

and same steps for decrypting means "converting new string to old string"

Pls help me The whole code in ASP C# .Net
I want User interface and coding
-sabitha





Responses

#329870    Author:       Member Level: Gold      Member Rank: 0     Date: 16/Dec/2008   Rating: 2 out of 52 out of 5     Points: 6

Use following Class for encrypting and decrypting!!!!
---------------------------------------------------------

Enjoy Code!!!!!

using System;
using System.Configuration;
using System.Security.Cryptography;
using System.Text;

namespace DCViewer.Cryptoghaphy
{
/// <summary>
/// Represets the CryptorEngine for the <see cref="DCViewer"/>.
/// </summary>
public class CryptorEngine
{
#region Public Methods

/// <summary>
/// Encrypt a string using dual encryption method. Returns a encrypted text.
/// </summary>
/// <param name="toEncrypt">string to be encrypted</param>
/// <param name="useHashing">use hashing? send to for extra secirity</param>
/// <returns>Returns encrypted string.</returns>
public static string Encrypt(string toEncrypt, bool useHashing)
{
try
{
byte[] keyArray;
byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);

System.Configuration.AppSettingsReader settingsReader = new AppSettingsReader();
// Get the key from config file
string key = (string)settingsReader.GetValue("SecurityKey", typeof(String));
//System.Windows.Forms.MessageBox.Show(key);
if (useHashing)
{
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
hashmd5.Clear();
}
else
keyArray = UTF8Encoding.UTF8.GetBytes(key);

TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
tdes.Key = keyArray;
tdes.Mode = CipherMode.ECB;
tdes.Padding = PaddingMode.PKCS7;

ICryptoTransform cTransform = tdes.CreateEncryptor();
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
tdes.Clear();
return Convert.ToBase64String(resultArray, 0, resultArray.Length);
}
catch (Exception)
{
throw;
}
}

/// <summary>
/// DeCrypt a string using dual encryption method. Return a DeCrypted clear string
/// </summary>
/// <param name="cipherString">encrypted string</param>
/// <param name="useHashing">Did you use hashing to encrypt this data? pass true is yes</param>
/// <returns>Returns decrypted text.</returns>
public static string Decrypt(string cipherString, bool useHashing)
{
try
{
byte[] keyArray;
byte[] toEncryptArray = Convert.FromBase64String(cipherString);

System.Configuration.AppSettingsReader settingsReader = new AppSettingsReader();
//Get your key from config file to open the lock!
string key = (string)settingsReader.GetValue("SecurityKey", typeof(String));

if (useHashing)
{
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
hashmd5.Clear();
}
else
keyArray = UTF8Encoding.UTF8.GetBytes(key);

TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
tdes.Key = keyArray;
tdes.Mode = CipherMode.ECB;
tdes.Padding = PaddingMode.PKCS7;

ICryptoTransform cTransform = tdes.CreateDecryptor();
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

tdes.Clear();
return UTF8Encoding.UTF8.GetString(resultArray);
}
catch (Exception)
{
throw;
}
}

#endregion
}
}



#329986    Author:       Member Level: Gold      Member Rank: 645     Date: 16/Dec/2008   Rating: 2 out of 52 out of 5     Points: 6

Since your encryption algorithm is very simple we can implement it directly in our code.

The Interface
-------------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server" Style="z-index: 100; left: 72px; position: absolute;
top: 115px"></asp:TextBox>
<asp:Button ID="ButtonEncrypt" runat="server" OnClick="ButtonEncrypt_Click" Style="z-index: 101;
left: 158px; position: absolute; top: 144px" Text="Encrypt" />
<asp:Label ID="lblCypher" runat="server" Style="z-index: 102; left: 77px; position: absolute;
top: 180px" Text="Label"></asp:Label>
<asp:Label ID="lblPlain" runat="server" Style="z-index: 105; left: 493px; position: absolute;
top: 148px" Text="Label"></asp:Label>
<asp:Button ID="ButtonDecrypt" runat="server" Style="z-index: 104; left: 461px; position: absolute;
top: 118px" Text="Decrypt" OnClick="ButtonDecrypt_Click" />

</div>
</form>
</body>
</html>





And the code follows.
It is too simple that it doesnt require any explanation.

------------
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void ButtonEncrypt_Click(object sender, EventArgs e)
{
string s = TextBox1.Text,Cypher="";
for (int i = 0; i < s.Length; i++)
{
char c = (char)(s[i] + 2);
Cypher = Cypher + new string(c, 1);
}
lblCypher.Text = Cypher;
}
protected void ButtonDecrypt_Click(object sender, EventArgs e)
{
string s = lblCypher.Text, PlainText= "";
for (int i = 0; i < s.Length; i++)
{
char c = (char)(s[i] - 2);
PlainText = PlainText + new string(c, 1);
}
lblPlain.Text = PlainText;

}
}


Hope this will help you.
If you still need any explanation please do let me know.

Regards
-Jayesh

Jayesh K.S.



#330362    Author:       Member Level: Bronze      Member Rank: 0     Date: 17/Dec/2008   Rating: 2 out of 52 out of 5     Points: 1

Hi Jayesh
thank u very much.
can we do this using byte array,if so pls tell me

-sabitha







Post Reply

 This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.


Next : handler file
Previous : What is Appliction Audit Report?
Return to Discussion Forum
Post New Message
Category: ASP.NET

Related Messages

My Profile

Active Members
TodayLast 7 Daysmore...


Awards & Gifts


Email subscription
  • .NET Jobs
  • .NET Articles
  • .NET Forums
  • Articles Rss Feeds
    Forum Rss Feeds



    About Us    Trademark Disclaimer    Contact Us    Copyright    Privacy Policy    Terms Of Use    Revenue Sharing sites   Advertise   Talk to Tony John
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2012 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.