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 »

Simple encryption and decryption


Posted Date: 16 Apr 2009    Resource Type: Code Snippets    Category: Encryption
Author: nisarMember Level: Gold    
Rating: 1 out of 5Points: 12



Hey! I already post this code on against many question about text encryption, but here everyone can access with out any problem
I write two functions Encrypt and Decrypt
For Encrypt function just pass a simple string and it will return encrypted string
and vise verse with decrypt function

These function are not only for storing and retrieving passwords, but you can also use these functions on query string, when you pass value as a parameter with page redirection, you can encrypt your parameter, and at the when you want to use this parameter any other page, just use decrypt function, hope this little code help you
I think this information is enough for using these functions, please feel free to ask if you have any query


using System;
using System.Data;
using System.Configuration;
using System.Collections;
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;
using System.Data.SqlClient;
using System.IO;
using System.Text;
using System.Security.Cryptography;

public static string Encrypt(string pstrText)
{
string pstrEncrKey = "1239;[pewGKG)NisarFidesTech";
byte[] byKey ={ };
byte[] IV = { 18, 52, 86, 120, 144, 171, 205, 239 };
byKey = System.Text.Encoding.UTF8.GetBytes(pstrEncrKey.Substring(0, 8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = Encoding.UTF8.GetBytes(pstrText);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
return Convert.ToBase64String(ms.ToArray());
}

public static string Decrypt(string pstrText)
{
pstrText = pstrText.Replace(" ", "+");
string pstrDecrKey = "1239;[pewGKG)NisarFidesTech";
byte[] byKey ={ };
byte[] IV = { 18, 52, 86, 120, 144, 171, 205, 239 };
byte[] inputByteArray = new byte[pstrText.Length];

byKey = System.Text.Encoding.UTF8.GetBytes(pstrDecrKey.Substring(0, 8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
inputByteArray = Convert.FromBase64String(pstrText);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
System.Text.Encoding encoding = System.Text.Encoding.UTF8;
return encoding.GetString(ms.ToArray());
}



Responses

Author: Miss Meetu Choudhary    16 Apr 2009Member Level: Diamond   Points : 1
For description

Please Describe your code as well to make it more efficient we respect your contributions.

And it will be of great help to viewers if they are well described.

add the description and revert back to us


Thanks and Regards
Meetu Choudhary
Editor


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Encryption  .  Encrypt  .  Decryption  .  Decrypt  .  

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: Encryption/Decryption in VB.Net
Previous Resource: Create Salt/IV file for password
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