/// <summary> /// 类测试 /// </summary> public static void Test() { System.Text.Encoding utf8 = System.Text.Encoding.UTF8; //key为abcdefghijklmnopqrstuvwx的Base64编码 byte[] key = Convert.FromBase64String("YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4"); byte[] iv = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }; //当模式为ECB时,IV无用 byte[] data = utf8.GetBytes("中国ABCabc123"); System.Console.WriteLine("ECB模式:"); byte[] str1 = Des3.Des3EncodeECB(key, iv, data); byte[] str2 = Des3.Des3DecodeECB(key, iv, str1); System.Console.WriteLine(Convert.ToBase64String(str1)); System.Console.WriteLine(System.Text.Encoding.UTF8.GetString(str2)); System.Console.WriteLine(); System.Console.WriteLine("CBC模式:"); byte[] str3 = Des3.Des3EncodeCBC(key, iv, data); byte[] str4 = Des3.Des3DecodeCBC(key, iv, str3); System.Console.WriteLine(Convert.ToBase64String(str3)); System.Console.WriteLine(utf8.GetString(str4)); System.Console.WriteLine(); }
/// <summary> /// 其它的测试. /// </summary> public static void OtherTest() { // 待加密的 xml 字符串. string xml = "<?xml version=\"1.0\" encoding=\"GBK\"?><request><head><h_exch_code>208801</h_exch_code><h_bank_no>0020</h_bank_no><h_user_id>1120888233</h_user_id><h_branch_id>B00008211</h_branch_id><h_fact_date></h_fact_date>2013-03-01<h_fact_time></h_fact_time>15:58:39<h_exch_date></h_exch_date><h_serial_no>12345678</h_serial_no><h_rsp_code></h_rsp_code><h_rsp_msg></h_rsp_msg></head><body><record></record></body></request>"; // 编码. System.Text.Encoding gbk = System.Text.Encoding.GetEncoding("GBK"); // 密码. string pwd = "ABCDEF0123456789abcdef11"; // 密钥字节数组. byte[] key = gbk.GetBytes(pwd); // 当模式为ECB时,IV无用 byte[] iv = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }; // 明文. byte[] data = gbk.GetBytes(xml); // 加密 byte[] str1 = Des3.Des3EncodeECB(key, iv, data); System.Console.WriteLine("加密结果:{0}", Convert.ToBase64String(str1)); // 解密. byte[] str2 = Des3.Des3DecodeECB(key, iv, str1); System.Console.WriteLine("解密后结果:{0}", System.Text.Encoding.UTF8.GetString(str2)); System.Console.WriteLine(); }
/// <summary> /// 测试 /// </summary> public static void Test() { System.Console.WriteLine("3DES 加密-解密 测试, 测试目标, 明确 ECB 模式 与 CBC 模式 的区别!"); System.Text.Encoding utf8 = System.Text.Encoding.UTF8; // key为abcdefghijklmnopqrstuvwx的Base64编码 byte[] key = Convert.FromBase64String("YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4"); // 当模式为ECB时,IV无用 byte[] iv = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }; byte[] iv2 = new byte[] { 8, 7, 6, 5, 4, 3, 2, 1 }; // 原文. byte[] data = utf8.GetBytes("中国ABCabc123"); System.Console.WriteLine("ECB模式:"); // 加密 byte[] str1 = Des3.Des3EncodeECB(key, iv, data); System.Console.WriteLine("加密结果:{0}", Convert.ToBase64String(str1)); // 验证,是否 ECB 模式下, IV 无用. byte[] strECB = Des3.Des3EncodeECB(key, iv2, data); System.Console.WriteLine("设置了其他的IV,加密结果为:{0}", Convert.ToBase64String(strECB)); // 解密. byte[] str2 = Des3.Des3DecodeECB(key, iv, str1); System.Console.WriteLine("解密后结果:{0}", System.Text.Encoding.UTF8.GetString(str2)); System.Console.WriteLine(); System.Console.WriteLine("CBC模式:"); // 加密 byte[] str3 = Des3.Des3EncodeCBC(key, iv, data); System.Console.WriteLine("加密结果:{0}", Convert.ToBase64String(str3)); // 验证,是否 CBC 模式下, IV 是有用的. byte[] strCBC = Des3.Des3EncodeCBC(key, iv2, data); System.Console.WriteLine("设置了其他的IV,加密结果为:{0}", Convert.ToBase64String(strCBC)); // 解密. byte[] str4 = Des3.Des3DecodeCBC(key, iv, str3); System.Console.WriteLine("解密后结果:{0}", utf8.GetString(str4)); System.Console.WriteLine(); }
public static string Encrypt(string strText) { try { byte[] keys = Encoding.GetEncoding("utf-8").GetBytes(_3dkey); byte[] data = Encoding.GetEncoding("utf-8").GetBytes(strText); byte[] iv = Encoding.GetEncoding("utf-8").GetBytes("12345678"); byte[] result = Des3.Des3EncodeECB(keys, iv, data); if (result != null) { return(ToHexString(result)); } return(string.Empty); } catch (System.Exception ex) { ExceptionHandler.HandleException(ex); return(string.Empty); } }
/// <summary> /// 类测试 /// </summary> public static void Test() { System.Text.Encoding utf8 = System.Text.Encoding.UTF8; //key为abcdefghijklmnopqrstuvwx的Base64编码 string ydata = "00068900002||20180927095839|||3002800173|0200000777|01|0155129175|||"; byte[] key = Convert.FromBase64String("YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4"); byte[] iv = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }; //当模式为ECB时,IV无用 byte[] data = utf8.GetBytes(ydata); System.Console.WriteLine("原始报文:"); System.Console.WriteLine(ydata); System.Console.WriteLine(); byte[] str1 = Des3.Des3EncodeECB(key, iv, data); byte[] str2 = Des3.Des3DecodeECB(key, iv, str1); System.Console.WriteLine("ECB加密报文:"); System.Console.WriteLine(Convert.ToBase64String(str1)); System.Console.WriteLine("ECB解密报文:"); System.Console.WriteLine(System.Text.Encoding.UTF8.GetString(str2)); System.Console.WriteLine(); byte[] str3 = Des3.Des3EncodeCBC(key, iv, data); byte[] str4 = Des3.Des3DecodeCBC(key, iv, str3); System.Console.WriteLine("CBC加密报文:"); System.Console.WriteLine(Convert.ToBase64String(str3)); System.Console.WriteLine("CBC解密报文:"); System.Console.WriteLine(utf8.GetString(str4)); System.Console.WriteLine(); Console.ReadLine(); }