TripleDES algorithm

xiaoxiao2021-03-06  60

#REGION TrIPLEDES algorithm

Public class classtripledes {public classtriple () {}

// 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 the public key of the 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-82739.html

New Post(0)