Wednesday, January 4, 2012

code access security in asp.net

Using code access security we can convert the normal string format data into the encrypted format,the encrypted format data we can convert into the decrypted format (or) normal string format the code access security .we can implement by importing system.Security.Cryptography name space .The code access security depending on the hexa decimal byte values and encoding class.As per Microsoft the standard hexadecimal byte values are ox01 to ox16
using encoding class we can convert the normal string format data into the byte format,the byte format data we can convert into the normal string format,this class defined in the system.text name space.

TripleDESCryptoServiceProvider:
The class is used to encrypt the data,decrypt the data this class is depending on the hexa decimal byte values and encoding class

methods in TripleDES cryption:
1.Create Encryptor
2.create Decryptor
3.Cryptostream
Here i will shown how to encrypt and decrypt using TripleDES in asp.net.For this i have taken two button,one is for encryption and other one is for decryption
Example:

//name spaces which we have to add 
Using System.Security.Cryptography;
Using System.Text;
Using System.Io;


byte[] k=new byte[]{0x01,0x02,0x03,0x04,0x04..0x16}
byte[] i=new byte[]{0x01,0x02,0x03,0x04,0x04..0x16}
//button1_click
FileStream fs=new FileStream("c:/test.txt",FileMode.Create);
TripleDESCryptoServiceProvider tds=new TripleDESCryptoServiceProvider();
CryptoStream cs=new CryptoStream(fs,tds.CreateEecryptor(k,i),CryptoStreamMode.Write);
byte[]data=Encoding.ASCII.GetBytes("The  welcome")
cs.write(data,o,data.lenth)";
cs.close();
fs.close();

//button2-click
FileStream fs=new FileStream("c:/test.txt",FileMode.Open);
TripleDESCryptoServiceProvider tds=new TripleDESCryptoServiceProvider();
CryptoStream cs=new CryptoStream(fs,tds.CreateDecryptor(k,i),CryptoStreamMode.Read);
byte[]data=new byte[1000];
int len;
len=cs.read(data,o,data.lenth);
string s=Encoding.ASCII.GetString(data,o,data.lenth)
messagebox.show(s);
cs.write(data,o,data.lenthe)";
cs.close();
fs.close();

No comments:

Bel