Asp.net tripledes encryption and decryption algorithms

Source: Internet
Author: User

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

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.