TripleDES algorithm

xiaoxiao2021-03-06  42

#REGION TrIPLEDES algorithm

Public Class Classtripledes

{

Public classtripledes ()

{

}

// Encryption, use the password to generate the public key of the encryption algorithm, and encrypt 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, use the password to generate an encryption algorithm, and decrypt the encrypted data using TrIPLEDES.

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 encrypt 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, use the password to generate an encryption algorithm, and decrypt the encrypted data using TrIPLEDES.

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;

}

}

}

#ndregion

转载请注明原文地址:https://www.9cbs.com/read-72485.html

New Post(0)