Posting time: 12:52:00
Using system;
Using system. Collections. Generic;
Using system. text;
Using system. Security. cryptography;
Using system. IO;
Namespace consoleapplication1
{
Class des
{
/// <Summary>
/// Encryption function
/// </Summary>
/// <Param name = "ptoencrypt"> </param>
/// <Param name = "skey"> </param>
/// <Returns> </returns>
Public String desencrypt (string ptoencrypt, string skey ){
Descryptoserviceprovider des = new descryptoserviceprovider ();
Byte [] inputbytearray = encoding. Default. getbytes (ptoencrypt );
Des. Key = asciiencoding. ASCII. getbytes (skey );
Des. IV = asciiencoding. ASCII. getbytes (skey );
Memorystream MS = new memorystream ();
Cryptostream cs = new cryptostream (MS, Des. createencryptor (), cryptostreammode. Write );
CS. Write (inputbytearray, 0, inputbytearray. Length );
CS. flushfinalblock ();
Stringbuilder ret = new stringbuilder ();
Foreach (byte B in ms. toarray ()){
Ret. appendformat ("{0: X2}", B );
}
Ret. tostring ();
Return ret. tostring ();
}
/// <Summary>
/// Decryption Function
/// </Summary>
/// <Param name = "ptodecrypt"> </param>
/// <Param name = "skey"> </param>
/// <Returns> </returns>
Public String desdecrypt (string ptodecrypt, string skey ){
Descryptoserviceprovider des = new descryptoserviceprovider ();
Byte [] inputbytearray = new byte [ptodecrypt. Length/2];
For (INT x = 0; x <ptodecrypt. Length/2; X ++ ){
Int I = (convert. toint32 (ptodecrypt. substring (x * 2, 2), 16 ));
Inputbytearray [x] = (byte) I;
}
Des. Key = asciiencoding. ASCII. getbytes (skey );
Des. IV = asciiencoding. ASCII. getbytes (skey );
Memorystream MS = new memorystream ();
Cryptostream cs = new cryptostream (MS, Des. createdecryptor (), cryptostreammode. Write );
CS. Write (inputbytearray, 0, inputbytearray. Length );
CS. flushfinalblock ();
Stringbuilder ret = new stringbuilder ();
Return System. Text. encoding. Default. getstring (Ms. toarray ());
}
}
Class Program
{
Static void main (string [] ARGs ){
Des d = new des ();
String STR = D. desencrypt ("cryptostream (MS", "12345678 ");
Console. writeline (STR );
STR = D. desdecrypt (STR, "12345678 ");
Console. writeline (STR );
}
}
}