Using system. Security. cryptography;
Using system. text;
Using system. IO;
Encrypt des # region encrypt des
Public String encrypt (string ptoencrypt, string skey)
{
Descryptoserviceprovider des = new descryptoserviceprovider ();
// Put the string in the byte array
// The original utf8 encoding. I changed it to unicode encoding. No.
Byte [] inputbytearray = encoding. Default. getbytes (ptoencrypt );
// Create the key and offset of the encryption object
// Make sure that you enter the English text for the password
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 ();
}
# Endregion
Decryption Method # region Decryption Method
// Ptodecrypt is the string to be decrypted and skey is the key
Public String decrypt (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 ();
// Create a stringbuild object. createdecrypt uses a stream object. The decrypted text must be converted to a stream object.
Stringbuilder ret = new stringbuilder ();
Return System. Text. encoding. Default. getstring (Ms. toarray ());
}
# Endregion