📄 my_ecc.cpp
字号:
return MP_OKAY;
}
if(chlong<7)
{
i+=4;
*++temp |= (mp_digit)(ch[i-1] & yy);
*temp <<= (mp_digit)CHAR_BIT;
*temp |= (mp_digit)(ch[i-2] & 255);
*temp <<= (mp_digit)CHAR_BIT;
*temp |= (mp_digit)(ch[i-3] & 255);
*temp <<= (mp_digit)CHAR_BIT;
*temp-- |= (mp_digit)(ch[i-4] & 255); //存放被切分的字符的低四位
for(j=chlong-1;j>=i;j--)
{
*temp |= (mp_digit)(ch[j] & 255);
*temp <<= (mp_digit)CHAR_BIT;
}
*temp >>= (mp_digit)4;
*temp |= (mp_digit)((ch[i-1] & 255) >> 4); //存放被切分的字符的高四位
a->used=2;
return MP_OKAY;
}
//以7个字符为单元循环,把七个字符放入的mp_int 的两个单元中
for(j=0;j<chlong/7;j++)
{
i+=7;
*++temp |= (mp_digit)(ch[i-1] & 255);
*temp <<= (mp_digit)CHAR_BIT;
*temp |= (mp_digit)(ch[i-2] & 255);
*temp <<= (mp_digit)CHAR_BIT;
*temp |= (mp_digit)(ch[i-3] & 255);
*temp <<= (mp_digit)4;
*temp-- |= (mp_digit)((ch[i-4] & 255) >> 4); //存放被切分的字符的高四位
*temp |= (mp_digit)(ch[i-4] & yy); //存放被切分的字符的低四位
*temp <<= (mp_digit)CHAR_BIT;
*temp |= (mp_digit)(ch[i-5] & 255);
*temp <<= (mp_digit)CHAR_BIT;
*temp |= (mp_digit)(ch[i-6] & 255);
*temp <<= (mp_digit)CHAR_BIT;
*temp++ |= (mp_digit)(ch[i-7] & 255);
temp++;
}
if((chlong>=7)&&(chlong%7!=0)) //剩余字符的存放
{
if(chlong%7 < 4) //剩余字符少余4个时,只需一个mp_digit单元存放
{
for(j=chlong-1;j>=i;j--)
{
*temp |= (mp_digit)(ch[j] & 255);
*temp <<= (mp_digit)CHAR_BIT;
}
*temp >>= (mp_digit)8;
a->used=chlong*2/7+1;
}
else
{ //剩余字符不小于4个时,需两个mp_digit单元存放
i+=4;
*temp |= (mp_digit)(ch[i-1] & yy);
*temp <<= (mp_digit)CHAR_BIT;
*temp |= (mp_digit)(ch[i-2] & 255);
*temp <<= (mp_digit)CHAR_BIT;
*temp |= (mp_digit)(ch[i-3] & 255);
*temp <<= (mp_digit)CHAR_BIT;
*temp++ |= (mp_digit)(ch[i-4] & 255); //存放被切分的字符的低四位
for(j=chlong-1;j>=i;j--)
{
*temp |= (mp_digit)(ch[j] & 255);
*temp <<= (mp_digit)CHAR_BIT;
}
*temp >>= (mp_digit)4;
*temp |= (mp_digit)((ch[i-1] & 255) >> 4); //存放被切分的字符的高四位
a->used=chlong*2/7+2;
}
}
else
{
a->used=chlong*2/7;
}
return MP_OKAY;
}
void Ecc_encipher(mp_int *qx,mp_int *qy, mp_int *px, mp_int *py,mp_int *a,mp_int *p){
mp_int mx, my;
mp_int c1x, c1y;
mp_int c2x, c2y;
mp_int r;
mp_int tempx, tempy;
bool zero=false;
FILE *fp,*fq;
int i;
char miwenx[280]={0};
char miweny[280]={0};
char stemp[650]={0};
mp_init(&mx);
mp_init(&my);
mp_init(&c1x);
mp_init(&c1y);
mp_init(&c2x);
mp_init(&c2y);
mp_init(&r);
mp_init(&tempx);
mp_init(&tempy);
GetPrime(&r, 100);
char filehead[60],filefoot[20],filename[85]={0};
cout<<"请输入您要加密文件的存放路径和文件名(如: c:\\000\\大整数运算 ):"<<endl;
cin>>filehead;
cout<<"请输入您要加密文件的扩展名(如: .doc ):"<<endl;
cin>>filefoot;
strcpy(filename,filehead);
strcat(filename,filefoot);
//打开要加密文件
if((fp=fopen(filename,"rb"))==NULL)
{
printf("can not open the file!");
exit(1);
}
unsigned int FileLong=0;//文件字符长度
char ChTem;//临时字符变
int Frequency=0;
int Residue=0;
while( !feof(fp) )//找文件字符长度
{
ChTem = fgetc( fp );
FileLong++;
}
--FileLong;
Frequency = FileLong/EN_LONG;
Residue = FileLong%EN_LONG;
int enlongtemp=EN_LONG/2;
//printf("%d\n",Frequency);
//printf("%d\n",Residue);
char filemi[85];
strcpy(filemi,filehead);
strcat(filemi,"密文");
strcat(filemi,filefoot);
//打开保存密文文件
if((fq=fopen(filemi,"wb"))==NULL)
{
printf("can not open the file!\n");
exit(1);
}
printf("\n开始加密...\n");
rewind(fp);
for(i=0; i<Frequency; i++)
{
fread(miwenx,1,enlongtemp,fp);//读入字符串
miwenx[enlongtemp]=char(255);
fread(miweny,1,enlongtemp,fp);//读入字符串
miweny[enlongtemp]=char(255);
putin(&mx, miwenx,enlongtemp+1);//文件存入
putin(&my, miweny,enlongtemp+1);//文件存入
Ecc_points_mul(&c2x,&c2y,px,py,&r,a,p);//加密
Ecc_points_mul(&tempx,&tempy,qx,qy,&r,a,p);
Two_points_add(&mx,&my,&tempx,&tempy,&c1x,&c1y,a,zero,p);
//保存密文
chmistore(&c1x,fq);
chmistore(&c1y,fq);
chmistore(&c2x,fq);
chmistore(&c2y,fq);
}
//剩余字符处理
if ( Residue > 0)
{
if (Residue <= enlongtemp )
{
fread(miwenx,1,Residue,fp);//读入字符串
miwenx[Residue]=char(255);
putin(&mx, miwenx,Residue+1);//文件存入
mp_zero(&my);
}
else
{
fread(miwenx,1,enlongtemp,fp);//读入字符串
miwenx[enlongtemp]=char(255);
fread(miweny,1,Residue-enlongtemp,fp);//读入字符串
miweny[Residue-enlongtemp]=char(255);
putin(&mx, miwenx,enlongtemp+1);//文件存入
putin(&my, miweny,Residue-enlongtemp+1);//文件存入
}
Ecc_points_mul(&c2x,&c2y,px,py,&r,a,p);//加密
Ecc_points_mul(&tempx,&tempy,qx,qy,&r,a,p);
Two_points_add(&mx,&my,&tempx,&tempy,&c1x,&c1y,a,zero,p);
//保存密文
chmistore(&c1x,fq);
chmistore(&c1y,fq);
chmistore(&c2x,fq);
chmistore(&c2y,fq);
}
cout<<"\nok!加密完毕!"<<endl;
cout<<"密文以二进制保存"<<endl;
cout<<"密文存放路径为 "<<filemi<<endl ;
fclose(fq);
fclose(fp);
mp_clear(&mx);
mp_clear(&my);
mp_clear(&c1x);
mp_clear(&c1y);
mp_clear(&c2x);
mp_clear(&c2y);
mp_clear(&r);
mp_clear(&tempx);
mp_clear(&tempy);
}
//取密文
int miwendraw(mp_int *a,char *ch,int chlong)
{
mp_digit *temp;
int i,j,res;
if(a->alloc<chlong/4)
{
if((res=mp_grow(a,chlong/4))!=MP_OKAY)
return res;
}
a->alloc=chlong/4;
a->sign=0;
mp_zero(a);
temp=a->dp;
i=0;
for(j=0;j<chlong/4;j++)
{
i+=4;
*temp |= (mp_digit)(ch[i-4] & 255);
*temp <<= (mp_digit)CHAR_BIT;
*temp |= (mp_digit)(ch[i-3] & 255);
*temp <<= (mp_digit)CHAR_BIT;
*temp |= (mp_digit)(ch[i-2] & 255);
*temp <<= (mp_digit)CHAR_BIT;
*temp++ |= (mp_digit)(ch[i-1] & 255);
}
a->used=chlong/4;
return MP_OKAY;
}
//实现将mp_int数a中的比特串还原为字符串并赋给字符串ch:
int chdraw(mp_int *a,char *ch)
{
int i,j;
mp_digit *temp,xx,yy;
temp=a->dp;
i=0;
yy=(mp_digit)255; //用于位与运算,取八位比特串
xx=(mp_digit)15; //用于位与运算,取四位比特串
for(j=0;j<a->used/2;j++) //以两个单元为循环,把两个单元的比特串赋给7个字符
{
i+=7;
ch[i-4]=(char)(*++temp & xx);
ch[i-3]=(char)((*temp >> (mp_digit)4) & yy);
ch[i-2]=(char)((*temp >> (mp_digit)12) & yy);
ch[i-1]=(char)((*temp-- >> (mp_digit)20) & yy);
ch[i-7]=(char)(*temp & yy);
ch[i-6]=(char)((*temp >> (mp_digit)8) & yy);
ch[i-5]=(char)((*temp >> (mp_digit)16) & yy);
ch[i-4] <<= 4;
ch[i-4]+=(char)((*temp++ >> (mp_digit)24) & xx);
temp++;
}
if(a->used%2!=0) //剩于一个单元的处理
{
ch[i++] = (char)(*temp & yy);
ch[i++] = (char)((*temp >> (mp_digit)8) & yy);
ch[i++] = (char)((*temp >> (mp_digit)16) & yy);
}
--i;
while(int(ch[i]&0xFF) != 255 && i>0) i--;
return i;
}
void Ecc_decipher(mp_int *k, mp_int *a,mp_int *p){
mp_int c1x, c1y;
mp_int c2x, c2y;
mp_int tempx, tempy;
mp_int mx, my;
mp_int temp;
mp_init(&temp);
mp_init(&c1x);
mp_init(&c1y);
mp_init(&c2x);
mp_init(&c2y);
mp_init(&tempx);
mp_init(&tempy);
mp_init(&mx);
mp_init(&my);
mp_int tempzero;
mp_init(&tempzero);
int i;
char stemp[700]={0};
FILE *fp,*fq;
bool zero=false;
char filehead[60],filefoot[20],filename[85]={0};
cout<<"请输入您要解密的文件的存放路径和文件名(如: c:\\000\\大整数运算 ):"<<endl;
cin>>filehead;
cout<<"请输入您要解密的文件的扩展名(如: .doc ):"<<endl;
cin>>filefoot;
strcpy(filename,filehead);
strcat(filename,filefoot);
printf("\n开始解密\n");
if((fp=fopen(filename,"rb"))==NULL)
{
printf("can not open the file!");
exit(1);
}
//打开保存解密结果文件
char filemi[80];
strcpy(filemi, filehead);
strcat(filemi, "解密");
strcat(filemi, filefoot);
if((fq=fopen(filemi,"wb"))==NULL)
{
printf("can not open the file!");
exit(1);
}
rewind(fp);
while(!feof(fp))
{
i=0;
while(1)
{
stemp[i]=fgetc(fp);
if(i%4==0)
{
if(int(stemp[i]&0xFF) == 255 ) goto L1;
}
i++;
}
L1: miwendraw(&c1x, stemp, i);
i=0;
while(1)
{
stemp[i]=fgetc(fp);
if(i%4==0)
{
if(int(stemp[i]&0xFF) == 255 ) goto L2;
}
i++;
}
L2: miwendraw(&c1y, stemp, i);
i=0;
while(1)
{
stemp[i]=fgetc(fp);
if(i%4==0)
{
if(int(stemp[i]&0xFF) == 255 ) goto L3;
}
i++;
}
L3: miwendraw(&c2x, stemp, i);
i=0;
while(1)
{
stemp[i]=fgetc(fp);
if(i%4==0)
{
if(int(stemp[i]&0xFF) == 255 ) goto L4;
}
i++;
}
L4: miwendraw(&c2y, stemp, i);
mp_zero(&tempzero);
if(mp_cmp(&c1x, &tempzero)==0) break;
Ecc_points_mul(&tempx, &tempy, &c2x, &c2y, k, a, p);
mp_neg(&tempy, &temp);
Two_points_add(&c1x,&c1y,&tempx,&temp,&mx,&my,a,zero,p);
int chtem;
chtem=chdraw(&mx,stemp);//从ming中取出字符串
//保存解密结果
for(int kk=0;kk<chtem;kk++)
{
fprintf(fq,"%c",stemp[kk]);
}
chtem=chdraw(&my,stemp);//从ming中取出字符串
//保存解密结果
for(kk=0;kk<chtem;kk++)
{
fprintf(fq,"%c",stemp[kk]);
}
}
cout<<"\nok!解密完毕!"<<endl;
cout<<"解密后的文字存放路径为 "<<filemi<<endl;
fclose(fq);
fclose(fp);
mp_clear(&c1x);
mp_clear(&c1y);
mp_clear(&c2x);
mp_clear(&c2y);
mp_clear(&tempx);
mp_clear(&tempy);
mp_clear(&mx);
mp_clear(&my);
mp_clear(&temp);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -