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 »

Encryption- Decryption Class


Posted Date: 22 Dec 2008    Resource Type: Code Snippets    Category: Encryption
Author: gokulMember Level: Silver    
Rating: 1 out of 5Points: 15



Hi Friends,
Here I have added a Encyrption Class which you can use in u r project for Encrypting - Decrypting parameters.




using System;
using System.Xml;
using System.Text;
using System.Security.Cryptography;

///
/// Summary description for converter
///
public class converter
{
public converter()
{
//
// TODO: Add constructor logic here
//
}

public static string Decrypt(string TextToBeDecrypted)
{
RijndaelManaged RijndaelCipher = new RijndaelManaged();

string Password = "CSC";
string DecryptedData;

try
{
byte[] EncryptedData = Convert.FromBase64String(TextToBeDecrypted);

byte[] Salt = Encoding.ASCII.GetBytes(Password.Length.ToString());
//Making of the key for decryption
PasswordDeriveBytes SecretKey = new PasswordDeriveBytes(Password, Salt);
//Creates a symmetric Rijndael decryptor object.
ICryptoTransform Decryptor = RijndaelCipher.CreateDecryptor(SecretKey.GetBytes(32), SecretKey.GetBytes(16));

MemoryStream memoryStream = new MemoryStream(EncryptedData);
//Defines the cryptographics stream for decryption.THe stream contains decrpted data
CryptoStream cryptoStream = new CryptoStream(memoryStream, Decryptor, CryptoStreamMode.Read);

byte[] PlainText = new byte[EncryptedData.Length];
int DecryptedCount = cryptoStream.Read(PlainText, 0, PlainText.Length);
memoryStream.Close();
cryptoStream.Close();

//Converting to string
DecryptedData = Encoding.Unicode.GetString(PlainText, 0, DecryptedCount);
}
catch
{
DecryptedData = TextToBeDecrypted;
}
return DecryptedData;
}

public static string Encrypt(string TextToBeEncrypted)
{
RijndaelManaged RijndaelCipher = new RijndaelManaged();
string Password = "CSC";
byte[] PlainText = System.Text.Encoding.Unicode.GetBytes(TextToBeEncrypted);
byte[] Salt = Encoding.ASCII.GetBytes(Password.Length.ToString());
PasswordDeriveBytes SecretKey = new PasswordDeriveBytes(Password, Salt);
//Creates a symmetric encryptor object.
ICryptoTransform Encryptor = RijndaelCipher.CreateEncryptor(SecretKey.GetBytes(32), SecretKey.GetBytes(16));
MemoryStream memoryStream = new MemoryStream();
//Defines a stream that links data streams to cryptographic transformations
CryptoStream cryptoStream = new CryptoStream(memoryStream, Encryptor, CryptoStreamMode.Write);
cryptoStream.Write(PlainText, 0, PlainText.Length);
//Writes the final state and clears the buffer
cryptoStream.FlushFinalBlock();
byte[] CipherBytes = memoryStream.ToArray();
memoryStream.Close();
cryptoStream.Close();
string EncryptedData = Convert.ToBase64String(CipherBytes);

return EncryptedData;
}
}





Thanks & Regards,
Gokul



Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Encryption  .  Class  .  

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: Encypting and Decrypting the strings
Previous Resource: Encryption/Decryption in VB.Net
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