Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 21st, 2005, 01:39 AM
Registered User
 
Join Date: Dec 2005
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to munnu Send a message via Yahoo to munnu
Default 128-bit encryption

How to write a C# programme which encrypt and decrypt the given password using 128-bit encryption.



 
Old December 28th, 2005, 06:28 AM
Registered User
 
Join Date: Dec 2005
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to munnu Send a message via Yahoo to munnu
Default

I could find out the way to solve this. You can just create a class file and paste this onto it.

Create a Login Page with the user id and password, a submit button.
On click of a submit button, put the following code...

//Declare this ..
EnDecrypt128 oEnDe = new EnDecrypt128();

string EncryptPwdString;
string DecryptPwdString;
string key="&%&$*($@!!#()*^%^%$!@s*(#"; // This key can be anything.
EncryptPwdString = oEnDe.EncryptString(txtPwd.Text, key);
DecryptPwdString = oEnDe.DecryptString(EncryptPwdString, key);

// EnDecrypt128.cs -- This is the class name.

public string EncryptString(string InputText, string Password)
{
// "Password" string variable is nothing but the key(your secret key) value which is sent from the front end.
// "InputText" string variable is the actual password sent from the login page.

// We are now going to create an instance of the
// Rihndael class.
RijndaelManaged RijndaelCipher = new RijndaelManaged();

// First we need to turn the input strings into a byte array.
byte[] PlainText = System.Text.Encoding.Unicode.GetBytes(InputText);

// We are using Salt to make it harder to guess our key
// using a dictionary attack.
byte[] Salt = Encoding.ASCII.GetBytes(Password.Length.ToString() );

// The (Secret Key) will be generated from the specified
// password and Salt.

//PasswordDeriveBytes -- It Derives a key from a password
PasswordDeriveBytes SecretKey = new PasswordDeriveBytes(Password, Salt);

// Create a encryptor from the existing SecretKey bytes.
// We use 32 bytes for the secret key
// (the default Rijndael key length is 256 bit = 32 bytes) and
// then 16 bytes for the IV (initialization vector),
// (the default Rijndael IV length is 128 bit = 16 bytes)

ICryptoTransform Encryptor = RijndaelCipher.CreateEncryptor(SecretKey.GetBytes( 16), SecretKey.GetBytes(16));

// Create a MemoryStream that is going to hold the encrypted bytes
MemoryStream memoryStream = new MemoryStream();

// Create a CryptoStream through which we are going to be processing our data.
// CryptoStreamMode.Write means that we are going to be writing data
// to the stream and the output will be written in the MemoryStream
// we have provided. (always use write mode for encryption)
CryptoStream cryptoStream = new CryptoStream(memoryStream, Encryptor, CryptoStreamMode.Write);

// Start the encryption process.
cryptoStream.Write(PlainText, 0, PlainText.Length);

// Finish encrypting.
cryptoStream.FlushFinalBlock();

// Convert our encrypted data from a memoryStream into a byte array.
byte[] CipherBytes = memoryStream.ToArray();

// Close both streams.
memoryStream.Close();
cryptoStream.Close();

// Convert encrypted data into a base64-encoded string.
// A common mistake would be to use an Encoding class for that.
// It does not work, because not all byte values can be
// represented by characters. We are going to be using Base64 encoding
// That is designed exactly for what we are trying to do.
string EncryptedData = Convert.ToBase64String(CipherBytes);

// Return encrypted string.
return EncryptedData;
}
--------------------------------------------------------------------

public string DecryptString(string InputText, string Password)
{
RijndaelManaged RijndaelCipher = new RijndaelManaged();

byte[] EncryptedData = Convert.FromBase64String(InputText);
byte[] Salt = Encoding.ASCII.GetBytes(Password.Length.ToString() );

PasswordDeriveBytes SecretKey = new PasswordDeriveBytes(Password, Salt);

// Create a decryptor from the existing SecretKey bytes.
ICryptoTransform Decryptor = RijndaelCipher.CreateDecryptor(SecretKey.GetBytes( 16), SecretKey.GetBytes(16));

MemoryStream memoryStream = new MemoryStream(EncryptedData);

// Create a CryptoStream. (always use Read mode for decryption).
CryptoStream cryptoStream = new CryptoStream(memoryStream, Decryptor, CryptoStreamMode.Read);

// Since at this point we don't know what the size of decrypted data
// will be, allocate the buffer long enough to hold EncryptedData;
// DecryptedData is never longer than EncryptedData.
byte[] PlainText = new byte[EncryptedData.Length];

// Start decrypting.
int DecryptedCount = cryptoStream.Read(PlainText, 0, PlainText.Length);

memoryStream.Close();
cryptoStream.Close();

// Convert decrypted data into a string.
string DecryptedData = Encoding.Unicode.GetString(PlainText, 0, DecryptedCount);

// Return decrypted string.
return DecryptedData; }


Note: Its taken from some of the article found in google and customised to my requirement.

- Have a nice time...






Similar Threads
Thread Thread Starter Forum Replies Last Post
64 Bit - Issue in 64 bit IIS calling Win32 API Hubman General .NET 1 August 24th, 2006 09:19 AM
Code on p.128 php4ever BOOK: Professional PHP 5 ISBN: 978-0-7645-7282-1 0 August 21st, 2005 04:05 AM
p.128 ch.4 createreviews.php ERROR wildroosta BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 1 September 12th, 2004 09:17 AM
A 128 bit encryption login page pajnaidoo ASP.NET 1.0 and 1.1 Basics 0 March 2nd, 2004 11:07 AM
Question from page 128: Why the double cast? bobby BOOK: Beginning Java 2 8 August 19th, 2003 03:43 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.