Simple Encryption
Code
Using System;
Using System. Collections. Generic;
Namespace MD
{
Class Mainclass
{
Public Interface Ibindesh
{
String Encode ( String Str );
String Decode ( String Str );
}
Public Class Encryptiondecryption: ibindesh
{
Public String Encode ( String Str)
{
String Htext = "" ;
For ( Int I = 0 ; I < Str. length; I ++ )
{
Htext = Htext + ( Char ) (STR [I] + 10 - 1 * 2 );
}
Return Htext;
}
Public String Decode ( String Str)
{
String Dtext = "" ;
For ( Int I = 0 ; I < Str. length; I ++ )
{
Dtext = Dtext + ( Char ) (STR [I] - 10 + 1 * 2 );
}
Return Dtext;
}
Public Static Void Main ( String [] ARGs)
{
Encryptiondecryption inter = New Encryptiondecryption ();
String Str = (Ibindesh) Inter). encode ( " Admin " );
Console. writeline (STR );
Console. writeline (ibindesh) Inter). Decode (STR ));
Console. Read ();
}
}
}
}
Use the classes provided in. Net for DES encryption
Code
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. IO;
Using System. Security. cryptography;
Using System. text;
Public Partial Class Encryption and decryption: system. Web. UI. Page
{
Protected Void Page_load ( Object Sender, eventargs E)
{
String Encryptdata = " ABCD " ; // String to be encrypted
String Encryptkey = " 12345678 " ; // Key, which must be 8 bits
Stringbuilder sb = New Stringbuilder ();
SB. append ( " The data to be encrypted is: " + Encryptdata + " <Br/> " );
SB. append ( " The encrypted data is: " + Encrypt (encryptdata, encryptkey) + " <Br/> " );
SB. append ( " The decrypted data is: " + Decrypt (encrypt (encryptdata, encryptkey), encryptkey) + " <Br/> " );
Response. Write (sb. tostring ());
}
# Region
Private String Encrypt ( String Strdata, String Strkey)
{
Descryptoserviceprovider des = New Descryptoserviceprovider ();
Byte [] Bytedata = Encoding. utf8.getbytes (strdata );
// Foreach (byte B in bytedata)
// {
// Response. Write (B + "<br/> ");
// }
// Response. Write ("<HR/> ");
Des. Key = Asciiencoding. ASCII. getbytes (strkey ); // ASCII Value
Des. iv = Asciiencoding. ASCII. getbytes (strkey );
// Foreach (byte C in Des. Key)
// {
// Response. Write (C + "<br/> ");
// }
// Response. Write ("<HR/> ");
Memorystream MS = New Memorystream ();
Using (Cryptostream CS = New Cryptostream (MS, Des. createencryptor (), cryptostreammode. Write ))
{
CS. Write (bytedata, 0 , Bytedata. Length );
CS. flushfinalblock ();
CS. Close ();
}
// Foreach (byte D in ms. toarray ())
// {
// Response. Write (D + "<br/> ");
// }
String Str = Convert. tobase64string (Ms. toarray ());
Ms. Close ();
Des. Clear ();
Return STR;
}
Private String Decrypt ( String Strdata, String Strkey)
{
Byte [] Bytedata = Convert. frombase64string (strdata );
Using (Descryptoserviceprovider des = New Descryptoserviceprovider ())
{
Des. Key = Asciiencoding. ASCII. getbytes (strkey );
Des. iv = Asciiencoding. ASCII. getbytes (strkey );
Memorystream MS = New Memorystream ();
Using (Cryptostream CS = New Cryptostream (MS, Des. createdecryptor (), cryptostreammode. Write ))
{
CS. Write (bytedata, 0 , Bytedata. Length );
CS. flushfinalblock ();
CS. Close ();
}
String Str = Encoding. utf8.getstring (Ms. toarray ());
Ms. Close ();
Return STR;
}
}
# Endregion
}