You must Sign In to post a response.

Encrypt/Decrypt the password in C# .net

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


Comments

Author: Amit Shah16 Dec 2008 Member Level: Gold   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
{
///


/// Represets the CryptorEngine for the .
///

public class CryptorEngine
{
#region Public Methods

///
/// Encrypt a string using dual encryption method. Returns a encrypted text.
///

/// string to be encrypted
/// use hashing? send to for extra secirity
/// Returns encrypted string.
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;
}
}

///
/// DeCrypt a string using dual encryption method. Return a DeCrypted clear string
///

/// encrypted string
/// Did you use hashing to encrypt this data? pass true is yes
/// Returns decrypted text.
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
}
}

Author: Jayesh16 Dec 2008 Member Level: Gold   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" %>





Untitled Page



















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

Author: sabitha17 Dec 2008 Member Level: Bronze   Points : 1

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

-sabitha