Commit d0078d4d by kevin

add des encrypt and limit num

1 parent c994deb0
...@@ -32,7 +32,7 @@ void CZ_LK_CHECKIT() ...@@ -32,7 +32,7 @@ void CZ_LK_CHECKIT()
verify.get_chip_id(chip_id, len); verify.get_chip_id(chip_id, len);
printf("\n"); printf("\n");
verify.read_chip_id_from_nvm(chip_id_from_nvm, len); verify.read_chip_id_limit_num_from_nvm(chip_id_from_nvm, len);
int is_sim = compare_chip_id_hardware_nvm(chip_id, chip_id_from_nvm, 8); int is_sim = compare_chip_id_hardware_nvm(chip_id, chip_id_from_nvm, 8);
if (is_sim > 0) if (is_sim > 0)
...@@ -50,8 +50,8 @@ void CZ_LK_CHECKIT() ...@@ -50,8 +50,8 @@ void CZ_LK_CHECKIT()
} }
} }
void CZ_LK_CHECKIT_WITH_STEP() void CZ_LK_CHECKIT_time_to_time()
{//check with each step {//check it from time to time
srand((unsigned int)time(NULL)); srand((unsigned int)time(NULL));
const int MAX = 10000; const int MAX = 10000;
const int step = 3600; const int step = 3600;
......
#ifndef __FUCBASE_H__
#define __FUCBASE_H__
#define UKEY_RIGHT 0
#define UKEY_WRONG -1
CString DelStr(CString strIn);
int Hex2Byte(CString strIn, BYTE *datOut);
#endif
\ No newline at end of file
...@@ -4,7 +4,9 @@ ...@@ -4,7 +4,9 @@
#include <iostream> #include <iostream>
#include <WinBase.h> #include <WinBase.h>
#include "FucBase.h" #include "des.h"
//#include "FucBase.h"
/* /*
void LK_SEETA_VERIFY::lk_seeta_establish_context() void LK_SEETA_VERIFY::lk_seeta_establish_context()
...@@ -348,83 +350,83 @@ int Hex2Byte(std::string strIn, BYTE *datOut) ...@@ -348,83 +350,83 @@ int Hex2Byte(std::string strIn, BYTE *datOut)
return (leth); return (leth);
} }
void LK_SEETA_VERIFY::contrast_authenticate() //void LK_SEETA_VERIFY::contrast_authenticate()
{// not work //{// not work
char* command = "8008 0000 09 03 1122334455667788";//8008 0000 09 03 1122334455667788 // char* command = "8008 0000 09 03 1122334455667788";//8008 0000 09 03 1122334455667788
std::cout << "command:" << command << std::endl; // std::cout << "command:" << command << std::endl;
//
INT lenSnd, lenRcv, res, len; // INT lenSnd, lenRcv, res, len;
BYTE cmdAPDU[0x200], outBuf[0x200]; // BYTE cmdAPDU[0x200], outBuf[0x200];
//
lenSnd = Hex2Byte(command, cmdAPDU); // lenSnd = Hex2Byte(command, cmdAPDU);
//
res = SendAPDU(lenSnd, cmdAPDU, &lenRcv, outBuf); // res = SendAPDU(lenSnd, cmdAPDU, &lenRcv, outBuf);
printf("outBuf:"); // printf("outBuf:");
for (int i = 0; i < lenRcv; ++i) // for (int i = 0; i < lenRcv; ++i)
{ // {
//printf("%d:%x,%d ", i, outBuf[i], outBuf[i]); // //printf("%d:%x,%d ", i, outBuf[i], outBuf[i]);
printf("%d:%x ", i, outBuf[i]); // printf("%d:%x ", i, outBuf[i]);
} // }
//
if (0 != res) // if (0 != res)
{ // {
char buf[100]; // char buf[100];
sprintf(buf, "command execute error:%d\n", res); // sprintf(buf, "command execute error:%d\n", res);
//
std::string msg(buf); // std::string msg(buf);
std::cout << msg.c_str() << std::endl; // std::cout << msg.c_str() << std::endl;
throw msg.c_str(); // throw msg.c_str();
} // }
//
BYTE inoutdata[128]; // BYTE inoutdata[128];
BYTE keyStr[128]; // BYTE keyStr[128];
memset(inoutdata, 0, 0x20); // memset(inoutdata, 0, 0x20);
memcpy(inoutdata, "\x08\x11\x22\x33\x44\x55\x66\x77\x88", 0x09); //注意第一个字节为加密明文长度 // memcpy(inoutdata, "\x08\x11\x22\x33\x44\x55\x66\x77\x88", 0x09); //注意第一个字节为加密明文长度
memcpy(keyStr, "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x00\x00", 0x10); // memcpy(keyStr, "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x00\x00", 0x10);
//
//
printf("\n"); // printf("\n");
printf("inoutdata:"); // printf("inoutdata:");
for (int i = 0; i < 17; ++i) // for (int i = 0; i < 17; ++i)
{ // {
printf("%d:%x ", i, inoutdata[i]); // printf("%d:%x ", i, inoutdata[i]);
} // }
//
printf("\n"); // printf("\n");
printf("keyStr:"); // printf("keyStr:");
for (int i = 0; i < 16; ++i) // for (int i = 0; i < 16; ++i)
{ // {
printf("%d:%x ", i, keyStr[i]); // printf("%d:%x ", i, keyStr[i]);
} // }
printf("\n"); // printf("\n");
//
//xor with random inside // //xor with random inside
for (int i = 1; i < 9; ++i) // for (int i = 1; i < 9; ++i)
{//index 0 length of plaintext // {//index 0 length of plaintext
inoutdata[i] ^= outBuf[i]; // inoutdata[i] ^= outBuf[i];
} // }
//3DES加密 // //3DES加密
encrypt_3des(inoutdata, keyStr); // encrypt_3des(inoutdata, keyStr);
//
printf("\n"); // printf("\n");
printf("des result:"); // printf("des result:");
for (int i = 9; i < 17; ++i) // for (int i = 9; i < 17; ++i)
{ // {
printf("%d:%x ", i, inoutdata[i]); // printf("%d:%x ", i, inoutdata[i]);
} // }
//
int result = compare_des_inside_outside(inoutdata + 9, outBuf + 9, 8); // int result = compare_des_inside_outside(inoutdata + 9, outBuf + 9, 8);
//
if (result < 0) // if (result < 0)
{ // {
char buf[100]; // char buf[100];
sprintf(buf, "authorize failed!\n"); // sprintf(buf, "authorize failed!\n");
//
std::string msg(buf); // std::string msg(buf);
std::cout << msg.c_str() << std::endl; // std::cout << msg.c_str() << std::endl;
throw msg.c_str(); // throw msg.c_str();
} // }
} //}
void LK_SEETA_VERIFY::get_chip_id(unsigned char chip_id[], int &lenRcv) void LK_SEETA_VERIFY::get_chip_id(unsigned char chip_id[], int &lenRcv)
{ {
...@@ -445,23 +447,49 @@ void LK_SEETA_VERIFY::get_chip_id(unsigned char chip_id[], int &lenRcv) ...@@ -445,23 +447,49 @@ void LK_SEETA_VERIFY::get_chip_id(unsigned char chip_id[], int &lenRcv)
} }
} }
void LK_SEETA_VERIFY::write_chip_id_to_nvm() void LK_SEETA_VERIFY::write_chip_id_limit_num_to_nvm()
{ {
uint8_t DES_ENCRYPT_DECRYPT_KEY[128];
memcpy(DES_ENCRYPT_DECRYPT_KEY, "\x01\x09\x09\01\x00\x07\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00", 0x10);
unsigned char chip_id[300]; unsigned char chip_id[300];
int len; int len;
get_chip_id(chip_id, len); get_chip_id(chip_id, len);
//for (int i = 0; i < len; ++i)
//{
// printf("%d:%x ", i, chip_id[i]);
//}
unsigned char cmd_write_chip[300]; unsigned char cmd_write_chip[300];
unsigned char chip_id_and_limit_num[300];//mingwen
unsigned char chip_id_and_limit_num_des[300];//des chip id and limit num
memset(cmd_write_chip, 0, 300); memset(cmd_write_chip, 0, 300);
memcpy(cmd_write_chip, "\x80\x08\x00\x00\x09\x04", 6); memset(chip_id_and_limit_num, 0, 300);
memcpy(cmd_write_chip, chip_id, 8); memset(chip_id_and_limit_num_des, 0, 300);
memcpy(cmd_write_chip, "\x80\x08\x00\x00\x11\x04", 6);
memcpy(chip_id_and_limit_num, "\x10", 1);
memcpy(chip_id_and_limit_num + 1, chip_id, 8);
memcpy(chip_id_and_limit_num + 9, "\x05\x00\x00\x01\x00\x00\x00\x00", 8);// limit==10000
printf("write into:\n");
for (int i = 0; i < 17; ++i)
{
printf("%d:%x ", i, chip_id_and_limit_num[i]);
}
encrypt_3des(chip_id_and_limit_num, DES_ENCRYPT_DECRYPT_KEY);
memcpy(chip_id_and_limit_num_des, chip_id_and_limit_num + 17, 16);
memcpy(cmd_write_chip + 6, chip_id_and_limit_num_des, 16);
unsigned char rec[300]; unsigned char rec[300];
int rec_len; int rec_len;
int res = SendAPDU(14, cmd_write_chip, &rec_len, rec);//write to nvm int res = SendAPDU(22, cmd_write_chip, &rec_len, rec);//write to nvm
//for (int i = 0; i < 14; ++i) //for (int i = 0; i < 14; ++i)
//{ //{
// printf("%d:%x ", i, cmd_write_chip[i]); // printf("%2x ",cmd_write_chip[i]);
//} //}
if (0 != res) if (0 != res)
...@@ -477,15 +505,32 @@ void LK_SEETA_VERIFY::write_chip_id_to_nvm() ...@@ -477,15 +505,32 @@ void LK_SEETA_VERIFY::write_chip_id_to_nvm()
printf("write id to nvm succeed!\n"); printf("write id to nvm succeed!\n");
} }
void LK_SEETA_VERIFY::read_chip_id_from_nvm(unsigned char chip_id_from_nvm[], int &lenRcv) void LK_SEETA_VERIFY::read_chip_id_limit_num_from_nvm(unsigned char chip_id_from_nvm[], int &lenRcv)
{ {
uint8_t DES_ENCRYPT_DECRYPT_KEY[128];
memcpy(DES_ENCRYPT_DECRYPT_KEY, "\x01\x09\x09\01\x00\x07\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00", 0x10);
unsigned char cmd_read_chip_id[300]; unsigned char cmd_read_chip_id[300];
unsigned char rec[300]; unsigned char rec[300];
unsigned char in_out_data[300];
int rec_len; int rec_len;
memset(cmd_read_chip_id, 0, 300); memset(cmd_read_chip_id, 0, 300);
memcpy(cmd_read_chip_id, "\x80\x08\x00\x00\x01\x05", 6); memcpy(cmd_read_chip_id, "\x80\x08\x00\x00\x01\x05", 6);
int res = SendAPDU(14, cmd_read_chip_id, &rec_len, rec);//read from nvm int res = SendAPDU(14, cmd_read_chip_id, &rec_len, rec);//read from nvm
memset(in_out_data, 0 ,300);
memcpy(in_out_data, "\x10", 1);
memcpy(in_out_data + 1, rec, 16);
decrypt_3des(in_out_data, DES_ENCRYPT_DECRYPT_KEY);
printf("read from:\n");
for (int i = 0; i < 17; ++i)
{
printf("%d:%x ", i, in_out_data[i]);
}
if (0 != res) if (0 != res)
{ {
char buf[100]; char buf[100];
...@@ -502,7 +547,7 @@ void LK_SEETA_VERIFY::read_chip_id_from_nvm(unsigned char chip_id_from_nvm[], in ...@@ -502,7 +547,7 @@ void LK_SEETA_VERIFY::read_chip_id_from_nvm(unsigned char chip_id_from_nvm[], in
//} //}
lenRcv = 8; lenRcv = 8;
memmove(chip_id_from_nvm, rec, 8); memmove(chip_id_from_nvm, in_out_data + 17, 8);
return; return;
} }
\ No newline at end of file
...@@ -22,9 +22,9 @@ typedef int(__stdcall *pSendAPDU)(int, unsigned char*, int*, unsigned char*); ...@@ -22,9 +22,9 @@ typedef int(__stdcall *pSendAPDU)(int, unsigned char*, int*, unsigned char*);
void get_chip_id(unsigned char chip_id[], int &lenRcv);//get chip id from hardware void get_chip_id(unsigned char chip_id[], int &lenRcv);//get chip id from hardware
void write_chip_id_to_nvm();//get chip id and write to nvm void write_chip_id_limit_num_to_nvm();//get chip id and limit num, then write to nvm
void read_chip_id_from_nvm(unsigned char chip_id_from_nvm[], int &lenRcv); void read_chip_id_limit_num_from_nvm(unsigned char chip_id_from_nvm[], int &lenRcv);
private: private:
HMODULE g_hDll; //̬ HMODULE g_hDll; //̬
pConnect ConnectUkey; pConnect ConnectUkey;
......
...@@ -56,15 +56,19 @@ ...@@ -56,15 +56,19 @@
</ImportGroup> </ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
<Import Project="ORZ.props" />
</ImportGroup> </ImportGroup>
<ImportGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="PropertySheets"> <ImportGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="PropertySheets">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
<Import Project="ORZ.props" />
</ImportGroup> </ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
<Import Project="ORZ.props" />
</ImportGroup> </ImportGroup>
<ImportGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="PropertySheets"> <ImportGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="PropertySheets">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
<Import Project="ORZ.props" />
</ImportGroup> </ImportGroup>
<PropertyGroup Label="UserMacros" /> <PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
......
...@@ -29,10 +29,10 @@ ...@@ -29,10 +29,10 @@
<ClInclude Include="LK_seeta_verify.h"> <ClInclude Include="LK_seeta_verify.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="des.h"> <ClInclude Include="CZ_LK_CHECKIT.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="CZ_LK_CHECKIT.h"> <ClInclude Include="des.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
</ItemGroup> </ItemGroup>
......
...@@ -5,10 +5,14 @@ ...@@ -5,10 +5,14 @@
int main() int main()
{ {
LK_SEETA_VERIFY verify;
verify.write_chip_id_limit_num_to_nvm();
while (true) while (true)
{ {
//CZ_LK_CHECKIT(); CZ_LK_CHECKIT();
CZ_LK_CHECKIT_RANDOM();
//CZ_LK_CHECKIT_time_to_time();
} }
while (1); while (1);
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!