C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Encryption in .NET


Posted Date: 07 Oct 2008      Total Responses: 1

Posted By: kali raj       Member Level: Gold     Points: 1


How to achieve encryption and decryption asp.net



Responses

Author: loki    07 Oct 2008Member Level: BronzeRating:     Points: -20
Hi,

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; } }

enjoy coding,
loki


Post Reply
You must Sign In to post a response.
Next : Send Mail
Previous : date format
Return to Discussion Forum
Post New Message
Category: ASP.NET

Related Messages



dotNet Slackers   BizTalk Adaptors    Web Design

audio conferencing services

Contact Us    Privacy Policy    Terms Of Use