hardware_cmd.cpp 33.3 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186
#include "stdafx.h"

#include "etradeclient/hardware/hardware_cmd.h"

#include <sstream>

#include <boost/algorithm/string.hpp>

#include "etradeclient/boost_patch/property_tree/json_parser.hpp" // WARNIING! Make sure to include our patched version.
#include "etradeclient/utility/logging.h"
#include "etradeclient/utility/string_converter.h"
#include "etradeclient/utility/application_config.h"

#include "etradeclient/hardware/pin_pad.h"
#include "etradeclient/hardware/id_card_reader.h"
#include "etradeclient/hardware/dili_card_device.h"
#include "etradeclient/hardware/bank_card_reader.h"
#include "etradeclient/hardware/password_machine.h"
#include "ETradeClient/utility/openssl_aes_cbc.h"
#include "ETradeClient/utility/DLBmpManager.h"
#include "ETradeClient/utility/string_converter.h"
#include "ETradeClient/utility/DLDes.h"

namespace StatusCode
{
	static const std::string OK = ""; // 操作成功.

	static const std::string PINPAD_CONNECT_FAILED = "100"; // 连接密码键盘失败. 
	static const std::string PINPAD_TIMEOUT = "101"; // 用户密码输入超时. 
	static const std::string PINPAD_CANCELLED = "102"; // 用户取消密码输入. 
	static const std::string PINPAD_READ_FAILED = "103"; // 读取密码失败. 

	static const std::string ID_CARD_READER_CONNECT_FAILED = "200"; // 连接身份证读卡器失败. 
	static const std::string ID_CARD_READER_VERIFY_FAILED = "201"; // 身份证验证失败.
	static const std::string ID_CARD_READER_READ_FAILED = "202"; // 读取身份证信息失败.

	static const std::string CPU_CARD_DEVICE_CONNECT_FAILED = "300"; // 打开CPU卡读卡器失败. 
	static const std::string CPU_CARD_DEVICE_CANNOT_FIND_CARD = "301"; // 无法找到卡片,请放卡到读卡器上(或重新放置卡片).
	
	static const std::string DILI_CARD_DEVICE_ACTIVATE_CARD_FAILED = "302"; // 卡片激活失败, 请检查该卡是否已处于激活状态.
	static const std::string DILI_CARD_DEVICE_RESET_CARD_FAILED = "303"; // 卡片撤销激活失败, 请检查该卡是否已处于未激活状态.
	static const std::string DILI_CARD_DEVICE_READ_BASIC_INFO_FAILED = "304"; // 读取卡片基本信息失败.
	static const std::string DILI_CARD_DEVICE_READ_SERVICE_INFO_FAILED = "305"; // 读取卡片业务信息失败.
	static const std::string DILI_CARD_DEVICE_WRITE_SERVICE_INFO_FAILED = "306"; // 写入卡片业务信息失败.
	static const std::string DILI_CARD_DEVICE_WRITE_UNMATCHED_CARD = "307"; // 写卡卡片的序列号不匹配.
	static const std::string DILI_CARD_DEVICE_NEED_UPDATE = "308"; //卡片需要升级,请升级后继续使用。

	static const std::string PWD_MACHINE_CONNECT_FAILED = "401"; // 连接加密机失败.
	static const std::string PWD_MACHINE_GET_KEY_FAILED = "402"; // 从加密机读取密钥失败.

	static const std::string BANK_CARD_READ_CARD_NUM_FAILED = "501"; // 读取银行卡卡号失败.
}

namespace EncryptTools
{
	int Encrypt(unsigned char* pSrc, unsigned char u8Len, unsigned char* pKey, unsigned char* pCiphertText)
	{
		int iResult = 0;

		uint8_t au8DataBlock[50][8] = { 0 };

		int iBlockCount = 0, iCoverPos = 0, iBlockNum = 0;
		iBlockCount = u8Len / 8;
		iCoverPos = u8Len % 8;

		while (iBlockCount > 0)
		{
			memcpy(au8DataBlock[iBlockNum], pSrc, 8);
			pSrc += 8;
			--iBlockCount;
			++iBlockNum;
		}

		if (iCoverPos)
		{
			memcpy(au8DataBlock[iBlockNum], pSrc, iCoverPos);
			au8DataBlock[iBlockNum][iCoverPos] = 0x80;
			++iCoverPos;
			for (int index = iCoverPos; iCoverPos < 8; ++iCoverPos)
			{
				au8DataBlock[iBlockNum][iCoverPos] = 0x00;
			}
		}
		else
		{
			--iBlockNum;
		}

		uint8_t au8KeyL[8] = { 0 }, au8KeyR[8] = { 0 };
		memcpy(au8KeyL, pKey, 8);
		memcpy(au8KeyR, pKey, 8);
		for (int index = 0; index <= iBlockNum; ++index)
		{
			uint8_t au8Cipher1[8] = { 0 }, au8Cipher2[8] = { 0 };
			DES(au8KeyL, au8DataBlock[index], au8Cipher1);
			_DES(au8KeyR, au8Cipher1, au8Cipher2);
			DES(au8KeyL, au8Cipher2, &pCiphertText[index * 8]);
		}

		return iResult;
	}

	bool HexToChar(unsigned char* pHex, int iHexLen, unsigned char* pChar)
	{
		bool bResult = true;
		if (pHex && iHexLen && pChar)
		{
			int iCharCount = 0;
			for (int index = 0; index < iHexLen; ++index)
			{
				char cNum[3] = { 0 };
				if (pHex[index] <= 0x0f)
				{
					pChar[iCharCount] = '0';
					++iCharCount;
					sprintf_s(cNum, "%X", pHex[index]);
					pChar[iCharCount] = cNum[0];
					++iCharCount;
				}
				else
				{
					sprintf_s(cNum, "%2X", pHex[index]);
					pChar[iCharCount] = cNum[0];
					++iCharCount;
					pChar[iCharCount] = cNum[1];
					++iCharCount;
				}
			}
		}
		return bResult;
	}

	bool CharToHex(unsigned char* pStr, unsigned char& cHex)
	{
		bool bResult = true;

		unsigned char aHex[2] = { 0 }, cTemp = 0;
		if (pStr)
		{
			int iCount = 2;
			do
			{
				char cStr = tolower(pStr[iCount - 1]);
				switch (cStr)
				{
				case '0':
				case '1':
				case '2':
				case '3':
				case '4':
				case '5':
				case '6':
				case '7':
				case '8':
				case '9':
				{
					cTemp = atoi(&cStr);
					break;
				}
				case 'a':
				{
					cTemp = 10;
					break;
				}
				case 'b':
				{
					cTemp = 11;
					break;
				}
				case 'c':
				{
					cTemp = 12;
					break;
				}
				case 'd':
				{
					cTemp = 13;
					break;
				}
				case 'e':
				{
					cTemp = 14;
					break;
				}
				case 'f':
				{
					cTemp = 15;
					break;
				}
				}
				aHex[iCount - 1] = cTemp;
				--iCount;
			} while (iCount);

			cHex = (aHex[0] << 4) + aHex[1];
		}
		else
		{
			bResult = false;
		}
		return bResult;
	}

	bool StringToHex(unsigned char* pStr, int iLen, unsigned char* pHex)
	{
		bool bResult = true;

		int iStrIndex = 0;
		int index = 0;
		while (bResult && iStrIndex < iLen)
		{
			bResult = CharToHex(&pStr[iStrIndex], pHex[index]);
			++index;
			iStrIndex += 2;
		}

		if (!bResult)
		{
			throw std::exception("9999");
		}
		/*for (int index = 0, iStrIndex = 0; iStrIndex < iLen; ++index, iStrIndex += 2)
		{
		CharToHex(&pStr[iStrIndex], pHex[index]);
		}*/

		return bResult;
	}
}

namespace 
{
	const PINPad& PINPad_()
	{
		static PINPad pin_pad;
		return pin_pad;
	}

	IDCardReader& IDCardReader_()
	{
		static IDCardReader id_card_reader;
		return id_card_reader;
	}

	const DILICard::RWDevice& DILICardRWDevice()
	{
		static DILICard::RWDevice dili_card_device;
		return dili_card_device;
	}

	const PWDMachine& PWDMachine_()
	{
		static PWDMachine pwd_machine;
		return pwd_machine;
	}

	const BankCardReader& BankCardReader_()
	{
		static BankCardReader bank_card_reader;
		return bank_card_reader;
	}

	std::string g_sCardNum;//用于办卡时,保证操作的卡片是同一张
}

ReadPINPadCmd::Reply ReadPINPadCmd::Execute(const std::string& input)
{
	LOG_TRACE(L"读取用户输入密码。");
	const uint8_t PWD_MAX_LEN = ApplicationConfig::Instance().PINPadPWDLen();
	const uint8_t TIMEOUT = ApplicationConfig::Instance().PINPadTimeout();
	auto& pin_pad = PINPad_();
	std::string pwd("");
	Reply reply;
	do
	{
		try
		{
			if (!pin_pad.Connect())
			{
				LOG_ERROR(L"连接密码键盘失败。");
				reply.error_code = StatusCode::PINPAD_CONNECT_FAILED;
				break;
			}
			pin_pad.DisplayBuiltInText(PINPad::BuiltInText::kEnterPWD);
			pin_pad.PlayVoice(PINPad::BuiltInVoice::kEnterPWD);
			pin_pad.SetPWDMaxLen(PWD_MAX_LEN);
			PINPad::StatusCode res = pin_pad.ReadUserInputPWD(PINPad::DispMode::kEncrypted, PINPad::DispLine::kL2, TIMEOUT, pwd);
			if (PINPad::StatusCode::kCancelled == res)
			{
				reply.error_code = StatusCode::PINPAD_CANCELLED;
				LOG_ERROR(L"用户取消密码输入。");
			}
			else if (PINPad::StatusCode::kTimeout == res)
			{
				reply.error_code = StatusCode::PINPAD_TIMEOUT;
				LOG_ERROR(L"用户输入密码超时。");
			}
			else if (PINPad::StatusCode::kUnknown == res)
			{
				reply.error_code = StatusCode::PINPAD_READ_FAILED;
				LOG_ERROR(L"读取用户输入密码失败,请确认密码键盘连接正常!");
			}
		}
		catch (std::exception& ex)
		{
			LOG_FATAL(L"读取用户输入密码发生异常,异常信息: " + str_2_wstr(ex.what()));
			reply.error_code = StatusCode::PINPAD_READ_FAILED;
			break;
		}
	} while (0);
	pin_pad.Disconnect();
	if (boost::iequals(reply.error_code, StatusCode::OK))
		LOG_TRACE(L"读取用户输入密码成功。");

	reply.data.put("password", pwd);
	return reply;
}

ReadPINPadCmd::Reply ReadIDCardCmd::Execute(const std::string& input)
{
	LOG_TRACE(L"读取身份证信息。");
	auto& id_card_reader = IDCardReader_();
	Reply reply;
	IDCardInfo id_card_info;

	uint8_t u8IDReaderType = ApplicationConfig::Instance().IDReaderType();

	switch (u8IDReaderType)
	{
	case 0:
	{
		do
		{
			try
			{
				if (!id_card_reader.Connect())
				{
					LOG_ERROR(L"建立身份证读卡器连接失败!");
					reply.error_code = StatusCode::ID_CARD_READER_CONNECT_FAILED;
					break;
				}
				if (!id_card_reader.VerifyCard())
				{
					LOG_ERROR(L"未找到身份证或身份证验证失败!");
					reply.error_code = StatusCode::ID_CARD_READER_VERIFY_FAILED;
					break;
				}
				id_card_info = id_card_reader.ReadCard();
			}
			catch (std::exception& ex)
			{
				LOG_FATAL(L"读取身份证信息失败,错误信息: " + str_2_wstr(ex.what()));
				reply.error_code = StatusCode::ID_CARD_READER_READ_FAILED;
				break;
			}
		} while (0);
		id_card_reader.Disconnect();
	}
	break;
	case 1:
	{
		do
		{
			try
			{
				if (!id_card_reader.SynConnect())
				{
					LOG_ERROR(L"建立身份证读卡器连接失败!");
					reply.error_code = StatusCode::ID_CARD_READER_CONNECT_FAILED;
					break;
				}
				if (!id_card_reader.SynFindCard())
				{
					LOG_ERROR(L"未找到身份证或身份证验证失败!");
					reply.error_code = StatusCode::ID_CARD_READER_VERIFY_FAILED;
					break;
				}
				id_card_info = id_card_reader.SynReadCard();
			}
			catch (std::exception& ex)
			{
				LOG_FATAL(L"读取身份证信息失败,错误信息: " + str_2_wstr(ex.what()));
				reply.error_code = StatusCode::ID_CARD_READER_READ_FAILED;
				break;
			}
		} while (0);
		id_card_reader.SynDisConnect();
	}
	break;
	}
	
	if (boost::iequals(reply.error_code, StatusCode::OK))
		LOG_TRACE(L"读取身份证信息成功。");

	CDLBmpManager cBmp;
	string sBmpData = cBmp.ReadBmp(wstr_2_str(id_card_info.portrait_img_path).c_str());
	switch (u8IDReaderType)
	{
		case 0:
		{
			DeleteFile(id_card_info.portrait_img_path.c_str());
			wstring sPath = id_card_info.portrait_img_path.substr(0, id_card_info.portrait_img_path.size() - 6);
			DeleteFile((sPath + L"wz.txt").c_str());
			DeleteFile((sPath + L"xp.wlt").c_str());
		}
		break;
		case 1:
		{
			DeleteFile(id_card_info.portrait_img_path.c_str());
		}
		break;
	}
	
	//读卡器读出来的数据中可能带空格,要去掉
	id_card_info.Trim();

	reply.data.put("id", wstr_2_str(id_card_info.id_code));
	reply.data.put("name", wstr_2_str(id_card_info.name)); // Use 'wstr_2_str' to convert to UTF-8 string.
	reply.data.put("gender", wstr_2_str(id_card_info.gender));
	reply.data.put("nation", wstr_2_str(id_card_info.nation));
	reply.data.put("birthday", wstr_2_str(id_card_info.birth_date));
	reply.data.put("address", wstr_2_str(id_card_info.address));
	reply.data.put("portrait_img_path", wstr_2_str(id_card_info.portrait_img_path));
	reply.data.put("depart", wstr_2_str(id_card_info.depart));
	reply.data.put("start_date", wstr_2_str(id_card_info.start_date));
	reply.data.put("end_date", wstr_2_str(id_card_info.end_date));
	reply.data.put("img_data", sBmpData);
	return reply;
}

ReadPINPadCmd::Reply ActivateDILICardCmd::Execute(const std::string& input)
{
	Reply reply;
	LOG_TRACE(L"卡片激活。");
	
	std::string card_sn;
	std::string key_DCCK, key_F0015_DACK, key_F0015_DEAK, key_F0015_DAMK1, key_F0005_DAMK1;
	DILICard::BasicInfo card_basic_info;
	SecurityLevel slLevel = (SecurityLevel)ApplicationConfig::Instance().SecurityLevel();
	EncryptType etEncrypType = (EncryptType)ApplicationConfig::Instance().EncryptType();
	auto& dili_card_device = DILICardRWDevice();
	do 
	{
		try
		{
			dili_card_device.Connect();
		}
		catch (std::exception& ex)
		{
			LOG_FATAL(L"连接读卡器失败,错误信息: " + str_2_wstr(ex.what()));
			reply.error_code = StatusCode::CPU_CARD_DEVICE_CONNECT_FAILED;
			break;
		}
		try
		{
			dili_card_device.FindCard();
		}
		catch (std::exception& ex)
		{
			LOG_FATAL(L"寻卡失败,错误信息: " + str_2_wstr(ex.what()));
			reply.error_code = StatusCode::CPU_CARD_DEVICE_CANNOT_FIND_CARD;
			break;
		}

		DILICard::BasicInfo stuCardBasicInfo;
		try { stuCardBasicInfo = dili_card_device.ReadCardBasicInfo(); }
		catch (std::exception& ex)
		{
			LOG_FATAL(L"读取卡片基本信息失败,错误信息: " + str_2_wstr(ex.what()));
			reply.error_code = StatusCode::DILI_CARD_DEVICE_READ_BASIC_INFO_FAILED;
			break;
		}

		LOG_TRACE(L"计算SN。");
		card_sn = stuCardBasicInfo.chip_num + std::string("44494c49") + stuCardBasicInfo.chip_num;

		if (slLevel == HardwareCmd::SL_0)
		{
			stuCardBasicInfo.device_id = card_sn;
			card_basic_info = stuCardBasicInfo;
			reply.error_code = StatusCode::OK;
			break;
		}

		if (etEncrypType == ET_PWM)
		{
			LOG_TRACE(L"加密机模式计算主控秘钥。");
			const std::string PWD_MACHINE_IP = ApplicationConfig::Instance().PWDMachineIP();
			const uint32_t PWD_MACHINE_PORT = ApplicationConfig::Instance().PWDMachinePort();
			const uint8_t TIMEOUT = ApplicationConfig::Instance().PWDMachineTimeout();

			auto& pwd_machine = PWDMachine_();

			if (!pwd_machine.Connect(PWD_MACHINE_IP, PWD_MACHINE_PORT))
			{
				LOG_ERROR(L"连接加密机失败!");
				reply.error_code = StatusCode::PWD_MACHINE_CONNECT_FAILED;
			}

			try
			{
				key_DCCK = pwd_machine.GetCardPassword(DILICard::CardKeyCode<DILICard::KeyCode::kDCCK>(), card_sn, TIMEOUT);
				key_F0015_DACK = pwd_machine.GetCardPassword(DILICard::CardKeyCode<DILICard::KeyCode::kF0015_DACK>(), card_sn, TIMEOUT);
				key_F0015_DEAK = pwd_machine.GetCardPassword(DILICard::CardKeyCode<DILICard::KeyCode::kF0015_DEAK>(), card_sn, TIMEOUT);
				key_F0015_DAMK1 = pwd_machine.GetCardPassword(DILICard::CardKeyCode<DILICard::KeyCode::kF0015_DAMK1>(), card_sn, TIMEOUT);
				key_F0005_DAMK1 = pwd_machine.GetCardPassword(DILICard::CardKeyCode<DILICard::KeyCode::kF0015_DAMK1>(), card_sn, TIMEOUT);
			}
			catch (std::exception& ex)
			{
				LOG_FATAL(L"从加密机读取密钥失败,错误信息: " + str_2_wstr(ex.what()));
				reply.error_code = StatusCode::PWD_MACHINE_GET_KEY_FAILED;
				break;
			}

			pwd_machine.Disconnect();
		}
		else if (etEncrypType == ET_DES)
		{
			LOG_TRACE(L"软加密模式计算主控秘钥。");
			try
			{
				const char* pDCCKDL = "43434bff43434bff";
				const char* pDACKDL = "41434bff41434bff";
				const char* pDEAKDL = "45414bff45414bff";
				const char* pDAMKDL = "414d4b31414d4b31";

				unsigned char aucCardSNHex[16] = { 0 };
				unsigned char aucDCCKDL[8] = { 0 }, aucDACKDL[8] = { 0 }, aucDEAKDL[8] = { 0 }, aucDAMKDL[8] = { 0 };
				EncryptTools::StringToHex((unsigned char*)pDCCKDL, 16, aucDCCKDL);
				EncryptTools::StringToHex((unsigned char*)pDACKDL, 16, aucDACKDL);
				EncryptTools::StringToHex((unsigned char*)pDEAKDL, 16, aucDEAKDL);
				EncryptTools::StringToHex((unsigned char*)pDAMKDL, 16, aucDAMKDL);
				EncryptTools::StringToHex((unsigned char*)card_sn.c_str(), 32, aucCardSNHex);

				unsigned char aucNewDAMKHex[16] = { 0 }, aucNewDCCKHex[16] = { 0 }, aucNewDACKHex[16] = { 0 }, aucNewDEAKHex[16] = { 0 };
				unsigned char aucNewDAMK[32] = { 0 }, aucNewDCCK[32] = { 0 }, aucNewDACK[32] = { 0 }, aucNewDEAK[32] = { 0 };

				EncryptTools::Encrypt(aucCardSNHex, 16, aucDAMKDL, aucNewDAMKHex);
				EncryptTools::Encrypt(aucCardSNHex, 16, aucDCCKDL, aucNewDCCKHex);
				EncryptTools::Encrypt(aucCardSNHex, 16, aucDACKDL, aucNewDACKHex);
				EncryptTools::Encrypt(aucCardSNHex, 16, aucDEAKDL, aucNewDEAKHex);

				EncryptTools::HexToChar(aucNewDAMKHex, 16, aucNewDAMK);
				EncryptTools::HexToChar(aucNewDCCKHex, 16, aucNewDCCK);
				EncryptTools::HexToChar(aucNewDACKHex, 16, aucNewDACK);
				EncryptTools::HexToChar(aucNewDEAKHex, 16, aucNewDEAK);

				std::string sDAMKKey((char*)aucNewDAMK, 32);
				std::string sDCCKKey((char*)aucNewDCCK, 32);
				std::string sDACKKey((char*)aucNewDACK, 32);
				std::string sDEAKKey((char*)aucNewDEAK, 32);

				key_F0005_DAMK1 = key_F0015_DAMK1 = sDAMKKey;
				key_DCCK = sDCCKKey;
				key_F0015_DACK = sDACKKey;
				key_F0015_DEAK = sDEAKKey;
				
			}
			catch (std::exception& ex)
			{
				LOG_FATAL(L"软加密密钥失败");
				reply.error_code = StatusCode::PWD_MACHINE_GET_KEY_FAILED;
				break;
			}
		}

		try
		{
			dili_card_device.ActivateCard(card_sn, key_DCCK, key_F0015_DACK, key_F0015_DEAK, key_F0015_DAMK1);
			card_basic_info = dili_card_device.ReadCardBasicInfo();
		}
		catch (std::exception& ex)
		{
			LOG_FATAL(L"卡片激活失败,错误信息: " + str_2_wstr(ex.what()));
			reply.error_code = StatusCode::DILI_CARD_DEVICE_ACTIVATE_CARD_FAILED;
			break;
		}
		
	} while (0);
	
	dili_card_device.Disconnect();
	if (boost::iequals(reply.error_code, StatusCode::OK))
		LOG_TRACE(L"卡片激活成功。");

	reply.data.put("chipNo", card_basic_info.chip_num);
	reply.data.put("deviceId", card_basic_info.device_id);
	reply.data.put("type", card_basic_info.type_code);
	reply.data.put("issuerCode", card_basic_info.issuer_code);
	reply.data.put("verifyCode", card_basic_info.verify_code);
	return reply;
}

ReadPINPadCmd::Reply ResetDILICardCmd::Execute(const std::string& input)
{
	//const uint8_t BEEP_TIME = 20; // 20 * 10ms
	

	LOG_TRACE(L"卡片撤销激活。");
	auto& dili_card_device = DILICardRWDevice();
	
	Reply reply;
	std::string card_sn;
	std::string key_DCCK, key_F0015_DACK;
	DILICard::BasicInfo card_basic_info;

	SecurityLevel slLevel = (SecurityLevel)ApplicationConfig::Instance().SecurityLevel();
	EncryptType etEncrypType = (EncryptType)ApplicationConfig::Instance().EncryptType();

	do
	{
		try
		{
			dili_card_device.Connect();
		}
		catch (std::exception& ex)
		{
			LOG_FATAL(L"连接读卡器失败,错误信息: " + str_2_wstr(ex.what()));
			reply.error_code = StatusCode::CPU_CARD_DEVICE_CONNECT_FAILED;
			break;
		}
		try
		{
			dili_card_device.FindCard();
		}
		catch (std::exception& ex)
		{
			LOG_FATAL(L"寻卡失败,错误信息: " + str_2_wstr(ex.what()));
			reply.error_code = StatusCode::CPU_CARD_DEVICE_CANNOT_FIND_CARD;
			break;
		}

		DILICard::BasicInfo stuCardBasicInfo;
		try { stuCardBasicInfo = dili_card_device.ReadCardBasicInfo(); }
		catch (std::exception& ex)
		{
			LOG_FATAL(L"读取卡片基本信息失败,错误信息: " + str_2_wstr(ex.what()));
			reply.error_code = StatusCode::DILI_CARD_DEVICE_READ_BASIC_INFO_FAILED;
			break;
		}
		
		if (slLevel == HardwareCmd::SL_0)
		{
			card_basic_info = stuCardBasicInfo;
			reply.error_code = StatusCode::OK;
			break;
		}

		//card_sn = stuCardBasicInfo.device_id;

		LOG_TRACE(L"计算SN。");
		if (slLevel == HardwareCmd::SL_1)
		{
			card_sn = stuCardBasicInfo.device_id;
		}
		else if (slLevel == HardwareCmd::SL_2)
		{
			card_sn = stuCardBasicInfo.chip_num + "44494c49" + stuCardBasicInfo.chip_num;
		}

		if (etEncrypType == ET_PWM)
		{
			LOG_TRACE(L"加密机模式计算主控秘钥。");
			const std::string PWD_MACHINE_IP = ApplicationConfig::Instance().PWDMachineIP();
			const uint32_t PWD_MACHINE_PORT = ApplicationConfig::Instance().PWDMachinePort();
			const uint8_t TIMEOUT = ApplicationConfig::Instance().PWDMachineTimeout();

			auto& pwd_machine = PWDMachine_();

			if (!pwd_machine.Connect(PWD_MACHINE_IP, PWD_MACHINE_PORT))
			{
				LOG_ERROR(L"连接加密机失败!");
				reply.error_code = StatusCode::PWD_MACHINE_CONNECT_FAILED;
			}

			try
			{
				key_DCCK = pwd_machine.GetCardPassword(DILICard::CardKeyCode<DILICard::KeyCode::kDCCK>(), card_sn, TIMEOUT);
				key_F0015_DACK = pwd_machine.GetCardPassword(DILICard::CardKeyCode<DILICard::KeyCode::kF0015_DACK>(), card_sn, TIMEOUT);
			}
			catch (std::exception& ex)
			{
				LOG_FATAL(L"从加密机读取密钥失败,错误信息: " + str_2_wstr(ex.what()));
				reply.error_code = StatusCode::PWD_MACHINE_GET_KEY_FAILED;
				break;
			}

			pwd_machine.Disconnect();
		}
		else if (etEncrypType == ET_DES)
		{
			LOG_TRACE(L"软加密模式计算主控秘钥。");
			try
			{
				const char* pDCCKDL = "43434bff43434bff";
				const char* pDACKDL = "41434bff41434bff";

				unsigned char aucCardSNHex[16] = { 0 };
				unsigned char aucDCCKDL[8] = { 0 }, aucDACKDL[8] = { 0 };
				EncryptTools::StringToHex((unsigned char*)pDCCKDL, 16, aucDCCKDL);
				EncryptTools::StringToHex((unsigned char*)pDACKDL, 16, aucDACKDL);
				EncryptTools::StringToHex((unsigned char*)card_sn.c_str(), 32, aucCardSNHex);

				unsigned char aucNewDCCKHex[16] = { 0 }, aucNewDACKHex[16] = { 0 };
				unsigned char aucNewDCCK[32] = { 0 }, aucNewDACK[32] = { 0 };

				EncryptTools::Encrypt(aucCardSNHex, 16, aucDCCKDL, aucNewDCCKHex);
				EncryptTools::Encrypt(aucCardSNHex, 16, aucDACKDL, aucNewDACKHex);

				EncryptTools::HexToChar(aucNewDCCKHex, 16, aucNewDCCK);
				EncryptTools::HexToChar(aucNewDACKHex, 16, aucNewDACK);

				std::string sDCCKKey((char*)aucNewDCCK, 32);
				std::string sDACKKey((char*)aucNewDACK, 32);

				key_DCCK = sDCCKKey;
				key_F0015_DACK = sDACKKey;

			}
			catch (std::exception& ex)
			{
				LOG_FATAL(L"软加密密钥失败");
				reply.error_code = StatusCode::PWD_MACHINE_GET_KEY_FAILED;
				break;
			}
		}

		if (slLevel == HardwareCmd::SL_2)
		{
			LOG_TRACE(L"验证卡片。");
			if (!dili_card_device.AuthenticationKey(key_DCCK))
			{
				LOG_FATAL(L"秘钥验证失败,卡片需要升级!");
				reply.error_code = StatusCode::DILI_CARD_DEVICE_NEED_UPDATE;
				break;
			}
		}
		

		try
		{
			dili_card_device.ResetCard(key_DCCK, key_F0015_DACK);
			card_basic_info = dili_card_device.ReadCardBasicInfo();
		}
		catch (std::exception& ex)
		{
			LOG_FATAL(L"卡片撤销激活失败,错误信息: " + str_2_wstr(ex.what()));
			reply.error_code = StatusCode::DILI_CARD_DEVICE_RESET_CARD_FAILED;
			break;
		}
	} while (0);
	dili_card_device.Disconnect();
	if (boost::iequals(reply.error_code, StatusCode::OK))
		LOG_TRACE(L"卡片撤销激活成功。");

	reply.data.put("chipNo", card_basic_info.chip_num);
	reply.data.put("deviceId", card_basic_info.device_id);
	reply.data.put("type", card_basic_info.type_code);
	reply.data.put("issuerCode", card_basic_info.issuer_code);
	reply.data.put("verifyCode", card_basic_info.verify_code);
	return reply;
}

ReadPINPadCmd::Reply ReadDILICardBasicInfoCmd::Execute(const std::string& input)
{
	LOG_TRACE(L"读取卡片基本信息。");
	auto& dili_card_device = DILICardRWDevice();
	Reply reply;
	DILICard::BasicInfo card_basic_info;
	do
	{
		try 
		{ 
			dili_card_device.Connect(); 
		}
		catch (std::exception& ex)
		{
			LOG_FATAL(L"连接读卡器失败,错误信息: " + str_2_wstr(ex.what()));
			reply.error_code = StatusCode::CPU_CARD_DEVICE_CONNECT_FAILED;
			break;
		}
		try
		{
			dili_card_device.FindCard();
		}
		catch (std::exception& ex)
		{
			LOG_FATAL(L"寻卡失败,错误信息: " + str_2_wstr(ex.what()));
			reply.error_code = StatusCode::CPU_CARD_DEVICE_CANNOT_FIND_CARD;
			break;
		}

		try { card_basic_info = dili_card_device.ReadCardBasicInfo(); }
		catch (std::exception& ex)
		{
			LOG_FATAL(L"读取卡片基本信息失败,错误信息: " + str_2_wstr(ex.what()));
			reply.error_code = StatusCode::DILI_CARD_DEVICE_READ_BASIC_INFO_FAILED;
			break;
		}

		
		if (SL_2 == ApplicationConfig::Instance().SecurityLevel())
		{
			LOG_TRACE(L"计算主控秘钥。");
			std::string sSN = card_basic_info.chip_num + "44494c49" + card_basic_info.chip_num;
			std::string sDCCK;

			if (ET_DES == ApplicationConfig::Instance().EncryptType())
			{
				LOG_TRACE(L"软加密模式计算主控秘钥。");
				const char* pDCCKDL = "43434bff43434bff";

				unsigned char aucCardSNHex[16] = { 0 };
				unsigned char aucDCCKDL[8] = { 0 };
				EncryptTools::StringToHex((unsigned char*)pDCCKDL, 16, aucDCCKDL);
				EncryptTools::StringToHex((unsigned char*)sSN.c_str(), 32, aucCardSNHex);

				unsigned char aucNewDCCKHex[16] = { 0 };
				unsigned char aucNewDCCK[32] = { 0 };

				EncryptTools::Encrypt(aucCardSNHex, 16, aucDCCKDL, aucNewDCCKHex);

				EncryptTools::HexToChar(aucNewDCCKHex, 16, aucNewDCCK);

				std::string sDCCKKey((char*)aucNewDCCK, 32);
				
				sDCCK = sDCCKKey;
			}
			else if (ET_PWM == ApplicationConfig::Instance().EncryptType())
			{
				LOG_TRACE(L"加密机模式计算主控秘钥。");
				const std::string PWD_MACHINE_IP = ApplicationConfig::Instance().PWDMachineIP();
				const uint32_t PWD_MACHINE_PORT = ApplicationConfig::Instance().PWDMachinePort();
				const uint8_t TIMEOUT = ApplicationConfig::Instance().PWDMachineTimeout();

				auto& pwd_machine = PWDMachine_();

				if (!pwd_machine.Connect(PWD_MACHINE_IP, PWD_MACHINE_PORT))
				{
					LOG_ERROR(L"连接加密机失败!");
					reply.error_code = StatusCode::PWD_MACHINE_CONNECT_FAILED;
				}

				try
				{
					sDCCK = pwd_machine.GetCardPassword(DILICard::CardKeyCode<DILICard::KeyCode::kDCCK>(), sSN, TIMEOUT);
				}
				catch (std::exception& ex)
				{
					LOG_FATAL(L"从加密机读取密钥失败,错误信息: " + str_2_wstr(ex.what()));
					reply.error_code = StatusCode::PWD_MACHINE_GET_KEY_FAILED;
					break;
				}
				pwd_machine.Disconnect();
			}

			LOG_TRACE(L"验证卡片。");
			if (!dili_card_device.AuthenticationKey(sDCCK))
			{
				LOG_FATAL(L"秘钥验证失败,卡片需要升级!");
				reply.error_code = StatusCode::DILI_CARD_DEVICE_NEED_UPDATE;
				break;
			}

		}
		
	} while (0);
	dili_card_device.Disconnect();
	if (boost::iequals(reply.error_code, StatusCode::OK))
		LOG_TRACE(L"读取卡片基本信息成功。");

	reply.data.put("chipNo", card_basic_info.chip_num);
	reply.data.put("deviceId", card_basic_info.device_id);
	reply.data.put("type", card_basic_info.type_code);
	reply.data.put("issuerCode", card_basic_info.issuer_code);
	reply.data.put("verifyCode", card_basic_info.verify_code);
	g_sCardNum = card_basic_info.device_id;
	return reply;
}

ReadPINPadCmd::Reply ReadDILICardServiceInfoCmd::Execute(const std::string& input)
{
	SecurityLevel slLevel = (SecurityLevel)ApplicationConfig::Instance().SecurityLevel();
	EncryptType etEncrypType = (EncryptType)ApplicationConfig::Instance().EncryptType();
	LOG_TRACE(L"读取卡片业务信息。");
	auto& dili_card_device = DILICardRWDevice();
	
	Reply reply;

	//安全等级都为0了,还读个毛的业务信息,直接返回!
	if (slLevel == HardwareCmd::SL_0)
	{
		reply.error_code == StatusCode::OK;
		LOG_TRACE(L"读取卡片业务信息成功。");
		return reply;
	}

	std::string key_r;
	std::string service_data;
	do
	{
		try
		{
			dili_card_device.Connect();
		}
		catch (std::exception& ex)
		{
			LOG_FATAL(L"连接读卡器失败,错误信息: " + str_2_wstr(ex.what()));
			reply.error_code = StatusCode::CPU_CARD_DEVICE_CONNECT_FAILED;
			break;
		}
		try
		{
			dili_card_device.FindCard();
		}
		catch (std::exception& ex)
		{
			LOG_FATAL(L"寻卡失败,错误信息: " + str_2_wstr(ex.what()));
			reply.error_code = StatusCode::CPU_CARD_DEVICE_CANNOT_FIND_CARD;
			break;
		}

		DILICard::BasicInfo stuCardBasicInfo;
		try { stuCardBasicInfo = dili_card_device.ReadCardBasicInfo(); }
		catch (std::exception& ex)
		{
			LOG_FATAL(L"读取卡片基本信息失败,错误信息: " + str_2_wstr(ex.what()));
			reply.error_code = StatusCode::DILI_CARD_DEVICE_READ_BASIC_INFO_FAILED;
			break;
		}

		LOG_TRACE(L"计算SN。");
		std::string sSN;
		if (slLevel == HardwareCmd::SL_1)
		{
			sSN = stuCardBasicInfo.device_id;
		}
		else if (slLevel == HardwareCmd::SL_2)
		{
			sSN = stuCardBasicInfo.chip_num + "44494c49" + stuCardBasicInfo.chip_num;
		}

		if (etEncrypType == ET_PWM)
		{
			LOG_TRACE(L"加密机模式计算主控秘钥。");
			auto& pwd_machine = PWDMachine_();
			const std::string PWD_MACHINE_IP = ApplicationConfig::Instance().PWDMachineIP();
			const uint32_t PWD_MACHINE_PORT = ApplicationConfig::Instance().PWDMachinePort();
			const uint8_t TIMEOUT = ApplicationConfig::Instance().PWDMachineTimeout();

			if (!pwd_machine.Connect(PWD_MACHINE_IP, PWD_MACHINE_PORT))
			{
				LOG_ERROR(L"连接加密机失败!");
				reply.error_code = StatusCode::PWD_MACHINE_CONNECT_FAILED;
				break;
			}

			try { key_r = pwd_machine.GetCardPassword(DILICard::CardKeyCode<DILICard::KeyCode::kF0015_DEAK>(), sSN, TIMEOUT); }
			catch (std::exception& ex)
			{
				LOG_FATAL(L"从加密机读取密钥失败,错误信息: " + str_2_wstr(ex.what()));
				reply.error_code = StatusCode::PWD_MACHINE_GET_KEY_FAILED;
				break;
			}

			pwd_machine.Disconnect();
		}
		else if (etEncrypType == ET_DES)
		{
			LOG_TRACE(L"软加密模式计算主控秘钥。");
			const char* pDEAKDL = "45414bff45414bff";

			unsigned char aucCardSNHex[16] = { 0 };
			unsigned char aucDEAKDL[8] = { 0 };
			EncryptTools::StringToHex((unsigned char*)pDEAKDL, 16, aucDEAKDL);
			EncryptTools::StringToHex((unsigned char*)sSN.c_str(), 32, aucCardSNHex);

			unsigned char aucNewDEAKHex[16] = { 0 };
			unsigned char aucNewDEAK[32] = { 0 };

			EncryptTools::Encrypt(aucCardSNHex, 16, aucDEAKDL, aucNewDEAKHex);

			EncryptTools::HexToChar(aucNewDEAKHex, 16, aucNewDEAK);

			std::string sDEAKKey((char*)aucNewDEAK, 32);

			key_r = sDEAKKey;
		}

		try { service_data = dili_card_device.ReadCardServiceInfo(key_r); }
		catch (std::exception& ex)
		{
			LOG_FATAL(L"读取卡片业务信息失败,错误信息: " + str_2_wstr(ex.what()));
			reply.error_code = StatusCode::DILI_CARD_DEVICE_READ_SERVICE_INFO_FAILED;
			break;
		}
	} while (0);
	
	dili_card_device.Disconnect();
	if (boost::iequals(reply.error_code, StatusCode::OK))
		LOG_TRACE(L"读取卡片业务信息成功。");

	reply.data.put("", service_data);
	return reply;
}

ReadPINPadCmd::Reply WriteDILICardServiceInfoCmd::Execute(const std::string& input)
{
	const char DATA_SPLITER = ':';
	SecurityLevel slLevel = (SecurityLevel)ApplicationConfig::Instance().SecurityLevel();
	EncryptType etEncrypType = (EncryptType)ApplicationConfig::Instance().EncryptType();

	LOG_TRACE(L"写入卡片业务信息。");
	auto& dili_card_device = DILICardRWDevice();
	
	Reply reply;
	//std::string card_sn;
	std::string key_w;
	do
	{
		try
		{
			dili_card_device.Connect();
		}
		catch (std::exception& ex)
		{
			LOG_FATAL(L"连接读卡器失败,错误信息: " + str_2_wstr(ex.what()));
			reply.error_code = StatusCode::CPU_CARD_DEVICE_CONNECT_FAILED;
			break;
		}

		try
		{
			dili_card_device.FindCard();
		}
		catch (std::exception& ex)
		{
			LOG_FATAL(L"寻卡失败,错误信息: " + str_2_wstr(ex.what()));
			reply.error_code = StatusCode::CPU_CARD_DEVICE_CANNOT_FIND_CARD;
			break;
		}

		DILICard::BasicInfo stuCardBasicInfo;
		try { stuCardBasicInfo = dili_card_device.ReadCardBasicInfo(); }
		catch (std::exception& ex)
		{
			LOG_FATAL(L"读取卡片基本信息失败,错误信息: " + str_2_wstr(ex.what()));
			reply.error_code = StatusCode::DILI_CARD_DEVICE_READ_BASIC_INFO_FAILED;
			break;
		}

		//安全等级都为0了,还写个毛的业务信息,直接返回!
		if (slLevel == HardwareCmd::SL_0)
		{
			reply.error_code == StatusCode::OK;
			reply.data.put("", "");
			LOG_TRACE(L"读取卡片业务信息成功。");
			break;
		}

		LOG_TRACE(L"计算SN。");
		std::string sSN;
		if (slLevel == HardwareCmd::SL_1)
		{
			sSN = stuCardBasicInfo.device_id;
		}
		else if (slLevel == HardwareCmd::SL_2)
		{
			sSN = stuCardBasicInfo.chip_num + "44494c49" + stuCardBasicInfo.chip_num;
		}


		std::size_t index = input.find(DATA_SPLITER);
		if (std::string::npos == index) // NOT found.
		{
			LOG_ERROR(L"写入卡片业务信息失败,错误信息:请求数据错误。");
			reply.error_code = StatusCode::DILI_CARD_DEVICE_WRITE_SERVICE_INFO_FAILED;
			break;
		}
		std::string device_id = g_sCardNum;//用于办卡时,保证操作的卡片是同一张;
		std::string service_data = input.substr(index + 1, input.size() - index - 1);

		// 验证操作的是否是同一张卡,即device_id是否匹配
		if (!boost::iequals(sSN, device_id))
		{
			LOG_ERROR(L"写卡卡片的序列号不匹配!");
			reply.error_code = StatusCode::DILI_CARD_DEVICE_WRITE_UNMATCHED_CARD;
			break;
		}

		if (etEncrypType == ET_PWM)
		{
			LOG_TRACE(L"加密机模式计算主控秘钥。");
			auto& pwd_machine = PWDMachine_();

			const std::string PWD_MACHINE_IP = ApplicationConfig::Instance().PWDMachineIP();
			const uint32_t PWD_MACHINE_PORT = ApplicationConfig::Instance().PWDMachinePort();
			const uint8_t TIMEOUT = ApplicationConfig::Instance().PWDMachineTimeout();

			if (!pwd_machine.Connect(PWD_MACHINE_IP, PWD_MACHINE_PORT))
			{
				LOG_ERROR(L"连接加密机失败!");
				reply.error_code = StatusCode::PWD_MACHINE_CONNECT_FAILED;
				break;
			}

			try { key_w = pwd_machine.GetCardPassword(DILICard::CardKeyCode<DILICard::KeyCode::kF0015_DAMK1>(), sSN, TIMEOUT); }
			catch (std::exception& ex)
			{
				LOG_FATAL(L"从加密机读取密钥失败,错误信息: " + str_2_wstr(ex.what()));
				reply.error_code = StatusCode::PWD_MACHINE_GET_KEY_FAILED;
				break;
			}

			pwd_machine.Disconnect();
		}
		else if (etEncrypType == ET_DES)
		{
			LOG_TRACE(L"软加密模式计算15写秘钥。");
			const char* pDAMKDL = "414d4b31414d4b31";

			unsigned char aucCardSNHex[16] = { 0 };
			unsigned char aucDAMKDL[8] = { 0 };
			EncryptTools::StringToHex((unsigned char*)pDAMKDL, 16, aucDAMKDL);
			EncryptTools::StringToHex((unsigned char*)sSN.c_str(), 32, aucCardSNHex);

			unsigned char aucNewDAMKHex[16] = { 0 };
			unsigned char aucNewDAMK[32] = { 0 };

			EncryptTools::Encrypt(aucCardSNHex, 16, aucDAMKDL, aucNewDAMKHex);

			EncryptTools::HexToChar(aucNewDAMKHex, 16, aucNewDAMK);

			std::string sDAMKKey((char*)aucNewDAMK, 32);

			key_w = sDAMKKey;
		}
		
		try { dili_card_device.WriteCardServiceInfo(key_w, service_data); }
		catch (std::exception& ex)
		{
			LOG_FATAL(L"写入卡片业务信息失败,错误信息: " + str_2_wstr(ex.what()));
			reply.error_code = StatusCode::DILI_CARD_DEVICE_WRITE_SERVICE_INFO_FAILED;
			break;
		}

		
	} while (0);
	
	dili_card_device.Disconnect();
	if (boost::iequals(reply.error_code, StatusCode::OK))
		LOG_TRACE(L"写入卡片业务信息成功。");
	reply.data.put("", "");
	return reply;
}

ReadPINPadCmd::Reply ReadBankCardNumCmd::Execute(const std::string& input)
{
	LOG_TRACE(L"读取银行卡号。");
	auto& bank_card_reader = BankCardReader_();
	std::string bank_card_no("");
	Reply reply;
	do
	{
		try
		{
			if (!bank_card_reader.Connect())
			{
				LOG_ERROR(L"连接银行卡读卡器失败。");
				reply.error_code = StatusCode::CPU_CARD_DEVICE_CONNECT_FAILED;
				break;
			}
			bank_card_no = bank_card_reader.ReadBankCardNum();
		}
		catch (std::exception& ex)
		{
			LOG_FATAL(L"读取银行卡号失败,错误信息: " + str_2_wstr(ex.what()));
			reply.error_code = StatusCode::BANK_CARD_READ_CARD_NUM_FAILED;
			break;
		}
	} while (0);
	bank_card_reader.Disconnect();
	if (boost::iequals(reply.error_code, StatusCode::OK))
		LOG_TRACE(L"读取银行卡号成功。");

	reply.data.put("chipNo", bank_card_no);
	return reply;
}