Commit 685958c1 by kevin

first commit for pcsc api

0 parents
/*
LKT4302A
*/
/**
* @file DEF_Macro.h
*
* $Revision$
* $Date$
* $Author$
*/
/*** Header define ************************************************************/
#ifndef __DEF_TYPE_H__
#define __DEF_TYPE_H__
/*** Macro definitions ********************************************************/
#define SIZE_BYTE 1
#define SIZE_HALFWORD 2
#define SIZE_WORD 4
/*** Type definitions *********************************************************/
typedef unsigned char u8;
typedef signed char s8;
typedef unsigned short u16;
typedef signed short s16;
typedef unsigned long u32;
typedef signed long s32;
typedef unsigned char BYTE;
typedef signed short SHORT;
typedef unsigned short USHORT;
typedef unsigned short WORD;
typedef signed long LONG;
typedef unsigned long ULONG;
typedef unsigned long DWORD;
#endif ///< __DEF_TYPE_H__
#include "des.h"
#include <string.h>
unsigned char outbuf[255],outbuf1[255];
unsigned char outbuf2[255];
unsigned char S[4][4][16] =//S1
{{{14,4,13,1,2,15,11,8,3,10,6,12,5,9,0,7},
{0,15,7,4,14,2,13,1,10,6,12,11,9,5,3,8},
{4,1,14,8,13,6,2,11,15,12,9,7,3,10,5,0},
{15,12,8,2,4,9,1,7,5,11,3,14,10,0,6,13}},
//S2
{{15,1,8,14,6,11,3,4,9,7,2,13,12,0,5,10},
{3,13,4,7,15,2,8,14,12,0,1,10,6,9,11,5},
{0,14,7,11,10,4,13,1,5,8,12,6,9,3,2,15},
{13,8,10,1,3,15,4,2,11,6,7,12,0,5,14,9}},
//S3
{{10,0,9,14,6,3,15,5,1,13,12,7,11,4,2,8},
{13,7,0,9,3,4,6,10,2,8,5,14,12,11,15,1},
{13,6,4,9,8,15,3,0,11,1,2,12,5,10,14,7},
{1,10,13,0,6,9,8,7,4,15,14,3,11,5,2,12}},
//S4
{{7,13,14,3,0,6,9,10,1,2,8,5,11,12,4,15},
{13,8,11,5,6,15,0,3,4,7,2,12,1,10,14,9},
{10,6,9,0,12,11,7,13,15,1,3,14,5,2,8,4},
{3,15,0,6,10,1,13,8,9,4,5,11,12,7,2,14}}} ;
//S5
uint8_t S1[4][4][16]=
{{{2,12,4,1,7,10,11,6,8,5,3,15,13,0,14,9},
{14,11,2,12,4,7,13,1,5,0,15,10,3,9,8,6},
{4,2,1,11,10,13,7,8,15,9,12,5,6,3,0,14},
{11,8,12,7,1,14,2,13,6,15,0,9,10,4,5,3}},
//S6
{{12,1,10,15,9,2,6,8,0,13,3,4,14,7,5,11},
{10,15,4,2,7,12,9,5,6,1,13,14,0,11,3,8},
{9,14,15,5,2,8,12,3,7,0,4,10,1,13,11,6},
{4,3,2,12,9,5,15,10,11,14,1,7,6,0,8,13}},
//S7
{{4,11,2,14,15,0,8,13,3,12,9,7,5,10,6,1},
{13,0,11,7,4,9,1,10,14,3,5,12,2,15,8,6},
{1,4,11,13,12,3,7,14,10,15,6,8,0,5,9,2},
{6,11,13,8,1,4,10,7,9,5,0,15,14,2,3,12}},
//S8
{{13,2,8,4,6,15,11,1,10,9,3,14,5,0,12,7},
{1,15,13,8,10,3,7,4,12,5,6,11,0,14,9,2},
{7,11,4,1,9,12,14,2,0,6,10,13,15,3,5,8},
{2,1,14,7,4,10,8,13,15,12,9,0,3,5,6,11}}};
/*void *memcpy(void *dest, const void *src, size_t n)
{
void *ret=dest;
while(n--){
*(char *)dest = *(char *)src;
dest = (char *)dest + 1;
src = (char *)src + 1;
}
return(ret);
} */
//将长度为8的字符串转为二进制位串
uint8_t Char8ToBit64(uint8_t ch[8],uint8_t *bit_a){
uint8_t cnt,cnt1;
for(cnt = 0; cnt < 8; cnt++){
for(cnt1 = 0;cnt1 < 8; cnt1++){
if((ch[cnt]& 0x80) == 0x80)
*bit_a = 0x01;
else
*bit_a = 0x00;
bit_a++;
ch[cnt]=ch[cnt]<<1;
}
}
return 1;
}
//将二进制位串转为长度为8的字符串
void Bit64ToChar8(uint8_t bit_a[64],uint8_t ch[8]){
char cnt,cnt1;
memset(ch,0,8);
for(cnt = 0; cnt < 8; cnt++){
for(cnt1 = 0;cnt1 < 8; cnt1++){
ch[cnt]=ch[cnt]<<1;
ch[cnt]|= *bit_a;
bit_a++ ;
}
}
}
//密钥置换1
void DES_PC1_Transform(uint8_t key[64],uint8_t tempbts[56]){
uint8_t cnt;
uint8_t PC_1[56] = {
56,48,40,32,24,16,8,
0,57,49,41,33,25,17,
9,1,58,50,42,34,26,
18,10,2,59,51,43,35,
62,54,46,38,30,22,14,
6,61,53,45,37,29,21,
13,5,60,52,44,36,28,
20,12,4,27,19,11,3 };
for(cnt = 0; cnt < 56; cnt++){
tempbts[cnt] = key[PC_1[cnt]];
}
}
//密钥置换2
void DES_PC2_Transform(uint8_t key[56], uint8_t tempbts[48]){
uint8_t cnt;
uint8_t PC_2[48] = {
13,16,10,23,0,4,2,27,
14,5,20,9,22,18,11,3,
25,7,15,6,26,19,12,1,
40,51,30,36,46,54,29,39,
50,44,32,47,43,48,38,55,
33,52,45,41,49,35,28,31};
for(cnt = 0; cnt < 48; cnt++){
tempbts[cnt] = key[PC_2[cnt]];
}
}
//循环左移
void DES_ROL(uint8_t *data_a,uint8_t time){
uint8_t temp[56],i;
//保存将要循环移动到左边的位
for(i=0;i<time;i++)
{
temp[i] = *(data_a+i);
temp[time+i] = *(data_a+28+i);
}
for(i=0;i<=28-time;i++)
{
*(data_a+i) = *(data_a+time+i) ;
*(data_a+28+i) = *(data_a+28+time+i);
}
for(i=0;i<time;i++)
{
*(data_a+28-time+i) = temp[i];
*(data_a+56-time+i) = temp[i+time];
}
}
//循环右移
void DES_ROR(uint8_t *data_a,uint8_t time){
uint8_t temp[56],i;
//保存将要循环移动到右边的位
for(i=0;i<time;i++)
{
temp[time-1-i] = *(data_a+28-1-i);
temp[time+1-i] = *(data_a+56-1-i);
}
for(i=0;i<=28-time;i++)
{
*(data_a+27-i) = *(data_a+27-time-i) ;
*(data_a+55-i) = *(data_a+55-time-i);
}
for(i=0;i<time;i++)
{
*(data_a+i) = temp[i];
*(data_a+28+i) = temp[i+2];
}
}
//IP置换
void DES_IP_Transform(uint8_t data_a[64]){
uint8_t cnt;
uint8_t temp[64];
uint8_t IP_Table[64] = {
57,49,41,33,25,17,9,1,
59,51,43,35,27,19,11,3,
61,53,45,37,29,21,13,5,
63,55,47,39,31,23,15,7,
56,48,40,32,24,16,8,0,
58,50,42,34,26,18,10,2,
60,52,44,36,28,20,12,4,
62,54,46,38,30,22,14,6 };
for(cnt = 0; cnt < 64; cnt++){
temp[cnt] = data_a[IP_Table[cnt]];
}
memcpy(data_a,temp,64);
}
//IP逆置换
void DES_IP_1_Transform(uint8_t data_a[64]){
uint8_t cnt;
uint8_t temp[64];
uint8_t IP_1_Table[64] = {
39,7,47,15,55,23,63,31,
38,6,46,14,54,22,62,30,
37,5,45,13,53,21,61,29,
36,4,44,12,52,20,60,28,
35,3,43,11,51,19,59,27,
34,2,42,10,50,18,58,26,
33,1,41,9,49,17,57,25,
32,0,40,8,48,16,56,24 };
for(cnt = 0; cnt < 64; cnt++){
temp[cnt] = data_a[IP_1_Table[cnt]];
}
memcpy(data_a,temp,64);
}
//扩展置换
void DES_E_Transform(uint8_t data_a[48]){
uint8_t cnt;
uint8_t temp[48];
uint8_t E_Table[48] = {
31,0,1,2,3,4,
3,4,5,6,7,8,
7,8,9,10,11,12,
11,12,13,14,15,16,
15,16,17,18,19,20,
19,20,21,22,23,24,
23,24,25,26,27,28,
27,28,29,30,31,0};
for(cnt = 0; cnt < 48; cnt++){
temp[cnt] = data_a[E_Table[cnt]];
}
memcpy(data_a,temp,48);
}
//P置换
void DES_P_Transform(uint8_t data_a[32]){
uint8_t cnt;
uint8_t temp[32];
uint8_t P_Table[32] = {
15,6,19,20,28,11,27,16,
0,14,22,25,4,17,30,9,
1,7,23,13,31,26,2,8,
18,12,29,5,21,10,3,24};
for(cnt = 0; cnt < 32; cnt++){
temp[cnt] = data_a[P_Table[cnt]];
}
memcpy(data_a,temp,32);
}
//异或
void DES_XOR(uint8_t R[48], uint8_t L[48] ,uint8_t count){
uint8_t cnt;
for(cnt = 0; cnt < count; cnt++){
R[cnt] ^= L[cnt];
}
}
//S盒置换
void DES_SBOX(uint8_t data_a[48]){
uint8_t cnt;
uint8_t lines,row;
uint8_t cur1,cur2;
uint8_t output ;
//逆初始置换表IP^-1
for(cnt = 0; cnt < 8; cnt++){
cur1 = cnt*6;
cur2 = cnt<<2;
//计算在S盒中的行与列
lines = (data_a[cur1]<<1) + data_a[cur1+5];
row = (data_a[cur1+1]<<3) + (data_a[cur1+2]<<2)
+ (data_a[cur1+3]<<1) + data_a[cur1+4];
if(cnt<4)
output = S[cnt][lines][row];
else
{
output = S1[cnt-4][lines][row];
}
//化为2进制
data_a[cur2] = (output&0X08)>>3;
data_a[cur2+1] = (output&0X04)>>2;
data_a[cur2+2] = (output&0X02)>>1;
data_a[cur2+3] = output&0x01;
}
}
//交换
void DES_Swap(uint8_t left[32], uint8_t right[32]){
uint8_t temp[32];
memcpy(temp,left,32);
memcpy(left,right,32);
memcpy(right,temp,32);
}
//加密单个分组
void encrypt_des(uint8_t *inoutdata ,uint8_t *keyStr){
uint8_t plainBits[64];
uint8_t copyRight[48];
uint8_t keyBlock[8];
uint8_t bKey[64];
uint8_t subKeys[48];
uint8_t temp[56];
uint8_t cnt;
uint8_t MOVE_TIMES[16] = {1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1};
memcpy(keyBlock,keyStr,8);
//将密钥转换为二进制流
Char8ToBit64(keyBlock,bKey);
DES_PC1_Transform(bKey,temp);//PC1置换
Char8ToBit64(inoutdata,plainBits);
//初始置换(IP置换)
DES_IP_Transform(plainBits);
for(cnt = 0; cnt < 16; cnt++){
DES_ROL(temp,MOVE_TIMES[cnt]); //左移
DES_PC2_Transform(temp,subKeys); //生成子密钥
// DES_MakeSubKeys(subKeys,cnt);
memcpy(copyRight,plainBits+32,32);
//将右半部分进行扩展置换,从32位扩展到48位
DES_E_Transform(copyRight);
//将右半部分与子密钥进行异或操作
DES_XOR(copyRight,subKeys,48);
//异或结果进入S盒,输出32位结果
DES_SBOX(copyRight);
//P置换
DES_P_Transform(copyRight);
//将明文左半部分与右半部分进行异或
DES_XOR(plainBits,copyRight,32);
if(cnt != 15){
//最终完成左右部的交换
DES_Swap(plainBits,plainBits+32);
}
}
//逆初始置换(IP^1置换)
DES_IP_1_Transform(plainBits);
Bit64ToChar8(plainBits,inoutdata);
}
//解密单个分组
void decrypt_des(uint8_t *inoutdata,uint8_t *keyStr){
uint8_t cipherBits[64];
uint8_t copyRight[48];
uint8_t keyBlock[8];
uint8_t bKey[64];
uint8_t temp[56];
uint8_t subKeys[48];
uint8_t cnt;
uint8_t MOVE_TIMES[16] = {0,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1};
memcpy(keyBlock,keyStr,8);
//将密钥转换为二进制流
Char8ToBit64(keyBlock,bKey);
DES_PC1_Transform(bKey,temp); //PC1置换
Char8ToBit64(inoutdata,cipherBits);
//初始置换(IP置换)
DES_IP_Transform(cipherBits);
//16轮迭代
for(cnt = 0; cnt <16; cnt++){
if(cnt !=0)
{
DES_ROR(temp,MOVE_TIMES[cnt]); //右移
}
DES_PC2_Transform(temp,subKeys); //生成子密钥
memcpy(copyRight,cipherBits+32,32);
//将右半部分进行扩展置换,从32位扩展到48位
DES_E_Transform(copyRight);
//将右半部分与子密钥进行异或操作
DES_XOR(copyRight,subKeys,48);
//异或结果进入S盒,输出32位结果
DES_SBOX(copyRight);
//P置换
DES_P_Transform(copyRight);
//将明文左半部分与右半部分进行异或
DES_XOR(cipherBits,copyRight,32);
if(cnt != 15){
//最终完成左右部的交换
DES_Swap(cipherBits,cipherBits+32);
}
}
//逆初始置换(IP^1置换)
DES_IP_1_Transform(cipherBits);
Bit64ToChar8(cipherBits,inoutdata);
}
/*void CbcDes_encrypt(unsigned char *iv,unsigned char *MsgIn, unsigned char *Key)
{
unsigned char i , j,time , k=0 ,k1=0;
time = MsgIn[0] / 8;
for(i=0; i<time; i++)
{
if(i==0)
{
for(j=0;j<8;j++)
MsgIn[j+1]= MsgIn[j+1]^iv[j];
encrypt_des(MsgIn+1,Key);
for(j=0;j<8;j++)
outbuf2[j]= MsgIn[j+1];
}
else
{
for(j=0;j<8;j++)
MsgIn[j+1]= MsgIn[j+k+1]^outbuf2[j+k1];
encrypt_des(MsgIn+1,Key);
for(j=0;j<8;j++)
outbuf2[j+k]= MsgIn[j+1];
k1+=8;
}
k+=8;
}
memcpy(MsgIn+1,outbuf2,time*8);
}
void CbcDes_decrypt(unsigned char *iv,unsigned char *MsgIn, unsigned char *Key)
{
unsigned char i , j,time , k=0 ,k1=0;
time = MsgIn[0] / 8;
for(i=0; i<time; i++)
{
if(i==0)
{
memcpy(outbuf1,MsgIn+1,8);
decrypt_des(MsgIn+1,Key);
for(j=0;j<8;j++)
MsgIn[j+1]= MsgIn[j+1]^iv[j];
for(j=0;j<8;j++)
outbuf[j]= MsgIn[j+1];
}
else
{
memcpy(outbuf1+k,MsgIn+k+1,8);
decrypt_des(MsgIn+k+1,Key);
for(j=0;j<8;j++)
MsgIn[j+1]= MsgIn[j+k+1]^outbuf1[j+k1];
for(j=0;j<8;j++)
outbuf[j+k]= MsgIn[j+1];
k1+=8;
}
k+=8;
}
memcpy(MsgIn+1,outbuf,time*8);
} */
/**********CBC模式DES加密**********/
void CbcDes_encrypt(unsigned char *iv,unsigned char *MsgIn, unsigned char *Key)
{
unsigned char j;
for(j=0;j<8;j++)
MsgIn[j+1]= MsgIn[j+1]^iv[j];
encrypt_des(MsgIn+1,Key);
for(j=0;j<8;j++)
outbuf2[j]= MsgIn[j+1];
memcpy(MsgIn+1,outbuf2,8);
}
/**********CBC模式DES解密**********/
void CbcDes_decrypt(unsigned char *iv,unsigned char *MsgIn, unsigned char *Key)
{
unsigned char j;
memcpy(outbuf1,MsgIn+1,8);
decrypt_des(MsgIn+1,Key);
for(j=0;j<8;j++)
MsgIn[j+1]= MsgIn[j+1]^iv[j];
for(j=0;j<8;j++)
outbuf[j]= MsgIn[j+1];
memcpy(MsgIn+1,outbuf,8);
}
/**********CBC模式3DES加密**********
第一个参数*iv为初始向量长度为8字节
第二个参数*inoutdata表示明文和密文数据,注意数据前面要加上长度,加密后的数据也会存到*inoutdata里
第三个参数*keyStr为16字节加密密钥
***/
void Cbcencrypt_3des(unsigned char *iv,uint8_t *inoutdata,uint8_t *keyStr){
uint8_t uszkeytmp[8] , time;
uint8_t iv1[8];
unsigned char i , k=0 ,k1=0;
time = inoutdata[0] / 8;
for(i=0; i<time; i++)
{
if(i==0)
{
memcpy(uszkeytmp,keyStr,8);
CbcDes_encrypt(iv,inoutdata,uszkeytmp);
memcpy(uszkeytmp,keyStr+8,8);
CbcDes_decrypt(iv,inoutdata,uszkeytmp);
memcpy(uszkeytmp,keyStr,8);
CbcDes_encrypt(iv,inoutdata,uszkeytmp);
}
else
{
memcpy(iv1,inoutdata+1+k1,8);
memcpy(uszkeytmp,keyStr,8);
CbcDes_encrypt(iv1,inoutdata+k,uszkeytmp);
memcpy(uszkeytmp,keyStr+8,8);
CbcDes_decrypt(iv1,inoutdata+k,uszkeytmp);
memcpy(uszkeytmp,keyStr,8);
CbcDes_encrypt(iv1,inoutdata+k,uszkeytmp);
k1+=8;
}
k+=8;
}
return;
}
/**********CBC模式3DES解密***********
第一个参数*iv为初始向量长度为8字节
第二个参数*inoutdata表示密文和明文数据,注意数据前面要加上长度,解密后的数据也会存到*inoutdata里
第三个参数*keyStr为16字节加密密钥
***/
void Cbcdecrypt_3des(unsigned char *iv,uint8_t *inoutdata ,uint8_t *keyStr){
uint8_t uszkeytmp[8] , time;
uint8_t iv2[8];
uint8_t iv1[8];
unsigned char i , k=0 , k1=0;
time = inoutdata[0] / 8;
for(i=0; i<time; i++)
{
if(i==0)
{
memcpy(iv1,inoutdata+1,8);
memcpy(uszkeytmp,keyStr,8);
CbcDes_decrypt(iv,inoutdata,uszkeytmp);
memcpy(uszkeytmp,keyStr+8,8);
CbcDes_encrypt(iv,inoutdata,uszkeytmp);
memcpy(uszkeytmp,keyStr,8);
CbcDes_decrypt(iv,inoutdata,uszkeytmp);
}
else
{
memcpy(iv2,inoutdata+1+k,8);
memcpy(uszkeytmp,keyStr,8);
CbcDes_decrypt(iv1,inoutdata+k,uszkeytmp);
memcpy(uszkeytmp,keyStr+8,8);
CbcDes_encrypt(iv1,inoutdata+k,uszkeytmp);
memcpy(uszkeytmp,keyStr,8);
CbcDes_decrypt(iv1,inoutdata+k,uszkeytmp);
k1+=8;
memcpy(iv1,iv2,8);
}
k+=8;
}
return;
}
/**********3DES加密*************
第一个参数inoutdata表示明文和密文数据,注意数据前面要加上长度,加密后的数据也会存到*inoutdata里
第二个参数?keyStr为16字节加密密钥
***/
void encrypt_3des(uint8_t *inoutdata,uint8_t *keyStr){
/*uint8_t uszkeytmp[8];
//16字节左半部分DES解密
memcpy(uszkeytmp,keyStr,8);
encrypt_des(inoutdata,uszkeytmp);
//16字节右半部分des加密
memcpy(uszkeytmp,keyStr+8,8);
decrypt_des(inoutdata,uszkeytmp);
//16字节左半部分des解密
memcpy(uszkeytmp,keyStr,8);
encrypt_des(inoutdata,uszkeytmp);
return; */
uint8_t uszkeytmp[8];
uint8_t uszkeytmp1[8];
uint8_t i,j;
int k=1;
i=inoutdata[0];
memcpy(uszkeytmp,keyStr,8);
memcpy(uszkeytmp1,keyStr+8,8);
if((i%8)!=0)
{
inoutdata[i+1]=0x80;
for(j=0;j<(i/8)+1;j++)
{
//16字节左半部分DES加密
encrypt_des(inoutdata+k,uszkeytmp);
//16字节右半部分DES解密
decrypt_des(inoutdata+k,uszkeytmp1);
//16字节左半部分DES加密
encrypt_des(inoutdata+k,uszkeytmp);
k+=8;
}
}
else
{
for(j=0;j<i/8;j++)
{
//16字节左半部分DES加密
encrypt_des(inoutdata+k,uszkeytmp);
//16字节右半部分DES解密
decrypt_des(inoutdata+k,uszkeytmp1);
//16字节左半部分DES加密
encrypt_des(inoutdata+k,uszkeytmp);
k+=8;
}
}
return;
}
/**********3DES解密*******************
第一个参数inoutdata表示明文和密文数据,注意数据前面要加上长度,加密后的数据也会存到*inoutdata里
第二个参数keyStr为16字节解密密钥
***/
void decrypt_3des(uint8_t *inoutdata ,uint8_t *keyStr){
uint8_t uszkeytmp[8];
uint8_t uszkeytmp1[8];
uint8_t i,j;
int k=1;
memcpy(uszkeytmp,keyStr,8);
memcpy(uszkeytmp1,keyStr+8,8);
i=inoutdata[0];
if((i%8)!=0)
i=((i/8)+1)*8;
for(j=0;j<i/8;j++)
{
//16字节左半部分DES解密
decrypt_des(inoutdata+k,uszkeytmp);
//16字节右半部分des加密
encrypt_des(inoutdata+k,uszkeytmp1);
//16字节左半部分des解密
decrypt_des(inoutdata+k,uszkeytmp);
k+=8;
}
return;
}
#include "lk_seeta_verify.h"
#include <exception>
#include <string>
#include <iostream>
void LK_SEETA_VERIFY::lk_seeta_establish_context()
{
LONG result = SCardEstablishContext(dwScope,
NULL,
NULL,
&hContext);
if (SCARD_S_SUCCESS != result)
{
std::string msg = "scard establish context failed!";
std::cout<< msg.c_str() << std::endl;
throw msg.c_str();
}
DWORD cch = SCARD_AUTOALLOCATE;
result = SCardListReaders(hContext,
NULL,
(LPTSTR)&readers,
&cch);
if (SCARD_S_SUCCESS != result)
{
std::string msg = "scard list reader failed!";
std::cout << msg.c_str() << std::endl;
throw msg.c_str();
}
}
void LK_SEETA_VERIFY::lk_seeta_sdcard_connect()
{
LONG result = SCardConnect(hContext,
(LPTSTR)readers,
dwShareMode,
SCARD_PROTOCOL_T0|SCARD_PROTOCOL_T1,
&hCardHandle[index],
&dwProtocols[index]);
if (SCARD_S_SUCCESS != result)
{
char buf[100];
sprintf(buf, "Connect Failed,ErrorCode:%X\n", result);
std::string msg(buf);
std::cout << msg.c_str() << std::endl;
throw msg.c_str();
}
}
void LK_SEETA_VERIFY::lk_seeta_sdcard_reconnect()
{
LONG result = SCardReconnect(hCardHandle[index],
1,
3,
2,
&dwProtocols[index]);
if (SCARD_S_SUCCESS != result)
{
char buf[100];
sprintf(buf, "Reconnect Failed,ErrorCode:%X\n", result);
std::string msg(buf);
std::cout << msg.c_str() << std::endl;
throw msg.c_str();
}
DWORD dwState, cch, cByte;
result = SCardStatus(hCardHandle[index],
NULL,
&cch,
&dwState,
&dwProtocol,
bAttr,
&cByte);
if (SCARD_S_SUCCESS != result)
{
char buf[100];
sprintf(buf, "status:%08X\n", dwState);
std::string msg(buf);
std::cout << msg.c_str() << std::endl;
throw msg.c_str();
}
}
void command_convert(char* command, DWORD len, BYTE* inBuf)
{//convert to hex
len = len / 2;
for (int i = 0; i<1000; i++)
{
sscanf(command + 2 * i, "%02x", inBuf + i); //research the send data structure,x -hex
if (i >= (len - 1))
break;
}
}
void LK_SEETA_VERIFY::lk_seeta_sdcard_transmit(char* command, DWORD len, BYTE* outBuf, DWORD &dwLenr)
{
BYTE inBuf[1000];
DWORD inBufLength = len / 2;
command_convert(command, len, inBuf);
LONG result = SCardTransmit(hCardHandle[index],
(dwProtocols[index] == SCARD_PROTOCOL_T0 ? SCARD_PCI_T0 : SCARD_PCI_T1),
inBuf,
inBufLength,
NULL,
outBuf,
&dwLenr);
if (SCARD_S_SUCCESS != result)
{
char buf[100];
sprintf(buf, "The Command Execute error:%08X\n", result);
std::string msg(buf);
std::cout << msg.c_str() << std::endl;
throw msg.c_str();
}
}
void LK_SEETA_VERIFY::lk_seeta_scard_disconnect()
{
LONG result = SCardDisconnect(hCardHandle[index], 0);
if (SCARD_S_SUCCESS != result)
{
char buf[100];
sprintf(buf, "disconnect error:%08X\n", result);
std::string msg(buf);
std::cout << msg.c_str() << std::endl;
throw msg.c_str();
}
}
void LK_SEETA_VERIFY::lk_seeta_sdcard_release_context()
{
LONG result = SCardReleaseContext(hContext);
if (SCARD_S_SUCCESS != result)
{
char buf[100];
sprintf(buf, "release context error:%08X\n", result);
std::string msg(buf);
std::cout << msg.c_str() << std::endl;
throw msg.c_str();
}
}
LK_SEETA_VERIFY::LK_SEETA_VERIFY()
{//create context and connects
lk_seeta_establish_context();
lk_seeta_sdcard_connect();
}
LK_SEETA_VERIFY::~LK_SEETA_VERIFY()
{//disconnect and release context
lk_seeta_scard_disconnect();
lk_seeta_sdcard_release_context();
}
DWORD get_command_length(char* command)
{
DWORD len = 0;
char* temp = command;
while (*temp != '\0')
{
++len;
temp = temp + 1;
}
return len;
}
int compare_des_inside_outside(BYTE* des_outside, BYTE* des_inside, DWORD len)
{//check if inside equal to outsize
int result = 0;
for (int i = 0; i < len; ++i)
{
if (des_outside[i] != des_inside[i])
{
result = -1;
break;
}
}
return result;
}
void LK_SEETA_VERIFY::contrast_authenticate()
{
std::string prefix_str = "80080000";
std::string in_data_len = "09";
std::string commpand_type = "03";
std::string in_data = "1122334455667788";
char* command = const_cast<char*>(std::string("").append(prefix_str).append(in_data_len).append(commpand_type).append(in_data).c_str());//8008 0000 09 03 1122334455667788
DWORD len = get_command_length(command);
BYTE out_buf_info[10];
DWORD dwLenr;
lk_seeta_sdcard_transmit(command, len, out_buf_info, dwLenr);//get "61XX"
BYTE outBuf[1000];//8 random 8 des result
std::string get_content_str = std::string("00C00000").append(1, out_buf_info[2]).append(1, out_buf_info[3]);
char* command_get_content = const_cast<char*>(get_content_str.c_str());
len = get_command_length(command_get_content);
lk_seeta_sdcard_transmit(command_get_content, len, outBuf, dwLenr);//actual result
if (dwLenr % 2 != 0)
{//length must 2 times
char buf[100];
sprintf(buf, "outBuf length error\n");
std::string msg(buf);
std::cout << msg.c_str() << std::endl;
throw msg.c_str();
}
BYTE inoutdata[128];
BYTE keyStr[128];
memset(inoutdata, 0, 0x20);
memcpy(inoutdata, "\x08\x11\x22\x33\x44\x55\x66\x77\x88", 0x09); //注意第一个字节为加密明文长度
memcpy(keyStr, "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 0x10);
//xor with random inside
for (int i = 0; i < dwLenr / 2; ++i)
{
inoutdata[i] ^= outBuf[i];
}
//3DES加密
encrypt_3des(inoutdata, keyStr);
int shift_and_length = dwLenr / 2;
int result = compare_des_inside_outside(inoutdata + shift_and_length + 1, outBuf + shift_and_length, shift_and_length);
if (result < 0)
{
char buf[100];
sprintf(buf, "authorize failed!\n");
std::string msg(buf);
std::cout << msg.c_str() << std::endl;
throw msg.c_str();
}
}
#pragma once
#ifndef LK_SEETA_VERIFY_H
#define LK_SEETA_VERIFY_H
#ifdef __cplusplus
extern "C" {
#endif
#include "winscard.h"
#include "des.h"
class LK_SEETA_VERIFY
{
public:
LK_SEETA_VERIFY();
~LK_SEETA_VERIFY();
void contrast_authenticate();
private:
void lk_seeta_establish_context();//create context resource
void lk_seeta_sdcard_connect();//connect sdcard
void lk_seeta_sdcard_reconnect(); //active sdcard
void lk_seeta_sdcard_transmit(char* commond, DWORD len, BYTE* outBuf, DWORD &dwLenr);//transmit command and get result
void lk_seeta_scard_disconnect();//disconnect
void lk_seeta_sdcard_release_context();//release context resource
private:
DWORD dwScope = SCARD_SCOPE_USER;
SCARDCONTEXT hContext;
SCARDHANDLE hCardHandle[4];
DWORD dwShareMode = SCARD_SHARE_SHARED;
DWORD dwProtocols[4];//
LPTSTR readers = NULL;//
int index = 0;
BYTE bAttr[32];
DWORD dwProtocol;
private:
LK_SEETA_VERIFY& operator=(const LK_SEETA_VERIFY&) = delete;
};
#ifdef __cplusplus
}
#endif
#endif // !LK_SEETA_VERIFY_H
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{60BA1C11-DF1F-4EA5-A7BA-15712662B7BE}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>compare_verify</RootNamespace>
<WindowsTargetPlatformVersion>8.1</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<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" />
</ImportGroup>
<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" />
</ImportGroup>
<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" />
</ImportGroup>
<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" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<PrecompiledHeader>
</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>WIN32;_DEBUG;_WINDOWS;_USRDLL;COMPARE_VERIFY_EXPORTS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>winscard.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<PrecompiledHeader>
</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>winscard.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<PrecompiledHeader>
</PrecompiledHeader>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>WIN32;NDEBUG;_WINDOWS;_USRDLL;COMPARE_VERIFY_EXPORTS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<PrecompiledHeader>
</PrecompiledHeader>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="DES.c" />
<ClCompile Include="main.cpp" />
<ClCompile Include="LK_seeta_verify.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="DEF_Type.h" />
<ClInclude Include="des.h" />
<ClInclude Include="LK_seeta_verify.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Source Files">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Header Files">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;hm;inl;inc;xsd</Extensions>
</Filter>
<Filter Include="Resource Files">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="main.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="LK_seeta_verify.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="DES.c">
<Filter>Header Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="LK_seeta_verify.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="des.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="DEF_Type.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>
\ No newline at end of file
#ifndef __DES_h
#define __DES_h
#endif
#ifdef __DES_h
#include <stdint.h>
unsigned char Char8ToBit64(unsigned char *ch,unsigned char *bit_a);
void Bit64ToChar8(unsigned char bit_a[64],unsigned char ch[8]);
void DES_PC1_Transfrom(unsigned char key[64],unsigned char tempbts[56]);
void DES_PC2_Transfrom(unsigned char key[56],unsigned char tempbts[48]);
void DES_ROL(unsigned char *data_a,unsigned char time);
void DES_ROR(unsigned char *data_a,unsigned char time);
void DES_IP_Transform(unsigned char data_a[64]);
void DES_IP_1_Transform(unsigned char data_a[64]);
void DES_E_Transform(unsigned char data_a[48]);
void DES_P_Transform(unsigned char data_a[32]);
void DES_SBOX(unsigned char data_a[48]);
void DES_XOR(unsigned char R[48],unsigned char L[48],unsigned char count);
void DES_Swap(unsigned char left[32],unsigned char right[32]);
void encrypt_des(unsigned char *inoutdata,unsigned char *keyStr);
void decrypt_des(unsigned char *inoutdata, unsigned char *keyStr);
void CbcDes_encrypt(unsigned char *iv,unsigned char *MsgIn, unsigned char *Key);
void CbcDes_decrypt(unsigned char *iv,unsigned char *MsgIn, unsigned char *Key);
void encrypt_3des(uint8_t *inoutdata,uint8_t *keyStr);
void decrypt_3des(uint8_t *inoutdata ,uint8_t *keyStr);
/*CBC_DES加密函数*/
/* iv 初始向量8字节; inoutdata 输入时为明文数据,输出时为密文数据(注:第一字节为长度); keyStr 加密密钥 */
void Cbcencrypt_3des(unsigned char *iv,uint8_t *inoutdata,uint8_t *keyStr);
/*CBC_DES解密函数*/
void Cbcdecrypt_3des(unsigned char *iv,uint8_t *inoutdata ,uint8_t *keyStr);
/*iv 初始向量8字节; inoutdata 输入时为密文数据,输出时为明文数据(注:第一字节为长度); keyStr 加密密钥 */
#endif
#include "lk_seeta_verify.h"
#include <iostream>
#include <string>
int main()
{
LK_SEETA_VERIFY verify;
verify.contrast_authenticate();
while (1);
return 0;
}
\ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!