哪位大虾帮我看一下,这个ECC加密程序那里错了,加密后的数据都是乱的
是基于TomMath库的
*/
#include <time.h>
//#include "FlashFRAM\Flash&FRAM.h"
#include "ECC.h"
/*******************************************************************************
* Function Name : Ecc_encipher
* Description : Reads a block of data from the FLASH.
* Input : - QX : Public key X coordinate
- QY : Public key Y coordinate
* - GX : Curve G point X coordinate
* - GY : Curve G point Y coordinate
* - A :Curve parameter A
* - P :Finite field P
* Output : None
* Return : return a state
*******************************************************************************/
char Ecc_encipher(mp_int *qx,mp_int *qy, mp_int *px, mp_int *py,mp_int *a,mp_int *p, char* pchStr, \
char* penStr, uint32_t NumByte)
{
mp_int mx, my;
mp_int c1x, c1y;
mp_int c2x, c2y;
mp_int r;
mp_int tempx, tempy;
bool zero=FALSE;
int i,k,ecce,j=0,cj=0;
int Residue=0,Frequency=0;
char miweny[280];
char miwenx[280];
int enlongtemp=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);
Frequency = NumByte/EN_LONG;
Residue = NumByte%EN_LONG;
enlongtemp=EN_LONG/2;
for(i=0; i<Frequency; i++)
{
// fread(miwenx,1,enlongtemp,fp);//读入字符串
// miwenx[enlongtemp]=char(255);
//
// fread(miweny,1,enlongtemp,fp);//读入字符串
// miweny[enlongtemp]=char(255);
//EN_LONG这个是字节数
for( k=0;k<enlongtemp;k++){
miwenx[k]=penStr[k+enlongtemp*j];
}
miwenx[enlongtemp]=(char)255;
j++;
for( k=0;k<enlongtemp;k++){
miweny[k]=penStr[k+j*enlongtemp];
}
miweny[enlongtemp]=(char)255;
j++;
//将char转化成mp_int型
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);
//保存密文,每次保存5个数据
chmistore(&c1x,pchStr,cj);
cj=+5;
chmistore(&c1y,pchStr,cj);
cj=+5;
chmistore(&c2x,pchStr,cj);
cj=+5;
chmistore(&c2y,pchStr,cj);
cj=+5;
}
//剩余字符处理
if ( Residue > 0)
{
if (Residue <= enlongtemp )
{
// fread(miwenx,1,Residue,fp);//读入字符串
for(ecce=0;ecce<Residue;ecce++){
miwenx[ecce]=penStr[(Frequency*EN_LONG)+ecce];
}
miwenx[Residue]=(char)255;
putin(&mx, miwenx,Residue+1);//文件存入
mp_zero(&my);
}
else
{
// fread(miwenx,1,enlongtemp,fp);//读入字符串
for(ecce=0;ecce<enlongtemp;ecce++){
miwenx[ecce]=penStr[(Frequency*EN_LONG)+ecce];
}
miwenx[enlongtemp]=(char)255;
// fread(miweny,1,Residue-enlongtemp,fp);//读入字符串
for(ecce=0;ecce<(Residue-enlongtemp);ecce++){
miweny[ecce]=penStr[(Frequency*EN_LONG+enlongtemp)+ecce];
}
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,pchStr,cj);
cj=+5;
chmistore(&c1y,pchStr,cj);
cj=+5;
chmistore(&c2x,pchStr,cj);
cj=+5;
chmistore(&c2y,pchStr,cj);
cj=+5;
}
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);
return *pchStr;
}
char Ecc_decipher(mp_int *k, mp_int *a,mp_int *p, char* dbuffer, char* cbuffer, uint32_t NumByte){
int chtem;
mp_int c1x, c1y;
mp_int c2x, c2y;
mp_int tempx, tempy;
mp_int mx, my;
mp_int temp;
mp_int tempzero;
int i,j=0,jj=0;
int kk;
char stemp[BIT_LEN]={0};
bool zero=FALSE;
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_init(&tempzero);
//判断文件是否结束了
while(/*!feof(fp)*/NumByte-j>0)
{
i=0;
while(1)
{
stemp[i]=/*fgetc(fp)*/cbuffer[i+j];
if(i%4==0){
if((int)(stemp[i]&0xFF) == 255 )
goto L1;
}
i++;
}
L1: miwendraw(&c1x, stemp, i);
j=j+i;
i=0;
while(1){
stemp[i]=/*fgetc(fp)*/cbuffer[i+j];
if(i%4==0){
if((int)(stemp[i]&0xFF) == 255 ) goto L2;
}
i++;
}
L2: miwendraw(&c1y, stemp, i);
j=j+i;
i=0;
while(1){
stemp[i]=/*fgetc(fp)*/cbuffer[i+j];
if(i%4==0){
if((int)(stemp[i]&0xFF) == 255 ) goto L3;
}
i++;
}
L3: miwendraw(&c2x, stemp, i);
j=j+i;
i=0;
while(1){
stemp[i]=/*fgetc(fp)*/cbuffer[i+j];
if(i%4==0){
if((int)(stemp[i]&0xFF) == 255 ) goto L4;
}
i++;
}
L4: miwendraw(&c2y, stemp, i);
j=j+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);
chtem=chdraw(&mx,stemp);//从ming中取出字符串
//保存解密结果
for(kk=0;kk<chtem;kk++)
{
// fprintf(fq,"%c",stemp[kk]);
dbuffer[kk+jj]=stemp[kk];
}
chtem=chdraw(&my,stemp);//从ming中取出字符串
jj=jj+kk;
//保存解密结果
for(kk=0;kk<chtem;kk++)
{
// fprintf(fq,"%c",stemp[kk]);
dbuffer[kk+jj]=stemp[kk];
}
jj=jj+kk;
}
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);
return *dbuffer;
}