📄 ecc.c
字号:
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<<"File to encrypt(e.g: c:\\code\\ecc\\test ):"<<endl;
cin>>filehead;
cout<<"Extension(e.g: .txt ):"<<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,"en");
strcat(filemi,filefoot);
//打开保存密文文件
if((fq=fopen(filemi,"wb"))==NULL)
{
printf("can not open the file!\n");
exit(1);
}
printf("\nencrypting...\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!encrypted!"<<endl;
cout<<"Saved in binary"<<endl;
cout<<"Patch: "<<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, kk;
char stemp[700]={0};
FILE *fp,*fq;
bool zero=false;
char filehead[60],filefoot[20],filename[85]={0};
cout<<"File to decrypt(e.g: c:\\code\\ecc\\testen ):"<<endl;
cin>>filehead;
cout<<"Extension(e.g: .txt ):"<<endl;
cin>>filefoot;
strcpy(filename,filehead);
strcat(filename,filefoot);
printf("\ndecrypting\n");
if((fp=fopen(filename,"rb"))==NULL)
{
printf("can not open the file!");
exit(1);
}
//打开保存解密结果文件
char filemi[80];
strcpy(filemi, filehead);
strcat(filemi, "de");
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(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!decrypted!"<<endl;
cout<<"Patch: "<<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);
}
/*以下为libtomath库*/
#ifdef BN_MP_INIT_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
* integer arithmetic as well as number theoretic functionality.
*
* The library was designed directly after the MPI library by
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tomstdenis@gmail.com, http://libtom.org
*/
/* init a new mp_int */
int mp_init (mp_int * a)
{
int i;
/* allocate memory required and clear it */
a->dp = OPT_CAST(mp_digit) XMALLOC (sizeof (mp_digit) * MP_PREC);
if (a->dp == NULL) {
return MP_MEM;
}
/* set the digits to zero */
for (i = 0; i < MP_PREC; i++) {
a->dp[i] = 0;
}
/* set the used to zero, allocated digits to the default precision
* and sign to positive */
a->used = 0;
a->alloc = MP_PREC;
a->sign = MP_ZPOS;
return MP_OKAY;
}
#endif
/* $Source: /cvs/libtom/libtommath/bn_mp_init.c,v $ */
/* $Revision: 1.4 $ */
/* $Date: 2006/12/28 01:25:13 $ */
#ifdef BN_MP_TORADIX_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
* integer arithmetic as well as number theoretic functionality.
*
* The library was designed directly after the MPI library by
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tomstdenis@gmail.com, http://libtom.org
*/
/* stores a bignum as a ASCII string in a given radix (2..64) */
int mp_toradix (mp_int * a, char *str, int radix)
{
int res, digs;
mp_int t;
mp_digit d;
char *_s = str;
/* check range of the radix */
if (radix < 2 || radix > 64) {
return MP_VAL;
}
/* quick out if its zero */
if (mp_iszero(a) == 1) {
*str++ = '0';
*str = '\0';
return MP_OKAY;
}
if ((res = mp_init_copy (&t, a)) != MP_OKAY) {
return res;
}
/* if it is negative output a - */
if (t.sign == MP_NEG) {
++_s;
*str++ = '-';
t.sign = MP_ZPOS;
}
digs = 0;
while (mp_iszero (&t) == 0) {
if ((res = mp_div_d (&t, (mp_digit) radix, &t, &d)) != MP_OKAY) {
mp_clear (&t);
return res;
}
*str++ = mp_s_rmap[d];
++digs;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -