asp.net tripledes encryption, decryption algorithm _ Practical skills
Last Update:2017-01-18
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 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
}