/// <summary> /// 签退 /// </summary> /// <returns></returns> static Dictionary <string, ResultData> signOut() { Dictionary <int, string> data = new Dictionary <int, string>(); data.Add(3, "000000"); data.Add(11, "000006"); data.Add(12, getHHMMSS()); data.Add(41, "pos00001"); data.Add(42, "000000000000257"); data.Add(60, "22000003000"); data.Add(63, "01"); byte[] sendData = EncryptWT.EncryptData("0820", "6000000000", EncryptWT.encryptLKL(data, calculateMac(data, "0400"))); Console.WriteLine(BCDUtil.byteArrToString(sendData)); AnalysisBaseWT lk = new AnalysisBaseWT(); byte[] result = Client(LakalaIp, LakalaPort, sendData); if (result.Length > 0) { return(lk.analysis(result)); //发送数据 } else { return(null); } }
public static String CSMMAC(String key, String data) { if (key.Length != 32) { new Exception("key's length must be 16!"); return(null); } StringBuilder sb = new StringBuilder(data); int mod = data.Length % 32; if (mod != 0) { for (int i = 0; i < 32 - mod; i++) { sb.Append("0"); } } String oper = sb.ToString(); int count = oper.Length / 32; String[] blocks = new String[count]; for (int i = 0; i < count; i++) { blocks[i] = oper.Substring(i * 32, 32); } String vector = blocks[0]; //循环进行异或 for (int i = 1; i < count; i++) { vector = xOrString(vector, blocks[i]); } Console.WriteLine("vector:" + vector); //DES加密 String left = ByteUtil.getHexStr(Encoding.Default.GetBytes(vector.Substring(0, 16))); Console.WriteLine("left:" + left); String right = ByteUtil.getHexStr(Encoding.Default.GetBytes(vector.Substring(16))); Console.WriteLine("right:" + right); left = BCDUtil.byteArrToString(CSm4.EncriptB(BCDUtil.HexStrToByteArray(key), BCDUtil.HexStrToByteArray(left))); Console.WriteLine("加密left:" + left); right = xOrString(left, right); Console.WriteLine("加密后异或:" + right); right = BCDUtil.byteArrToString(CSm4.EncriptB(BCDUtil.HexStrToByteArray(key), BCDUtil.HexStrToByteArray(right))); Console.WriteLine("异或后加密:" + right); return(right); }
/// <summary> /// 计算mac 公共类 /// </summary> /// <param name="data"></param> /// <returns></returns> public static byte[] calculateMac(Dictionary <int, string> data, string msgType) { //计算MAC用到的数据 byte[] countMacData = EncryptWT.countMacData(msgType, EncryptWT.encryptLKL(data, null)); Console.WriteLine(BCDUtil.byteArrToString(countMacData)); string str = MacUtils.CSMMAC(mackey, BCDUtil.byteArrToString(countMacData)); Console.WriteLine("bcd2Str:" + PosProtocol.bcd2Str(System.Text.Encoding.Default.GetBytes(str.Substring(0, 8)))); //MAC转byte[] String strMac = PosProtocol.bcd2Str(System.Text.Encoding.Default.GetBytes(str.Substring(0, 8).ToUpper())); byte[] macByte = BCDUtil.ToByteArray(strMac); return(macByte); }
/// <summary> /// 加密数据 /// </summary> /// <param name="qiandaoNo"></param> /// <returns></returns> public static byte[] EncryptData(string msgType, string tpduStr, Dictionary <int, byte[]> data) { int signLength = 0; byte[] bcdMsgType = BCDUtil.str2Bcd(msgType); signLength += bcdMsgType.Length; string bin = ""; for (int i = 1; i <= 64; i++) { if (data.ContainsKey(i)) { bin += "1"; signLength += data[i].Length; } else { bin += "0"; } } byte[] bitMap = BCDUtil.str2Bcd(BCDUtil.leftpad(Convert.ToString(Convert.ToInt64(bin, 2), 16), 8)); signLength = signLength + bitMap.Length; signLength = signLength + 7; byte[] result = new byte[signLength]; int copytopos = 0; byte[] messageLen = BCDUtil.str2Bcd(BCDUtil.leftpad(Convert.ToString((signLength - 2), 16), 4)); byte[] tpdu = BCDUtil.str2Bcd(tpduStr); messageLen.CopyTo(result, copytopos); copytopos += 2; tpdu.CopyTo(result, copytopos); copytopos += 5; bcdMsgType.CopyTo(result, copytopos); copytopos += bcdMsgType.Length; bitMap.CopyTo(result, copytopos); copytopos += bitMap.Length; foreach (byte[] b in data.Values) { b.CopyTo(result, copytopos); copytopos += b.Length; } return(result); }
public ResultData analysisBit62(byte[] dataStr, ResultData rd) { int len = 2; int datalength = Convert.ToInt32(PosProtocol.bcd2Str(PosProtocol.subbyte(dataStr, rd.pos, len))); rd.pos += len; //如果长度大于 已知位数,则截取掉最后多余的位数 string data = BCDUtil.byteArrToString(PosProtocol.subbyte(dataStr, rd.pos, datalength)); rd.pos += datalength; rd.value = data; rd.name = "自定义域终端密钥"; return(new ResultData(rd)); }
/// <summary> /// 验证数据是否通过验证 /// </summary> /// <param name="data"></param> /// <returns></returns> static string validateKey(string data, string key) { string validata = data.Substring(data.Length - 8, 8).ToUpper(); byte[] sources = BCDUtil.HexStrToByteArray(data.Substring(0, 32)); byte[] desKeys = BCDUtil.HexStrToByteArray(key); byte[] encSource = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; byte[] encKeys = CSm4.DecriptB(desKeys, sources); if (PosProtocol.bytesToHexString(CSm4.EncriptB(encKeys, encSource)).Substring(0, 8).Equals(validata)) { Console.WriteLine("密钥验证通过返回灌输密钥!!!!"); return(PosProtocol.bytesToHexString(encKeys)); } return(""); }
/// <summary> /// 换算PIN /// </summary> /// <param name="pwd">用户输入的密码</param> /// <param name="cardNo">获取的ic卡卡号</param> /// <returns></returns> public static string getPin(string pwd, string cardNo) { string result = string.Empty; int len = cardNo.Length - 4; string pwdLen = pwd.Length + ""; if (pwdLen.Length == 1) { pwd = "0" + pwdLen + pwd; } string cardNoCover = ""; if (len < 12) { cardNo = cardNo.Substring(4, len); for (int i = 0; i < 12 - len; i++) { cardNoCover += "f"; } } else { cardNo = cardNo.Substring(3, 12); } cardNo = cardNoCover + cardNo; for (int i = 0; i < 32; i++) { if (cardNo.Length < 32) { cardNo = "0" + cardNo; } if (pwd.Length < 32) { pwd = pwd + "f"; } } string orx = MacUtils.xOrString(cardNo, pwd); //Console.WriteLine("orx="+ orx); result = BCDUtil.byteArrToString(CSm4.EncriptB(BCDUtil.ToByteArray(pinKey), BCDUtil.ToByteArray(orx))); // Console.WriteLine("result="+ result); Console.WriteLine(result.ToUpper()); return(result.ToUpper()); }
public static string pinKey = "F88E8810DB0F767C6921540FE4D2AB52"; // string.Empty; /// <summary> /// 客户端 /// </summary> /// <param name="ip"></param> /// <param name="port"></param> /// <param name="message"></param> static byte[] Client(string ip, int port, byte[] messages) { string sfd = BCDUtil.byteArrToString(messages); byte[] datas; try { //1.发送数据 TcpClient client = new TcpClient(ip, port); IPEndPoint ipendpoint = client.Client.RemoteEndPoint as IPEndPoint; NetworkStream stream = client.GetStream(); stream.Write(messages, 0, messages.Length); BCDUtil.printData("推送数据", messages); //2.接收状态,长度<1024字节 byte[] bytes = new Byte[1024 * 8]; string data = string.Empty; //读取超时时间,10秒 stream.ReadTimeout = 30000; int length = stream.Read(bytes, 0, bytes.Length); if (length == 2) { string headLenStr1 = Convert.ToString(bytes[0], 10); string headLenStr2 = Convert.ToString(bytes[1], 10); int headLen = Convert.ToInt32(headLenStr1 + headLenStr2); length = stream.Read(bytes, length, (headLen)); length += 2; } datas = new byte[length]; Array.Copy(bytes, datas, length); BCDUtil.printData("接收数据", datas); //3.关闭对象 stream.Close(); client.Close(); return(datas); } catch (Exception ex) { Console.WriteLine(string.Format("{0:HH:mm:ss}->{1}", DateTime.Now, ex.Message)); } //Console.ReadKey(); datas = new byte[0]; return(datas); }
/// <summary> /// 计算MAC的加密数据 /// </summary> /// <param name="qiandaoNo"></param> /// <returns></returns> public static byte[] countMacData(string msgType, Dictionary <int, byte[]> data) { int signLength = 0; byte[] bcdMsgType = BCDUtil.str2Bcd(msgType); signLength += bcdMsgType.Length; string bin = ""; for (int i = 1; i <= 63; i++) { if (data.ContainsKey(i)) { bin += "1"; signLength += data[i].Length; } else { bin += "0"; } } bin += "1"; byte[] bitMap = BCDUtil.str2Bcd(BCDUtil.leftpad(Convert.ToString(Convert.ToInt64(bin, 2), 16), 8)); signLength = signLength + bitMap.Length; byte[] result = new byte[signLength]; int copytopos = 0; bcdMsgType.CopyTo(result, copytopos); copytopos += bcdMsgType.Length; bitMap.CopyTo(result, copytopos); copytopos += bitMap.Length; foreach (byte[] b in data.Values) { b.CopyTo(result, copytopos); copytopos += b.Length; } return(result); }
/// <summary> /// 长度BCD,数据ASCII /// </summary> /// <param name="str"></param> /// <param name="len"></param> /// <returns></returns> public static byte[] sumLenBCDDataACSII(string str, int len) { int length = (str.Length.ToString()).Length; int size = len - length; string strlen = ""; string temp = ""; for (int i = 0; i < size; i++) { temp += "0"; } strlen = temp + str.Length; byte[] bcdlength = str2Bcd(strlen); byte[] data = System.Text.Encoding.Default.GetBytes(str); byte[] result = new byte[bcdlength.Length + data.Length]; bcdlength.CopyTo(result, 0); data.CopyTo(result, bcdlength.Length); Console.WriteLine(BCDUtil.byteArrToString(result)); return(result); }
/// <summary> /// 签到 /// </summary> /// <returns></returns> static Dictionary <string, ResultData> sign() { //签到 Dictionary <int, string> data = new Dictionary <int, string>(); data.Add(3, "000000"); data.Add(11, "000008"); data.Add(41, "pos00001"); data.Add(42, "000000000000257"); data.Add(63, "01"); byte[] result = Client(LakalaIp, LakalaPort, EncryptWT.EncryptData("0800", "6000000000", EncryptWT.encryptLKL(data, null))); //发送数据 if (result == null || result.Length <= 0) { return(null); } Console.WriteLine(BCDUtil.byteArrToString(result)); AnalysisBaseWT lk = new AnalysisBaseWTSign(); Dictionary <string, ResultData> rd = lk.analysis(result); return(rd); }
/// <summary> /// 数据处理 /// </summary> /// <param name="src"></param> /// <param name="macByte"></param> /// <returns></returns> public static Dictionary <int, byte[]> encryptLKL(Dictionary <int, string> src, byte[] macByte) { Dictionary <int, byte[]> data = new Dictionary <int, byte[]>(); foreach (KeyValuePair <int, string> item in src) { switch (item.Key) { case 1: data.Add(1, null); break; case 2: data.Add(2, BCDUtil.sumLenBCDDataBCD(item.Value, 2)); break; case 3: data.Add(3, BCDUtil.str2Bcd(item.Value)); break; case 4: data.Add(4, BCDUtil.str2Bcd(item.Value)); break; case 5: data.Add(5, null); break; case 6: data.Add(6, null); break; case 7: data.Add(7, BCDUtil.str2Bcd(item.Value)); break; case 8: data.Add(8, null); break; case 9: data.Add(9, null); break; case 10: data.Add(10, null); break; case 11: data.Add(11, BCDUtil.str2Bcd(item.Value)); break; case 12: data.Add(12, BCDUtil.str2Bcd(item.Value)); break; case 13: data.Add(13, null); break; case 14: data.Add(14, BCDUtil.str2Bcd(item.Value)); break; case 15: data.Add(15, null); break; case 16: data.Add(16, null); break; case 17: data.Add(17, null); break; case 18: data.Add(18, null); break; case 19: data.Add(19, null); break; case 20: data.Add(20, null); break; case 21: data.Add(21, null); break; case 22: data.Add(22, BCDUtil.str2Bcd(item.Value)); break; case 23: data.Add(23, BCDUtil.str2Bcd(item.Value)); break; case 24: data.Add(24, null); break; case 25: data.Add(25, BCDUtil.str2Bcd(item.Value)); break; case 26: data.Add(26, null); break; case 27: data.Add(27, null); break; case 28: data.Add(28, null); break; case 29: data.Add(29, null); break; case 30: data.Add(30, null); break; case 31: data.Add(31, null); break; case 32: data.Add(32, null); break; case 33: data.Add(33, null); break; case 34: data.Add(34, null); break; case 35: data.Add(35, BCDUtil.sumLenBCDDataBCD(item.Value, 2)); break; case 36: data.Add(36, BCDUtil.sumLenBCDDataBCD(item.Value, 3)); break; case 37: data.Add(37, null); break; case 38: data.Add(38, System.Text.Encoding.Default.GetBytes(item.Value)); break; case 39: data.Add(39, System.Text.Encoding.Default.GetBytes(item.Value)); break; case 40: data.Add(40, null); break; case 41: data.Add(41, System.Text.Encoding.Default.GetBytes(item.Value)); break; case 42: data.Add(42, System.Text.Encoding.Default.GetBytes(item.Value)); break; case 43: data.Add(43, null); break; case 44: data.Add(44, null); break; case 45: data.Add(45, null); break; case 46: data.Add(46, null); break; case 47: data.Add(47, null); break; case 48: data.Add(48, null); break; case 49: data.Add(49, System.Text.Encoding.Default.GetBytes(item.Value)); break; case 50: data.Add(50, null); break; case 51: data.Add(51, null); break; case 52: data.Add(52, BCDUtil.str2Bcd(item.Value)); break; case 53: data.Add(53, BCDUtil.str2Bcd(item.Value)); break; case 54: data.Add(54, null); break; case 55: string str = item.Value; byte[] datas = BCDUtil.ToByteArray(str.ToUpper()); byte[] bcdlength = BCDUtil.str2Bcd(datas.Length + ""); Console.WriteLine("------------------------------------------" + str); byte[] result = new byte[bcdlength.Length + datas.Length]; Console.WriteLine("------------------------------------------" + BCDUtil.byteArrToString(datas)); bcdlength.CopyTo(result, 0); datas.CopyTo(result, bcdlength.Length); Console.WriteLine(BCDUtil.byteArrToString(result)); data.Add(55, result); break; case 56: data.Add(56, null); break; case 57: data.Add(57, BCDUtil.sumLenBCDDataACSII(item.Value, 3)); break; case 58: data.Add(58, null); break; case 59: //data.Add(59, BCDUtil.sumLenBCDDataACSII(item.Value, 3)); string strData = item.Value; int size = strData.Length; //实际长度 int len = (size % 2 != 0 ? size + 1 : size) / 2; //更具实际长度判断是否为偶数如果是偶数则不管 否加1 int allowLen = 3; //允许长度 int length = (len.ToString()).Length; //获取实际长度的数字长度 int allowSize = allowLen - length; //允许大小 string strlen = ""; string temp = ""; for (int i = 0; i < allowSize; i++) { temp += "0"; } strlen = temp + len; byte[] bcdlength_59 = BCDUtil.str2Bcd(strlen); bool isNum = false; if ((size % 2) != 0) //奇数 { isNum = true; } byte[] data_59 = null; if (isNum) { data_59 = BCDUtil.str2Bcd(strData + "0"); } else { data_59 = BCDUtil.str2Bcd(strData); } byte[] result_59 = new byte[bcdlength_59.Length + data_59.Length]; bcdlength_59.CopyTo(result_59, 0); data_59.CopyTo(result_59, bcdlength_59.Length); data.Add(59, result_59); break; case 60: data.Add(60, BCDUtil.sumLenBCDDataACSII(item.Value, 3)); break; case 61: data.Add(61, BCDUtil.sumLenBCDDataACSII(item.Value, 3)); break; case 62: data.Add(62, BCDUtil.sumLenBCDDataACSII(item.Value, 3)); break; case 63: data.Add(63, BCDUtil.sumLenBCDDataACSII(item.Value, 3)); break; //case 64: // data.Add(64, BCDUtil.HexStrToByteArray(item.Value)); // break; } } //添加64域校验码 if (macByte != null) { data.Add(64, macByte); } return(data); }
public static void printData(string dataName, byte[] data) { Console.WriteLine("=========================" + dataName + "========================"); Console.WriteLine(BCDUtil.byteArrToString(data)); Console.WriteLine("=========================" + dataName + "========================"); }
/// <summary> /// 数据处理 /// </summary> /// <param name="src"></param> /// <param name="macByte"></param> /// <returns></returns> public static Dictionary <int, byte[]> encryptLKL(Dictionary <int, string> src, byte[] macByte) { Dictionary <int, byte[]> data = new Dictionary <int, byte[]>(); foreach (KeyValuePair <int, string> item in src) { switch (item.Key) { case 1: data.Add(1, null); break; case 2: data.Add(2, BCDUtil.sumLenBCDDataBCD(item.Value, 2)); break; case 3: data.Add(3, BCDUtil.str2Bcd(item.Value)); break; case 4: data.Add(4, BCDUtil.str2Bcd(item.Value)); break; case 5: data.Add(5, null); break; case 6: data.Add(6, null); break; case 7: data.Add(7, BCDUtil.str2Bcd(item.Value)); break; case 8: data.Add(8, null); break; case 9: data.Add(9, null); break; case 10: data.Add(10, null); break; case 11: data.Add(11, BCDUtil.str2Bcd(item.Value)); break; case 12: data.Add(12, BCDUtil.str2Bcd(item.Value)); break; case 13: data.Add(13, null); break; case 14: data.Add(14, null); break; case 15: data.Add(15, null); break; case 16: data.Add(16, null); break; case 17: data.Add(17, null); break; case 18: data.Add(18, null); break; case 19: data.Add(19, null); break; case 20: data.Add(20, null); break; case 21: data.Add(21, null); break; case 22: data.Add(22, BCDUtil.str2Bcd(item.Value)); break; case 23: data.Add(23, null); break; case 24: data.Add(24, null); break; case 25: data.Add(25, BCDUtil.str2Bcd(item.Value)); break; case 26: data.Add(26, null); break; case 27: data.Add(27, null); break; case 28: data.Add(28, null); break; case 29: data.Add(29, null); break; case 30: data.Add(30, null); break; case 31: data.Add(31, null); break; case 32: data.Add(32, null); break; case 33: data.Add(33, null); break; case 34: data.Add(34, null); break; case 35: data.Add(35, BCDUtil.sumLenBCDDataBCD(item.Value, 2)); break; case 36: data.Add(36, BCDUtil.sumLenBCDDataBCD(item.Value, 3)); break; case 37: data.Add(37, null); break; case 38: data.Add(38, System.Text.Encoding.Default.GetBytes(item.Value)); break; case 39: data.Add(39, System.Text.Encoding.Default.GetBytes(item.Value)); break; case 40: data.Add(40, null); break; case 41: data.Add(41, System.Text.Encoding.Default.GetBytes(item.Value)); break; case 42: data.Add(42, System.Text.Encoding.Default.GetBytes(item.Value)); break; case 43: data.Add(43, null); break; case 44: data.Add(44, null); break; case 45: data.Add(45, null); break; case 46: data.Add(46, null); break; case 47: data.Add(47, null); break; case 48: data.Add(48, BCDUtil.sumLenBCDDataACSII(item.Value, 3)); break; case 49: data.Add(49, System.Text.Encoding.Default.GetBytes(item.Value)); break; case 50: data.Add(50, null); break; case 51: data.Add(51, null); break; case 52: data.Add(52, BCDUtil.str2Bcd(item.Value)); break; case 53: data.Add(53, BCDUtil.str2Bcd(item.Value)); break; case 54: data.Add(54, null); break; case 55: data.Add(55, null); break; case 56: data.Add(56, null); break; case 57: data.Add(57, BCDUtil.sumLenBCDDataACSII(item.Value, 3)); break; case 58: data.Add(58, null); break; case 59: data.Add(59, BCDUtil.sumLenBCDDataACSII(item.Value, 3)); break; case 60: data.Add(60, BCDUtil.sumLenBCDDataBCD(item.Value, 3)); break; case 61: data.Add(61, BCDUtil.sumLenBCDDataACSII(item.Value, 3)); break; case 62: string str = item.Value; int len = 3; string strlen = ""; string temp = ""; byte[] dd = BCDUtil.ToByteArray((str)); int length = (dd.Length.ToString()).Length; int size = len - length; for (int i = 0; i < size; i++) { temp += "0"; } strlen = temp + dd.Length; byte[] bcdlength = BCDUtil.str2Bcd(strlen); byte[] result = new byte[bcdlength.Length + dd.Length]; bcdlength.CopyTo(result, 0); dd.CopyTo(result, bcdlength.Length); Console.WriteLine(BCDUtil.byteArrToString(result)); data.Add(62, result); break; case 63: data.Add(63, BCDUtil.sumLenBCDDataACSII(item.Value, 3)); break; //case 64: // data.Add(64, BCDUtil.HexStrToByteArray(item.Value)); // break; } } //添加64域校验码 if (macByte != null) { data.Add(64, macByte); } return(data); }