前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >测试Rockey 4 Smart加密锁的C语言代码

测试Rockey 4 Smart加密锁的C语言代码

作者头像
用户3519280
发布2023-07-08 14:49:55
2860
发布2023-07-08 14:49:55
举报
文章被收录于专栏:c++ 学习分享
代码语言:javascript
复制
// win32Console_dog_test.cpp : Defines the entry point for the console application.
///
//
//测试Rockey 4 Smart加密锁的C语言代码
//
///
 
#include "stdafx.h"
#include <conio.h>
#include "time.h"
#include "Ry4S.h"
//--------------------------------------------------------
//define function style
void ShowERR(WORD retcode);
void HexBufferToString(char* str,BYTE* buf,int len);
char* word2cp(WORD value);                   //WORD转字符串函数
char* UnionStr(const char* A,const char* B); //合并字符串函数
char* strcat_lc(const char* A,const char* B);
char* a2u(const char* A,const char* B);
char* a3u(const char* A,const char* B,const char* C);
char* GetERR_Cn(WORD retcode);
//--------------------------------------------------------
//主程序入口函数
int main(int argc, char* argv[])
{
	printf("Hello World!\n");
	//定义变量
	bool is_2=false; //一级或二级密码开关
	is_2=true;
	//
    WORD handle, p1, p2, p3, p4, retcode;
	DWORD lp1, lp2;
	BYTE buffer[1024];//1024
	//设置密码  (试用版公开默认密码)
	//设置一级密码p1,p2
	p1 = 0xc44c;	// Rockey4Smart Demo Password1
	p2 = 0xc8f8;	// Rockey4Smart Demo Password2
	//设置二级密码p3,p4
	p3 = 0;			// Program needn't Password3, Set to 0
	p4 = 0;			// Program needn't Password4, Set to 0
	/*
	//二级试用版公开默认密码*/
	if(is_2==true)
	{
		p3 = 0x0799;
		p4 = 0xc43b;
	}
	//初始化
	memset(buffer,0,1024);
	//查找加密锁方法
	//----------------------------
	printf("查找锁中...");
    retcode = Rockey(RY_FIND, &handle, &lp1, &lp2, &p1, &p2, &p3, &p4, buffer);
	if (retcode)
	{
		printf("查找锁出错: ");
		ShowERR(retcode);
		return -1;
	}
	char* hid=new char[sizeof(lp1)];
	sprintf(hid,"%08X",lp1);
	printf("找到一个加密锁,hid=%s ",hid);
	//printf("Find Rock: %08X\n", lp1);
	printf("查找锁完成!\n");
    //----------------------------
	//找到加密锁并打开	
	printf("打开锁中...");
	retcode = Rockey(RY_OPEN, &handle, &lp1, &lp2, &p1, &p2, &p3, &p4, buffer);
	if (retcode)	// Error	
	{	
		printf("打开锁出错,Error Code: %d\n", retcode);		
		return -1;	
	}
	printf("打开锁完成!\n");
	//----------------------------
	//[用户存储区]写入数据
	printf("[用户存储区]写入数据中...");
	//0~499    低地址区(一,二级密码均可读写)
	//500~2000 高地址区(一级只读,二级可读写)
	if(is_2==false)
	{
		p1=0;     //pos
		p2=500;   //length 500
	}
	else
	{
        p1=0;     //pos
		p2=900;   //length 500 / 900 (=1024?)
	}
	strcpy((char*)buffer,"Hello world! Rockey4 Smart!");
	//for(int i=p1;i<p2;i++){buffer[i]=i;}
	retcode=Rockey(RY_WRITE,&handle,&lp1,&lp2,&p1,&p2,&p3,&p4,buffer);
	if(retcode)
	{
        printf("写入数据出错\n ErrorCode:%d\n,ErrorInfo:%s\n",retcode,GetERR_Cn(retcode));
		return -1;
	}
	printf("Write data:%s\n",buffer);
	printf("[用户存储区]写入数据完成!\n");
    //----------------------------
	//[用户存储区]读取数据
	printf("[用户存储区]读取数据中...");
    p1 = 0;	    // Pos	
	p2 = 500;	// Length	
	buffer[14] = 0;
	retcode = Rockey(RY_READ, &handle, &lp1, &lp2, &p1, &p2, &p3, &p4, buffer);	
	if (retcode)	// Error
	{	
		printf("读取用户存储区出错\n Error Code: %d\n", retcode);
	    return -1;
	}        
	// ==============================================================	
	printf("buffer:%s\n", buffer);
	printf("[用户存储区]读取数据完成!\n");
	//----------------------------
	//产生随机数 OK
	for(int i=0;i<10;i++)
	{
		retcode = Rockey(RY_RANDOM, &handle, &lp1, &lp2, &p1, &p2, &p3, &p4, buffer);
		if (retcode)	// Error
		{	printf("产生随机数出错了\n,Error Code: %d\n", retcode);
			return -1;
		}        
		printf("生成的随机数为Random 16Hex:%04X,10Dec:%d\n", p1,p1);
		//sprintf(buffer, "%04X", p1);
	}
	//----------------------------
    //产生种子码  OK
	lp2=0x12345678;
	retcode=Rockey(RY_SEED,&handle,&lp1,&lp2,&p1,&p2,&p3,&p4,buffer);
	if(retcode) //out error
	{
        printf("产生种子码出错\n,Error code:%d\n,Error info:%s\n",retcode,GetERR_Cn(retcode));
		return -1;
	}
	printf("种子码为种码为%X\n",lp2);
	printf("子码为Seed:%04X %04X %04X %04X\n",p1,p2,p3,p4);
	if (p1==0xD03A && p2==0x94D6 && p3==0x96A9 && p4==0x7F54)
	{   //种子码正确 种为0x12345678,子码为0xD03A  0x94D6 0x96A9	0x7F54	
		printf("种子码正确,Hello World!\n");
	}
	else
	{
		printf("种子码出错了,Hello error!\n");
		return -1;
	}	
	//----------------------------
	//写入用户ID  RY_WRITE_USERID
	if(is_2==true)
	{
		lp1 = 0x88888888;
		retcode = Rockey(RY_WRITE_USERID, &handle, &lp1, &lp2, &p1, &p2, &p3, &p4, buffer);
		if (retcode) // Error
		{	
			printf("写入用户ID,Error Code: %d\n", retcode);	
			return -1;
		}	
		printf("Write User ID: %08X\n", lp1);
	}
	//----------------------------
	//读取用户ID  RY_READ_USERID
	lp1 = 0;
	retcode = Rockey(RY_READ_USERID, &handle, &lp1, &lp2, &p1, &p2, &p3, &p4, buffer);
	if (retcode) // Error
	{		
		printf("Error Code: %d\n", retcode);	
		return -1;	
	}
	printf("读取用户ID UID=%X\n",lp1);
	if (lp1==0x88888888)
	{
		printf("Hello world! UID=%X\n",lp1);
	}
	else      
	{
		printf("Hello DEMO!\n");
		return -1;
	}
	//----------------------------
	//设置模块字RY_SET_MODULE
	if(is_2==true)
	{
		p1 = 0;	
		p2 = 3;
		p3 = 0;
		retcode = Rockey(RY_SET_MODULE, &handle, &lp1, &lp2, &p1, &p2, &p3, &p4, buffer);
		if (retcode) // Error
		{		
			printf("Error Code: %d\n", retcode);	
			return -1;	
		}
		printf("Set Moudle 0: Pass = %04X Decrease no allow\n", p2);
		//Set Moudle 0: Pass = 0x0003 Decrease no allow 不允许减少
	}
	//----------------------------
	//检查模块状态 RY_CHECK_MODULE
	p1 = 0;	
	retcode = Rockey(RY_CHECK_MODULE, &handle, &lp1, &lp2, &p1, &p2, &p3, &p4, buffer);
	if (retcode) // Error
	{		
		printf("检查模块状态,Error Code: %d\n", retcode);	
		return -1;	
	}
    printf("check Moudle 0: Pass = %04X Decrease no allow\n", p2);
	printf("检查模块状态,lp1=%04X,lp2=%04X,p1=%04X,p2=%04X,p3=%04X,p4=%04X\n", lp1,lp2,p1,p2,p3,p4);
	//pass =0x0001
	if (p2) printf("检查模块状态,Hello world!\n");		
	else return -1;		 
	if (p2!=1)
	{    
		printf("Update Please!\n");
		return -1;
	} 
	if(is_2==true)//if(p3==1)	
	{     
		/*
		p1=0;    //递减模块单元 RY_DECREASE
		retcode = Rockey(RY_DECREASE, &handle, &lp1, &lp2, &p1, &p2, &p3, &p4, buffer);   
		if(retcode)         
		{         
		     printf("递减模块单元,Error Code: %d,Error info:%s\n", retcode,GetERR_Cn(retcode));
		     return -1;	
		}
		printf("递减模块单元,lp1=%04X,lp2=%04X,p1=%04X,p2=%04X,p3=%04X,p4=%04X\n", lp1,lp2,p1,p2,p3,p4);
	    */
	} 
	//----------------------------
	//加密解密一个例子
	char str[20]="bfgis_system";
	DWORD mykey=12345678;
	int slen = strlen(str);
	int n;
    // Encrypt my data  加密我的数据
	lp2 = mykey;  //产生种子码
	retcode = Rockey(RY_SEED, &handle, &lp1, &lp2, &p1, &p2, &p3, &p4, buffer);
	if (retcode)	// Error
	{
		printf("Error Code: %d\n", retcode);
		return -1;
	}
	for (n=0;n<slen;n++)
	{
		str[n] = str[n] + (char)p1 + (char)p2 + (char)p3 + (char)p4;
	}
	printf("加密我的数据,Encrypted data is %s\n", str);
 
	// Decrypt my data 解密我的数据
	lp2 = mykey;  //产生种子码
	retcode = Rockey(RY_SEED, &handle, &lp1, &lp2, &p1, &p2, &p3, &p4, buffer);
	if (retcode)	// Error
	{
		printf("Error Code: %d\n", retcode);
		return -1;
	}
 
	for (n=0;n<slen;n++)
	{
		str[n] = str[n] - (char)p1 - (char)p2 - (char)p3 - (char)p4;
	}
	printf("解密我的数据,Decrypted data is %s\n", str);
	//----------------------------
	//日期操作
	char *buf=new char[1024];
	SYSTEMTIME st;
	GetLocalTime(&st);
	printf("Date:%04d%02d%02d\n",st.wYear,st.wMonth,st.wDay);  //获取年月日	
	sprintf(buf,"%04d%02d%02d",st.wYear,st.wMonth,st.wDay);
	//
	lp1 = 0x20121101;  //写入UID
	retcode = Rockey(RY_WRITE_USERID, &handle, &lp1, &lp2, &p1, &p2, &p3, &p4, buffer);
	if (retcode)
	{
		ShowERR(retcode);
		return -1;
	}
	printf("Write User ID: %08X\n", lp1);
 
	lp1 = 0;          //读取UID
	retcode = Rockey(RY_READ_USERID, &handle, &lp1, &lp2, &p1, &p2, &p3, &p4, buffer);
	if (retcode)
	{
		ShowERR(retcode);
		return -1;
	}
	printf("Read User ID: %08X\n", lp1);
	//   	
	//if(strcmp(t_buf,t_buffer)>=0) 
	if(1)
	{
	   printf("日期操作相等ok!\n");
	}
	else
	{
       printf("期操作不等 exit!\n");
	}
	
	//----------------------------
	//关闭加密锁  OK
	printf("关闭锁中...");
	retcode = Rockey(RY_CLOSE, &handle, &lp1, &lp2, &p1, &p2, &p3, &p4, buffer);
	if (retcode)
	{
		printf("关闭锁出错,Error Code: %d\n", retcode);
		return -1;
	}
	printf("关闭锁完成!\n");
    //----------------------------
	printf("暂停");
	int temp=getchar();
	return 0;
}
 
//显示错误信息函数
void ShowERR(WORD retcode)
{
	if (retcode == 0) return;
	printf("Error Code: %d,Error Info:%s\n", retcode,GetERR_Cn(retcode));
}
//16进制转字符串函数
void HexBufferToString(char* str,BYTE* buf,int len)
{
	int k;
	sprintf(str,"0x");
	for( k=0;k<len;k++)
	{
		char strTemp[20];
		sprintf(strTemp,"%02x",buf[k]);
		strcat(str,strTemp);
	}
}
 
//WORD转字符串函数 
char* word2cp(WORD value)
{
    char* s=new char[sizeof(value)];
	sprintf(s,"%d",value); 
    char* ret_s=new char[strlen(s)];
	strcpy(ret_s,s);	
	return ret_s;
}
//实现 合并字符串函数
char* UnionStr(const char* A,const char* B)
{
    int len=0;	
	len=strlen(A)+strlen(B)+1;
	char* result=new char[len];
	memset(result,0,len);	
	sprintf(result, "%s%s",A,B);	
	char* r=new char[strlen(result)+1];
	strcpy(r,result);
	//	
	return r;
}
//合并字符串函数
char* strcat_lc(const char* A,const char* B)
{
    return UnionStr(A,B);
}
//合并2参数
char* a2u(const char* A,const char* B)
{
    return strcat_lc(A,B);
}
//合并3参数
char* a3u(const char* A,const char* B,const char* C)
{
	char* t=strcat_lc(A,B);
	t=strcat_lc(t,C);
    return t;
}
//获取错误信息的中文信息函数
char* GetERR_Cn(WORD retcode)
{
    char* r=NULL;
	char* rc=word2cp(retcode);
     // 错误码
	switch(retcode)
	{
		case ERR_SUCCESS:		          //0  
			r="没有错误";
			break;
		case ERR_NO_ROCKEY:               //3
			r="没有ROCKEY";
			break;
		case ERR_INVALID_PASSWORD:        //4  
			r="有ROCKEY锁,但基本密码错";
			break;
		case ERR_INVALID_PASSWORD_OR_ID:  //5  
			r="错误的密码或硬件 ID";
			break;
		case ERR_SETID:                   //6
			r="设置硬件 ID 错";
			break;
		case ERR_INVALID_ADDR_OR_SIZE:    //7
			r="读写地址或长度有误";
			break;
		case ERR_UNKNOWN_COMMAND:         //8
			r="没有此命令";
			break;
		case ERR_NOTBELEVEL3:                 //9  
			r="内部错误";
			break;
		case ERR_READ:                        //10 
			r="读数据错";
			break;
		case ERR_WRITE:                       //11 
			r="写数据错";
			break;
		case ERR_RANDOM:                      //12 
			r="随机数错";
			break;
		case ERR_SEED:                        //13 
			r="种子码错";
			break;
		case ERR_CALCULATE:                   //14 
			r="计算错";
			break;
		case ERR_NO_OPEN:                     //15 
			r="在操作前没有打开锁";
			break;
		case ERR_OPEN_OVERFLOW:               //16 
			r="打开的锁太多(>16)";
			break;
		case ERR_NOMORE:                      //17 
			r="找不到更多的锁";
			break;
		case ERR_NEED_FIND:                   //18 
			r="没有 Find 直接用了 FindNext";
			break;
		case ERR_DECREASE:                    //19 
			r="递减错";
			break;
		case ERR_AR_BADCOMMAND:               //20 
			r="算法指令错";
			break;
		case ERR_AR_UNKNOWN_OPCODE:           //21 
			r="算法运算符错";
			break;
		case ERR_AR_WRONGBEGIN:               //22 
			r="算法第一条指令含有常数";
			break;			
		case ERR_AR_WRONG_END:                //23 
			r="算法最后一条指令含有常数";
			break;
		case ERR_AR_VALUEOVERFLOW:            //24 
			r="算法中常数值 > 63";
			break;
		case ERR_TOOMUCHTHREAD:               //25 
			r="同一个进程中打开锁的线程数 > 100";
			break;
		case ERR_INVALID_RY4S:                //30 
			r="试图对非Rockey4Smart的锁进行操作";
			break;
		case ERR_INVALID_PARAMETER:           //31 
			r="不合法的参数";
			break;
		case ERR_INVALID_TIMEVALUE:           //32 
			r="不合法的时间值";
			break;
		default:
			r=rc;
			break;	
	}
	return r;
}
//
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2023-06-24,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
对象存储
对象存储(Cloud Object Storage,COS)是由腾讯云推出的无目录层次结构、无数据格式限制,可容纳海量数据且支持 HTTP/HTTPS 协议访问的分布式存储服务。腾讯云 COS 的存储桶空间无容量上限,无需分区管理,适用于 CDN 数据分发、数据万象处理或大数据计算与分析的数据湖等多种场景。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档