asp.net tripledes encryption, decryption algorithm _ Practical skills

Source: Internet
Author: User
Tags decrypt
Using System;

Using System.Collections.Generic;

Using System.Linq;

Using System.Text;

Using System.Security.Cryptography;

Using System.IO;

Namespace WindowsFormsApplication1

{

#region TripleDES algorithm



public class Classtripledes

{

Public Classtripledes ()

{

}



Encryption, which uses a password to generate the public key of the cryptographic algorithm and encrypts the password using TripleDES.

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;

}

}



Decryption, using a password to generate the public key of the encryption algorithm, and using 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, which uses a password to generate the public key of the cryptographic algorithm and encrypts the password using TripleDES.

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;

}

}



Decryption, using a password to generate the public key of the encryption algorithm, and using 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

}
Related Article

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.