- 4
- 0
- 约2.29千字
- 约 11页
- 2022-11-15 发布于江苏
- 举报
实验三-DSA数字签名算法
实验三-DSA数字签名算法
不知道私钥,你也能确认它们是否是随机产生的,还是作了手脚。RSA算法却做不到。
源码描述
#include stdafx.h
#include string.h
#include stdio.h
#include BigInt.h
#include sha1.h
#include time.h
int shas1(const unsigned int x[], unsigned char digest[20])
{
SHA1_CTX context;
unsigned char buffer[16384]; //,digest[20];
// FILE *file;
SHA1Init(context);
SHA1Update(context, buffer, 1);
SHA1Final(digest, context);
return 0;
}
CBigInt sha(CBigInt y)
{
SHA1_CTX context;
CBigInt X;
unsigned char buffer[16384],digest[20];
CString str;
char *t=0123456789ABCDEF;
if((y.m_nLength==1)(y.m_ulValue[0]==0))
{
str=0;
X.Mov(0);
return X;
}
str=;
int a;
char ch=\0;
str.Insert(0,ch);
X.Mov(y);
while(X.m_ulValue[X.m_nLength-1]0)
{
a=X.Mod(16);
ch=t[a];
str.Insert(0,ch);
X.Mov(X.Div(16));
}
int i=0;
while (str[i]0)
i++;
for (a=i,i=0;ia;i++)
buffer[i]=str[i];
for (i=a;i64;i++) buffer[i]=\0;
SHA1Init(context);
SHA1Update(context, buffer, 1);
SHA1Final(digest, context);
int len=str.GetLength(),k;
X.Mov(0);
for(i=0;i20;i++)
{
X.Mov(X.Mul(256));
// if((digest[i]=0)(digest[i]=9))
k=digest[i];
// else if((digest[i]=A)(digest[i]=F))k=digest[i]-55;
// else if((digest[i]=a)(digest[i]=f))k=digest[i]-87;
// else k=0;
X.Mov(X.Add(k));
}
return X;
}
CBigInt makerandnumber( unsigned int len)
{
srand(time(NULL));
CBigInt X;
X.m_nLength=(len+31)/32;
// X.Mov(0);
for (unsigned int j=0;jX.m_nLength;j++)
{
X.m_ulValue[j]=rand()*0x10000+rand();
}
X.m_ulValue[0]=X.m_ulValue[0]|0x1;
X.m_ulValue[j-1]=X.m_ulValue[j-1]|0
return X;
}
CBigInt pow2( const int x, int y)
{
CBigInt p,q,t;
p.Mov(1);
q.Mov(x);
while (y0)
{
if (y%2==0) {q.Mov(q.Mul(q)); y /= 2 ; }
else { p.Mov(p.Mul(q)); y -= 1; }
}
return p;
}
CBigInt pow3(CBigInt x, CBigInt y, CBigInt m)
{
CBigInt p,t;
p.Mov(1);
t.Mov(0);
while (y.Cmp(t)0)
{
if (y.m
原创力文档

文档评论(0)