/// <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 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(); }
/// <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(); }