www.pudn.com > inet_stock.zip > RSA.C


#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define	DATALENGTH	350
#define	MLENGTH		26
#define	TESTNUM		20
#define	SKLENGTH	4
#define TEXTLENGTH	20

typedef signed char byteint[DATALENGTH]; //-128 ->127
typedef signed char mtype[MLENGTH];

mtype	*Model;

byteint ONEVALUE,ZEROVALUE,TWOVALUE,EIGHTVALUE;

//byteint *plaintext;
//byteint *cipertext;

void InitInt(void);
void SetZero(byteint A);
void IntCpy(byteint A,byteint B);
int IntValid(byteint validtemp);

int IntCmp(byteint A,byteint B);
void Plus(byteint A,byteint B,byteint C);
void Substract(byteint SA,byteint SB,byteint SC);
void Multiply(byteint A,byteint B,byteint C);
void SetMode(byteint A,byteint B,byteint C,byteint D);

void IntRandom(byteint RandomA,int num);

void LoadInt(byteint A,mtype B);
void Mdata(void);
void TransBi(byteint B,signed char flag[400]);

int PowerMode(byteint A,byteint C,byteint D,signed char flag[400]);

int Prime(byteint Prm);
int ComputingPK(byteint Rvalue,byteint SK,byteint PK);
int StrToByt(char *str,byteint byt);
void BytToStr(byteint byt,char *str);

void PackInt(byteint A,int B);

byteint R,SK,PK,RsaKey; 
int RsaOk =0;

extern int UDPBlockingHook(void);
extern char szDataPath[128];

int RsaPrepare(byteint sk,byteint pk,byteint r)
{
 	byteint p,q,Rvalue,buf1,buf2;
	
	Model =(mtype *)_fmalloc(TESTNUM*DATALENGTH);
	Mdata();
	InitInt();

	Prime(p);
	Prime(q);	
	Multiply(p,q,r);
	Substract(p,ONEVALUE,buf1);
	Substract(q,ONEVALUE,buf2);
	Multiply(buf1,buf2,Rvalue);
	ComputingPK(Rvalue,sk,pk); 	
	_ffree(Model);
	return TRUE;
}

int ReadRsaPk(int no,byteint r,byteint pk)
{
	char file[80],item[10],temp[100+1];

	sprintf(file,"%s\\sysinfo.dat",szDataPath);
	sprintf(item,"QS%d",no);
	
	if(GetPrivateProfileString("ZQS_RSA_R", item, "", temp,100, file)!=0)
		StrToByt(temp,r);	
	else
		return FALSE;
	if(GetPrivateProfileString("ZQS_RSA_PK", item, "", temp, 100, file)!=0)
		StrToByt(temp,pk);
	else
		return FALSE;
	return TRUE;
		
}


int WriteRsaPk(int no,byteint r,byteint pk)
{
	char file[80],item[10],temp[100+1];

	sprintf(file,"%s\\sysinfo.dat",szDataPath);
	sprintf(item,"QS%d",no);
	
	memset(temp,0,100+1);
	BytToStr(r,temp);
	if(WritePrivateProfileString("ZQS_RSA_R", item, temp, file)==0)
		return FALSE;
	memset(temp,0,100+1);
	BytToStr(pk,temp);	
	if(WritePrivateProfileString("ZQS_RSA_PK", item, temp,file)==0)
		return FALSE;
	return TRUE;		
}

int ReadRsaCiper(int no,byteint ciper,char *deskey)
{  
	char file[80],item[10],temp[100+1];
 
    sprintf(file,"%s\\sysinfo.dat",szDataPath);
    sprintf(item,"QS%d",no);

	if(GetPrivateProfileString("ZQS_DES_KEY", item, "", deskey, 100, file)==0)
		return FALSE;
	if(GetPrivateProfileString("ZQS_DES_RSA", item, "", temp, 100, file)==0)
		return FALSE;
	StrToByt(temp,ciper);
    return TRUE;
}

int WriteRsaCiper(int no,byteint ciper,char *deskey)
{  
	char file[80],item[10],temp[100+1];
 
    sprintf(file,"%s\\sysinfo.dat",szDataPath);
    sprintf(item,"QS%d",no);

	if(WritePrivateProfileString("ZQS_DES_KEY", item, deskey, file)==0)
		return FALSE;
	memset(temp,0,100+1);	
	BytToStr(ciper,temp);
	if(WritePrivateProfileString("ZQS_DES_RSA", item, temp, file)==0)
		return FALSE;
    return TRUE;
}

int EncipherDesKey(char *deskey,byteint r,byteint pk,byteint rsakey)
{
	signed char flag[400];
    byteint buf1;

	Model =(mtype *)_fmalloc(TESTNUM*DATALENGTH);
	
	Mdata();
	InitInt();
	
	//if(deskey[0] ==0)	
		IntRandom(buf1,8);
    //else
    //    StrToByt(deskey,buf1);
	TransBi(pk,flag);
	PowerMode(buf1,r,rsakey,flag);			
	memset(deskey,0,9);
    BytToStr(buf1,deskey);    
	
	_ffree(Model);
	return 0;
}


int DecipherDesKey(byteint rsakey,byteint r,byteint sk,char *deskey)
{
	byteint buf1;
	signed char flag[400];

	Model =(mtype *)_fmalloc(TESTNUM*DATALENGTH);
		
	Mdata();
	InitInt();

	TransBi(PK,flag);
	PowerMode(rsakey,r,buf1,flag);
	memset(deskey,0,9);
    BytToStr(buf1,deskey);
    
	_ffree(Model);
	
	return 0;
}

void InitInt(void)
{
	SetZero(ONEVALUE);
	ONEVALUE[DATALENGTH-1]=1;
	SetZero(ZEROVALUE);
	SetZero(TWOVALUE);
	TWOVALUE[DATALENGTH-1]=2;
	SetZero(EIGHTVALUE);
	EIGHTVALUE[DATALENGTH-1]=8;
	//randomize();
	srand((unsigned)time(NULL));
}


void Multiply(byteint A,byteint B,byteint C)
{
	register i,j,w;
	int X,Y,Z;
	int Avalid=0;
	int Bvalid=0;

	while(A[Avalid]==0 &&Avalid =Avalid;i--)
	{
		for(j=DATALENGTH-1;j>=Bvalid;j--)
		{
			X=A[i]*B[j];
			Y=X/10;
			Z=X-10*Y;
			w=i+j-(DATALENGTH-1);
			C[w]=C[w]+Z;
			C[w-1]=C[w-1]+(C[w]/10)+Y;
			C[w]=C[w]-(C[w]/10)*10;
		}
	}
}

void SetZero(byteint A)
{
	memset(A,0,DATALENGTH);
}

void IntCpy(byteint A,byteint B)
{
	memcpy(A,B,DATALENGTH);
}

void Plus(byteint A,byteint B,byteint C)
{
	register i;
	int X,Y,Z,m,n,valid;

	m=IntValid(A);
	n=IntValid(B);
	valid=(m>n) ? m+1:n+1;
	SetZero(C);
	for(i=DATALENGTH -1;i>=DATALENGTH -valid;i--)
	{
		X=A[i] +B[i];
		Y=X/10;
		Z=X-10*Y;

		C[i]=C[i]+Z;
		C[i-1]=C[i-1]+Y;
	}
}

void Substract(byteint SA,byteint SB,byteint SC)
{
	byteint buf;
	register i,j;
	int X;

	IntCpy(buf,SA);
	SetZero(SC);

	for(i=DATALENGTH-1;i>=0;i--)
	{
		if(buf[i]0)
				(buf[i-1])--;
			else {
				j=i-1;
				while (buf[j]==0)
					buf[j--]=9;
				buf[j]--;
			}
		}
		X=buf[i]-SB[i];
		SC[i]=X;
	}
}

int IntCmp(byteint A,byteint B)
{
	int stat;

	stat =memcmp(A,B,DATALENGTH);
	if(stat ==0)
		return 0;
	if(stat>0)
		return 1;
	return -1;
}

int IntValid(byteint validtemp)
{
	register i=0;
	while (validtemp[i]==0 && i0){
		valid_1 =IntValid(C);
		valid =valid_1-valid_2;
		if(valid>0){
			i=DATALENGTH -valid_1;
			j=DATALENGTH -valid_2;
			sbits=0;
			for(k=j;kB[j])
					break;
				if(C[i]=DATALENGTH -num +1)
		RandomA[i--]=rand() % 10;
}

void LoadInt(byteint A,mtype B)
{
	register i,j;

	SetZero(A);
	i=DATALENGTH -1;
	j=MLENGTH -1;
	while(j>=0)
		A[i--]=B[j--];
}

void Mdata(void)
{
	register i,j;

	int k=MLENGTH -2;
	memset(Model,0,TESTNUM*MLENGTH);
	for(i=0;i=k;j--)
			Model[i][j]=rand()%10;
		k-=1;
	}
}

void TransBi(byteint B,signed char flag[400])
{
	byteint buf;
	byteint result;
	byteint temp;
	register i;

	memset(flag,0,400);
	i=399;
	IntCpy(buf,B);
	while(IntCmp(buf,ZEROVALUE)==1){
		UDPBlockingHook();
		SetMode(buf,TWOVALUE,temp,result);
		flag[i]=temp[DATALENGTH -1];
		IntCpy(buf,result);
		i--;
	}
	flag[i] =-1;
}

int PowerMode(byteint A,byteint C,byteint D,signed char flag[400])
{
	byteint buf;
	byteint result;
	byteint temp,P;
	register i;

	IntCpy(temp,A);
	if(flag[399]==1)
		IntCpy(result,A);
	else
		IntCpy(result,ONEVALUE);
	i=398;
	while(flag[i]!=-1){
		UDPBlockingHook();
		Multiply(temp,temp,buf);
		SetMode(buf,C,temp,P);
		if(flag[i]!=0){
			Multiply(temp,result,buf);
			SetMode(buf,C,result,P);
		}
		i--;
	}
	IntCpy(buf,C);
	IntCpy(D,result);
	Substract(buf,ONEVALUE,temp);
	if(IntCmp(result,ONEVALUE)==0)
		return 1;
	if(IntCmp(result,temp)==0)
		return 2;
	return 0;
}

int Prime(byteint Prm)
{
	int i,k,ok,cnt=0;
	signed char flag[400];
	byteint A,B,D,buf1,buf2;

	while(1){
		int pass=0,pass_2=0;
		cnt++;
		IntRandom(B,MLENGTH);

		IntCpy(Prm,B);
		Substract(B,ONEVALUE,buf1);
		SetMode(buf1,TWOVALUE,buf2,B);
		TransBi(B,flag);
		ok=1;
		for(i=0;i0){
		A[i--]=B % 10;
		B=B/10;
	}
}

int StrToByt(char *str,byteint byt)
{
	unsigned int m;
	
	SetZero(byt);
	for(m=0;m