首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

怎么计算两个128位二进制数的相乘

2012-03-02 
如何计算两个128位二进制数的相乘我是做RSA运算,两个128位(或更多位)二进制数相乘怎么算?[解决办法]给你个

如何计算两个128位二进制数的相乘
我是做RSA运算,两个128位(或更多位)二进制数相乘怎么算?

[解决办法]
给你个以前写的大数相乘的例子,是十进制数的,但方法一样的:
#include <stdio.h>
#include <stdlib.h>
#define MAX 6000
int main(){

/*the Array to store two number and result*/
int Left[MAX],Right[MAX],Result[MAX*MAX],Temp[MAX*MAX];
int LeftLength,RightLength; /*user define the number 's length*/
FILE *fp; /*open file to store the two random number*/
int MulTemp=0; /*the temp varible for each multipe*/
int AddTemp=0; /*the temp varible for each add*/
int MulCarray=0,AddCarray=0; /*each mul and add carray*/
int i,j,k; /*the loop variable*/

for(i=0;i <MAX;){ /*new*/
Left[i]=0;
Right[i]=0;
Result[i]=0;
Temp[i]=0;
i++;
}
printf( "\n************************************************************ ");
printf( "\n| | ");
printf( "\n| To multipe tow very big integer number | ");
printf( "\n| the number 's length less than 5000 will be do correct | ");
printf( "\n| result will be saved to e:\\ result.txt | ");
printf( "\n| author:w_chen 2007.4 | ");
printf( "\n************************************************************ ");
printf( "\n---------Please input the two number 's length---- ");
printf( "\ninput a number and press Enter to input another... ");
printf( "\nfirst number 's length: ");
scanf( "%d ",&LeftLength);
printf( "second number 's length: ");
scanf( "%d ",&RightLength);
if((fp=fopen( "e:\\result.txt ", "wb+ "))==NULL){
printf( "\nopenfile error!! ");
return 0;
}
printf( "\nnow we 're make the random number and multipe... ");
printf( "\n ");
fputs( "\n ",fp);
for(i=MAX-LeftLength;i <=MAX-1;){
Left[i]=random(10);
fputc(48+Left[i],fp);
printf( "%d ",Left[i]);
i++;
}
printf( "\n\nX\n\n ");
fputc(13,fp);
fputs( "\n\n X \n\n ",fp);
for(i=MAX-RightLength;i <=MAX-1;){
Right[i]=random(10);
fputc(48+Right[i],fp);
printf( "%d ",Right[i]);
i++;
}
printf( "\n ");
for(i=MAX-1;i> =MAX-RightLength;){
for(k=0;k <=MAX-1;){
Temp[k]=0;
k++;
}
MulCarray=0;AddCarray=0;
for(j=MAX-1;j> =MAX-LeftLength;){
MulTemp=Left[j]*Right[i]+MulCarray;
if(MulTemp> =10)
MulCarray=MulTemp/10;
else
MulCarray=0;
Temp[i-MAX+1+j]=MulTemp-10*MulCarray;
j--;
}
if(MulCarray!=0){
Temp[i-MAX+1+j]=MulCarray;
MulCarray=0;
}
for(k=MAX-1;k> =i-MAX+1+j;){
AddTemp=Result[k]+Temp[k]+AddCarray;
if(AddTemp> =10)
AddCarray=AddTemp/10;
else
AddCarray=0;
Result[k]=AddTemp-10*AddCarray;
k--;
}
i--;
}
printf( "\n--------------------------\n ");
fputc(13,fp);
fputs( "\n\n------------------------\n\n ",fp);
fputc(13,fp);
for(k=0;Result[k]==0;k++);


k--;
for(;k <=MAX-1;){
printf( "%d ",Result[k]);
fputc(48+Result[k],fp);
k++;
}
fclose(fp);
getchar();
}
TC2.0下调试通过,模拟手算的过程...希望能对LZ有所帮助~

热点排行