给作业加个数字签名

为防止学生冒充笔迹修改作业分数,博主设计了一种基于Hash散列的数字签名方案。该方案通过将学生的姓名、签名日期及签名人的名字组合并进行散列运算,生成唯一的签名值,确保了作业评分的真实性和不可篡改性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

当了数据结构的助教,还要修改作业。在学生们的作业本上留下了对错号。但是没有写其他的东西。为了防止有学生来冒充我的笔迹来自行打对错号,然后声称没有给分这种现象的发生。我花了一个小时做了一个数字签名。因为我的笔迹很烂,很容易模仿。

代码如下:基本原理使用的Hash散列。所以不知道签名数据的人,绝对可能从自己作业的签名中获取签名数据,自然也不可能去冒充一个签名。

我使用的mysql的散列函数

#include"stdafx.h"
#include
<iostream>
#include
<string>

usingnamespacestd;

intiHTLen;

//mySqlhash
unsignedintHash(constchar*keys,constintlen)
...{
constchar*key=keys;
//Hashfunctionforcharacterkeys
intlength=len;
intnr=1,nr2=4;
while(length--)
...{
nr
^=(((nr&63)+nr2)*(*key++))+(nr<<8);
nr2
+=3;
}

return((unsignedint)nr)%iHTLen;
}


//justTesttheconsequencearecorrectornot
boolTestHashValue(constunsignedinthashValue[],intlen)
...{
for(inti=0;i<len;i++)
...{
for(intj=i+1;j<len;j++)
...{
if(hashValue[i]==hashValue[j])
...{
returnfalse;
}

}

}

returntrue;
}


//guaranteetheinputvaluesarenotthesame
boolTestPreValue(conststringstudents[],intlen)
...{
for(inti=0;i<len;i++)
...{
for(intj=i+1;j<len;j++)
...{
if(students[i]==students[j])
...{
returnfalse;
}

}

}

returntrue;
}


unsigned
intHash(stringstudentName,stringdate,stringsigner)
...{
intlen=studentName.length()+date.length()+signer.length();
char*sign=newchar[len];
memset(sign,
0,len);
strcpy(sign,studentName.c_str());
strcat(sign,date.c_str());
strcat(sign,signer.c_str());

returnHash(sign,len);

}



int_tmain(intargc,_TCHAR*argv[])
...{

intnum=0;
cin
>>num;
string*students=newstring[num];
stringsignName;
stringdate;

for(inti=0;i<num;i++)
...{
cin
>>students[i];
}


cin
>>date;
cin
>>signName;
cin
>>iHTLen;
boolisGoon=TestPreValue(students,num);
if(!isGoon)
...{
cout
<<"students'namesarethesame?"<<endl;
exit(
2);
}


unsigned
int*signNum=newunsignedint[num];
for(inti=0;i<num;i++)
...{
signNum[i]
=Hash(students[i],signName,date);
}


boolisSuccess=TestHashValue(signNum,num);
if(!isSuccess)
...{
cout
<<"pleasechangethelastvalueandrunagain!"<<endl;
exit(
2);
}


for(inti=0;i<num;i++)
...{
cout
<<students[i]<<" "<<signNum[i]<<endl;
}


return0;
}

幸好没有重名的学生。(*^__^*) 嘻嘻……

以上的代码使用VS2005的编译器。不过数据不方便透露,所以大家看懂代码可以自己输入自己的签名数据。大家不必在意Hash函数是如何去写的。因为我也不清楚为什么这样写。

以后改作业可以省心了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值