很早的一篇文章了,放到这里纪念下 :-)
文章作者:东方 [E.S.T](穆东栋)
信息来源:邪恶八进制信息安全团队()
#!/bin/perl
#名称:基于RSA算法的文件签名程序
#作者:穆东栋 [E.S.T]
#版本:1.0(演示版)
#################################################################
#
#说明:E.txt D.txt N.txt存放的是10进制的密钥文件
#oldfile.txt是要签名的文件,MD5.txt是oldfile.txt的md5值
#filesign.txt是对MD5.txt签名后的文件
#功能:对当前目录下的odlfile.txt用(E,N)签名,并且验证签名是否正确
#
#################################################################
#生成当前目录下的oldfile.txt文件的MD5变换文件MD5.txt
use Digest::MD5 qw(md5_hex);
use Math::BigInt;
print "开始生成MD5文件................\n";
open(file1,"
open(file2,">MD5.txt")||die("无法建立MD5.txt文件");
my $ctx=Digest::MD5->new;
$ctx->addfile(file1);
my $digest=$ctx->hexdigest; #$digest里面就是后面要签名用的hash值,为16进制
print file2 $digest;
close(file1);
close(file2);
print "成功生成MD5文件\n";
print "0x".$digest,"\n\n";
#开始签名
####################################################################
#获得私钥$D,$N
open(fileD,"
$d=
;
close(fileD);
open(fileE,"
$e=;
close(fileE);
open(fileN,"
$n=;
close(fileN);
$D=new Math::BigInt($d);
$E=new Math::BigInt($e);
$N=new Math::BigInt($n);
####################################################################
#对MD5.txt中的值签名成filesign.txt
#此时$D,$N,$digest都是16进制的数,
print "开始签名................\n";
$digest=new Math::BigInt("0x".$digest);
$digestMD5=$digest; #$digestMD5中保留了MD5值
$digest=Math::BigInt->bmodpow($digest,$D,$N); #此时$bigest是签名后的值
open(filenew,">filesign.txt")||die("无法建立filesign.txt文件");
print filenew $digest;
print "签名后的值为:\n";
print $digest->as_hex,"\n\n";
close(filenew);
####################################################################
#用解密的方法开始验证签名的正确性
print "开始验证签名.................\n";
$digest=Math::BigInt->bmodpow($digest,$E,$N); #解密签名
print "解密签名后的值为\n";
print $digest->as_hex,"\n";
print (($digest eq $digestMD5)?"签名成功":"签名失败");
附:
----------E.txt-----------
17
----------D.txt--------------
11991939606355182596939360387828150379931369979400268202633737702429943716307197316616194187390726021699688206538505080014031572262763719231474751930052745671933474322012941791370088951121677035389807200434322865219480512399843714614132692256700913970093204178347639079864665083099545763160881324086080562505
---------N.txt----------------
14561640950574150296283509042362754032773806403557468531769538638664931655515882455891092941831595883492478536511041882874181194890498801923933627343635486691604898872748503483061222525880898880133928924301353844283585363829921546926895663272082589630916565177608553446405511145363976459362181566677388966617
阅读(884) | 评论(0) | 转发(0) |