1. Import two namespaces:
Using system. Security. cryptography;
Using system. Text. regularexpressions;
2. encryption and decryption class:
Private Static readonly string skey = "qjzgeh6heszdvjecnfpguxzaib7nlqm3"; // key
Private Static readonly string SIV = "qcdy6x + aplw ="; // vector, which can be empty
Private Static algorithm ricalgorithm MCSP = new tripledescryptoserviceprovider (); // construct a symmetric algorithm
3. encryption method:
Public static string encryptdes (string source)
{
Icryptotransform CT;
Memorystream MS;
Cryptostream Cs;
Byte [] BYT;
String STR = NULL;
MCSP. Key = convert. frombase64string (skey );
MCSP. IV = convert. frombase64string (SIV );
MCSP. mode = system. Security. cryptography. ciphermode. ECB;
MCSP. Padding = system. Security. cryptography. paddingmode. pkcs7;
Ct = MCSP. createencryptor (MCSP. Key, MCSP. IV );
Byt = encoding. utf8.getbytes (source );
MS = new memorystream ();
Cs = new cryptostream (MS, CT, cryptostreammode. Write );
CS. Write (BYT, 0, byt. Length );
CS. flushfinalblock ();
CS. Close ();
STR = convert. tobase64string (Ms. toarray ());
Return STR;
}
4. Confidential methods:
Public static string decryptdes (string source)
{
Icryptotransform CT;
Memorystream MS;
Cryptostream Cs;
Byte [] BYT;
String STR = NULL;
MCSP. Key = convert. frombase64string (skey );
MCSP. IV = convert. frombase64string (SIV );
MCSP. mode = system. Security. cryptography. ciphermode. ECB;
MCSP. Padding = system. Security. cryptography. paddingmode. pkcs7;
Ct = MCSP. createdecryptor (MCSP. Key, MCSP. IV );
Byt = convert. frombase64string (source );
MS = new memorystream ();
Cs = new cryptostream (MS, CT, cryptostreammode. Write );
CS. Write (BYT, 0, byt. Length );
CS. flushfinalblock ();
CS. Close ();
STR = encoding. utf8.getstring (Ms. toarray ());
Return STR;
}