Hashlock协议C语言实现

一、原理

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

二、实现结果

1、验证成功

在这里插入图片描述

2、验证失败

不在数据库

在这里插入图片描述

无效密钥Key

在这里插入图片描述

三、源代码

// Hash-Lock.c
/********************************************************************************* 
  *Copyright(C),Fisher1016 
  *FileName:  Hash-Lock.c
  *Author:  xwang
  *Date:  2020.5.7 
  *Description: RFID electronic tag verification is realized through hash function
**********************************************************************************/  
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define Tag_N 3
#define Data_N 10
#define Hash_core 17    //hash
struct ElecTag
{
	int metalID;
	int ID;
};

struct DataBase
{
	int metalID;
	int Key;
	int ID;
};

ElecTag Tag[Tag_N];
DataBase Data[Data_N];
void Init() {
	int i;
	for (i = 0; i < Tag_N; i++) {
		Tag[i].metalID = rand();
		Tag[i].ID = rand();
	}
	for (i = 0; i < Data_N; i++) {
		Data[i].metalID = rand();
		Data[i].Key = rand();
		Data[i].ID = rand();
	}
}
// Administrator define Tag Values and modifies database
void AdminiWrite() {
	Tag[0].metalID = 14;  //it not exist in DataBase
	Tag[0].ID = 66666666;
	Tag[1].metalID = 15;
	Tag[1].ID = 66666666;
	Tag[2].metalID = 16;
	Tag[2].ID = 66666666;

	Data[0].metalID = 15;
	Data[0].Key = 32;
	Data[0].ID = 66666666;

	Data[1].metalID = 16;
	Data[1].Key = 32;  //true value is 33 ( 33 mod 17 =16 )
	Data[1].ID = 66666666;
}

//检测标签
int Query(ElecTag* Tag) {
	ElecTag* pTag = Tag;
	printf("\n**************************************************\n");
	printf("The Tag's metalID has been read in\n");
	return pTag->metalID;
}

//by Reader,return Data in Database to Reader
DataBase GetData(int RMetalID) {
	int i;
	for (i = 0; i < Data_N; i++) {
		if (RMetalID == Data[i].metalID) {
			printf("\n**************************************************\n");
			printf("Find this Tag in DataBase!\n");
			return Data[i];
		}
	}
	printf("\n**************************************************\n");
	printf("Error:Can not find this Tag in DataBase!\n");
	exit(-1);
}


int Hash(int Key) {
	return Key % Hash_core;
}
int GetTagID(DataBase * RevData, ElecTag * Tag) {
	DataBase* pData = RevData;
	ElecTag* pTag = Tag;
	int i;
	if (Hash(pData->Key) == pTag->metalID) {
		printf("\n**************************************************\n");
		printf("Electronic tag verification successful!\n");
		return pTag->ID;
	}
	printf("\n**************************************************\n");
	printf("Error:Electronic tag verification failed!\n");
	exit(-1);
}


void ReaderVer(int RevDataID, int TagID) {
	if (RevDataID == TagID) {
		printf("\n**************************************************\n");
		printf("Vertify Successfully!\n");
		printf("The ID is %d",TagID);
	}
	else {
		printf("\n**************************************************\n");
		printf("Error:ID does not match!\n");
	}
}
//Reader is core
int main() {
	Init();
	AdminiWrite();
	//you can choose "Test_Tag" equel to 0 or 2,but I set they are unabled to read
	int Test_Tag = 1;
	int RMetalID = Query(&Tag[Test_Tag]);
	DataBase RevData = GetData(RMetalID);
	int TagID = GetTagID(&RevData, &Tag[Test_Tag]);
	ReaderVer(RevData.ID, TagID);
}
  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值