Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using system. Security. cryptography;
Using system. IO;
Namespace windowsformsapplication1
{
# Region tripledesAlgorithm
Public class classtripledes
{
Public classtripledes ()
{
}
// Encryption: Use the password to generate the public key of the encryption algorithm, and use tripledes to encrypt the password.
Public static string encrypt (string pass)
{
Try
{
Byte [] bt = (new system. Text. unicodeencoding (). getbytes (PASS );
Passwordderivebytes PDB = new passwordderivebytes (Pass, null );
Byte [] Key = PDB. getbytes (24 );
Byte [] IV = PDB. getbytes (8 );
Memorystream MS = new memorystream ();
Tripledescryptoserviceprovider tdesc = new tripledescryptoserviceprovider ();
Cryptostream cs = new cryptostream (MS, tdesc. createencryptor (Key, IV), cryptostreammode. Write );
CS. Write (BT, 0, BT. Length );
CS. flushfinalblock ();
Return convert. tobase64string (Ms. toarray ());
}
Catch (exception ex)
{
Throw ex;
}
}
// Decrypt, generate the public key of the encryption algorithm using the password, and use tripledes to decrypt the encrypted data.
Public static string decrypt (string STR, string pass)
{
Try
{
Byte [] bt = convert. frombase64string (STR );
Passwordderivebytes PDB = new passwordderivebytes (Pass, null );
Byte [] Key = PDB. getbytes (24 );
Byte [] IV = PDB. getbytes (8 );
Memorystream MS = new memorystream ();
Tripledescryptoserviceprovider tdesc = new tripledescryptoserviceprovider ();
Cryptostream cs = new cryptostream (MS, tdesc. createdecryptor (Key, IV), cryptostreammode. Write );
CS. Write (BT, 0, BT. Length );
CS. flushfinalblock ();
Return (new system. Text. unicodeencoding (). getstring (Ms. toarray ());
}
Catch (exception ex)
{
Throw ex;
}
}
// Use:
// String STR = encrypt ("BBB ");
// Console. writeline (decrypt (STR, "BBB "));
// Encryption: Use the password to generate the public key of the encryption algorithm, and use tripledes to encrypt the password.
Public static string encryptwithkey (string pass, string p_key)
{
Try
{
Byte [] bt = (new system. Text. unicodeencoding (). getbytes (PASS );
Passwordderivebytes PDB = new passwordderivebytes (p_key, null );
Byte [] Key = PDB. getbytes (24 );
Byte [] IV = PDB. getbytes (8 );
Memorystream MS = new memorystream ();
Tripledescryptoserviceprovider tdesc = new tripledescryptoserviceprovider ();
Cryptostream cs = new cryptostream (MS, tdesc. createencryptor (Key, IV), cryptostreammode. Write );
CS. Write (BT, 0, BT. Length );
CS. flushfinalblock ();
Return convert. tobase64string (Ms. toarray ());
}
Catch (exception ex)
{
Throw ex;
}
}
// Decrypt, generate the public key of the encryption algorithm using the password, and use tripledes to decrypt the encrypted data.
Public static string decryptwithkey (string STR, string p_key)
{
Try
{
Byte [] bt = convert. frombase64string (STR );
Passwordderivebytes PDB = new passwordderivebytes (p_key, null );
Byte [] Key = PDB. getbytes (24 );
Byte [] IV = PDB. getbytes (8 );
Memorystream MS = new memorystream ();
Tripledescryptoserviceprovider tdesc = new tripledescryptoserviceprovider ();
Cryptostream cs = new cryptostream (MS, tdesc. createdecryptor (Key, IV), cryptostreammode. Write );
CS. Write (BT, 0, BT. Length );
CS. flushfinalblock ();
Return (new system. Text. unicodeencoding (). getstring (Ms. toarray ());
}
Catch (exception ex)
{
Throw ex;
}
}
}
# Endregion
}