⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pwdkey.cpp

📁 一个密码小键盘的封装的动态库
💻 CPP
字号:
// PwdKey.cpp : implementation file
//

#include "stdafx.h"
#include "PwdKey.h"
#include "des.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#define __JKP521__
//#define __NLK830__

/////////////////////////////////////////////////////////////////////////////
// CPwdKey

static usgc omk[32] = "88888888";
static usgc nmk[32] = "\x3C\x2E\x47\x8D\x1A\xB8\x73\x39";  // new main key


#ifdef __NLK830__

static usgc nuk[32] = "\x8D\x22\x53\xD6\x8C\x1E\xF3\x7A";  // new user key

static char act00[] = "\x1b\x41\x00\x00\x0d\x0a";			//激活用户0号密钥
//static char inmsg[] = "\x1b\x46\x30\x0d\x0a";				//打开小键盘,提示输入
static char inmsg[] = "\x1b\x46\x34\x0d\x0a";				//打开小键盘,提示输入
static char rstz[] = "\x1B\x43\x0D\x0A";					//关闭小键盘
static char clsctr[] = "\x1b\x52\x00\x00\x0d\x0a";			//清除用户0号密钥

#else
#ifdef __JKP521__
static usgc nuk[32] = "\x35\x39\x3A\x33\x36\x38\x37\x32";  // new user key
static char act00[] = "\x1b\x41\x00\x00\x0d\x0a";			//激活用户0号密钥
static char inmsg[] = "\x1b\x46\x30\x31\x0d\x0a";			//打开小键盘,提示输入
static char rstz[] = "\x1B\x5B\x2F\x33\x0D\x0A";			//关闭小键盘

static char clsctr[] = "\x1b\x52\x0d\x0a";					//清除用户0号密钥

#endif
#endif

char aschex1(char chr)
{
	if (chr>='0' && chr<='9')
		return chr-'0';
	if (chr>='A' && chr<='F')
		return chr-'A'+10;
    return 0;
}

//十六进制数->可见字符
char hexasc1(char hex)
{
	if (hex < 10)
		return hex+'0';
	if (hex < 16)
		return hex-10+'A';
	return ' ';
}

//十六进制->ASC码
char *hexasc2(int len, char *src, char *dst)
{
    int i;
    char ch1, ch2;
    for (i=0; i<len; i++)
    {
        ch1 = src[i];
        ch2 = ch1 & 0x0F;
        ch1 = (ch1 >> 4) & 0x0F;
        dst[i*2] = hexasc1(ch1);
        dst[i*2+1] = hexasc1(ch2);
    }
    return dst;
}

//ASC码->十六进制
char * aschex2(int len, char *src, char *dst)
{
    int i;
    char ch1, ch2;
    for (i=0; i<len; i++)
    {
        ch1 = src[i*2];
        ch2 = src[i*2+1];
        dst[i] = ((aschex1(ch1)<<4)&0xF0) | aschex1(ch2);
    }
    return dst;
}

CPwdKey::CPwdKey()
{
}

CPwdKey::~CPwdKey()
{
}


/////////////////////////////////////////////////////////////////////////////
// CPwdKey message handlers

int CPwdKey::Init(char *strComPort, int nBaudRate)
{
	char scom[12];
	pk_flg = 3;
	if (m_pCom.IsOpened())
		m_pCom.Close();
	strcpy(scom, strComPort);
	*(scom+3) = 0;
	if (stricmp(scom, "COM"))
		return 0;
	//	1200,N,8,1
	return m_pCom.Open(atoi(strComPort+3), 1200);
}

int CPwdKey::IgnoreRespone(int nCount, unsigned long nTmOut)
{
	int i = 0;
	unsigned long t0;
	char cmd[12] = "\0";
	t0 = GetTickCount();
	nTmOut = nTmOut/2;
	Sleep(nTmOut);
	while (i < nCount)
	{
		if (m_pCom.Read(cmd, 1) == 1)
		{
			i++;
			Sleep(10);
			continue;
		}
		if (GetTickCount()-t0 > nTmOut)
			break;
		Sleep(50);
	}
	return (usgc)*cmd;
}

void CPwdKey::expmk(usgc *mk, usgc *mkdst)
{
    int i, nbb=0x41;
    usgc c1, c2;
#ifdef __NLK830__
	nbb = 0x41;
#endif
#ifdef __JKP521__
	nbb = 0x30;
#endif
    for (i=0; i<8; i++) 
	{
        c1 = (mk[i]>>4)&0x0F;
        c2 = mk[i]&0x0F;
        mkdst[i*2] = c1+nbb;
        mkdst[i*2+1] = c2+nbb;
    }
    mkdst[16] = 0;
}



int CPwdKey::OpenKey(int nCommand)
{
	pk_flg = 3;
	if (!m_pCom.IsOpened())
		return 0;
    m_pCom.Write(act00, 6);
	IgnoreRespone(2, 100);
#ifdef __NLK830__
	m_pCom.Write(inmsg, 5);
#endif
#ifdef __JKP521__
	m_pCom.Write(inmsg, 6);
#endif
	IgnoreRespone(2, 200);
	pk_flg = 0;
	return 1;
}

int CPwdKey::CloseKey(int nCommand)
{
	pk_flg = 3;
	if (!m_pCom.IsOpened())
		return 0;
#ifdef __NLK830__
	m_pCom.Write(rstz, 4);
#endif
#ifdef __JKP521__
	m_pCom.Write(rstz, 6);
#endif
	IgnoreRespone(2, 200);
	return 1;
}

int CPwdKey::GetData(char *dst)
{
    int i, nbb=0x41;
	char ch, tmp[2];
	usgc ch1, ch2;
/*
	usgc kkkk[] = {0x39,0x3c,0x35,0x30,0x39,0x3f,0x35,0x3d,0x3d,0x3a,0x33,0x30,0x34,0x32,0x31,0x39};
	for (i=0; i<16; i++)
		pk_buf[i] = kkkk[i];
	nbb = 0x30;
	for (i=0; i<8; i++)
	{
		ch = (((usgc)pk_buf[i*2]-nbb)<<4)|((usgc)pk_buf[i*2+1]-nbb);
		dst[i] = ch;
	}
    DESFunc((usgc *)"00000000", (usgc *)dst, -1);
*/
#ifdef __NLK830__
	nbb = 0x41;
#endif
#ifdef __JKP521__
	nbb = 0x30;
#endif
	if (pk_flg != 5)
		*dst = 0;
	if (!m_pCom.IsOpened())
		return -1;
	while (1)
	{
		i = m_pCom.Read(tmp, 1);
		if (i < 1)
			return i;
		ch = *tmp;
		if (pk_flg == 0)
		{
			if (ch != 0x02)
				continue;
			pk_flg = 1;
			continue;
		}
#ifdef __NLK830__
		if (pk_flg == 1) 
		{
			pk_cnt = (int)ch-1;
			pk_cid = 0;
			pk_flg = 2;
			continue;
		}
		if (pk_flg != 2)
			return -1;
#endif
#ifdef __JKP521__
		if (pk_flg == 1)
		{
			ch1 = ch;
			pk_flg = 2;
			continue;
		}
		if (pk_flg == 2) 
		{
			ch2 = ch-nbb;
			ch1 = (ch1-nbb)<<4;
			pk_cnt = ch1|ch2;
			pk_cid = 0;
			pk_flg = 8;
			continue;
		}
		if (pk_flg != 8)
			return -1;
#endif
		if (pk_cid < 16) 
		{
			pk_buf[pk_cid] = ch;
		}
		if ((pk_cid++) < pk_cnt)
			continue;
		break;
	}
    pk_flg = 5;
#ifdef __NLK830__
    aschex2(8, (char *)pk_buf, (char *)dst);
#endif
#ifdef __JKP521__
	for (i=0; i<8; i++)
	{
		ch = (((usgc)pk_buf[i*2]-nbb)<<4)|((usgc)pk_buf[i*2+1]-nbb);
		dst[i] = ch;
	}
#endif
    DESFunc((usgc *)nuk, (usgc *)dst, -1);
    if (pk_cnt > 16)
       memcpy(dst, "gj;19p", 6);
    dst[6] = 0;	
	return 1;
}

int CPwdKey::IsReaded()
{
	if (pk_flg == 5)
		return 1;
	return 0;
}

int CPwdKey::ClearKey()
{
	if (!m_pCom.IsOpened())
		return 0;
#ifdef __NLK830__
	m_pCom.Write(clsctr, 6);
#endif
#ifdef __JKP521__
	m_pCom.Write(clsctr, 4);
#endif
	IgnoreRespone(2, 200);
	return 1;
}

int CPwdKey::FindKey(char *strComPort)
{
	int i, cnt=2;
	char strcomlst[][6] = {"COM1", "COM2"};
//	char kkk[66];
//	GetData(kkk);
//	return false;
	if (strComPort && *strComPort)
		cnt = 1;
	for (i=0; i<cnt; i++)
	{
		if (strComPort && *strComPort)
			strcpy(m_strComPort, strComPort);
		else
			strcpy(m_strComPort, strcomlst[i]);
		strupr(m_strComPort);
		if (!Init(m_strComPort, 1200))
			continue;
		if (m_pCom.Write(act00, 6) > 0)
		{
			if (IgnoreRespone(2, 300) == 0xAA)
				return true;
			Reset();
		}
		else
		{
			Reset();
		}
	}
	return false;
}


int CPwdKey::UpdateKey(char *strOldKey, char *strMainKey, char *strUserKey)
{
	int nlen = 16;
    char cmd[256];
	char tmp[48];
	if (!m_pCom.IsOpened())
		return 0;
#ifdef __NLK830__
	m_pCom.Write(clsctr, 6);
	IgnoreRespone(2, 200);
    memcpy(cmd, "\x1B\x55\x00\x00", 4);
	expmk(omk, (usgc *)tmp);
	if (strOldKey && *strOldKey)
		expmk((usgc *)strOldKey, (usgc *)tmp);
    memcpy(cmd+4, tmp, nlen);
	expmk(nuk, (usgc *)tmp);
	if (strUserKey && *strUserKey)
		expmk((usgc *)strUserKey, (usgc *)tmp);
    memcpy(cmd+4+nlen, tmp, nlen);
    memcpy(cmd+4+nlen+nlen, "\x0d\x0a", 2);
    m_pCom.Write(cmd, 4+nlen*2+2);
	IgnoreRespone(2, 200);
#endif
#ifdef __JKP521__
	m_pCom.Write(clsctr, 4);
	IgnoreRespone(2, 200);
/*	//先修改主密钥
	memcpy(cmd, "\x1B\x4D\x00", 3);
	memcpy(cmd+3, strOldKey, 8);
	memcpy(cmd+3+8, strMainKey, 8);
	memcpy(cmd+3+8+8, "\x0D\x0A", 2);
    m_pCom.Write(cmd, 3+8+8+2);
	IgnoreRespone(2, 200);
*/
	//修改用户密钥
	memcpy(cmd, "\x1B\x53\x00\x00", 4);
	memcpy((usgc *)tmp, nuk, 8);
	if (strUserKey && *strUserKey)
		memcpy((usgc *)tmp, (usgc *)strUserKey, 8);
	DESFunc((usgc *)"88888888", (usgc *)tmp, 1);
	memcpy(cmd+4, tmp, 8);
	memcpy(cmd+4+8, "\x0D\x0A", 2);
    m_pCom.Write(cmd, 4+8+2);
	IgnoreRespone(2, 400);
#endif
	memcpy(cmd, act00, 6);
	m_pCom.Write(cmd, 6);
	IgnoreRespone(2, 400);
	return 1;
}


int CPwdKey::Reset()
{
	pk_flg = 3;
	if (m_pCom.IsOpened())
		m_pCom.Close();
	return 0;
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -