📄 ecc8file.c
字号:
#include <stdio.h>
#include "stdio.h"
//#include <stdin.h>
//#include <stdout.h>
//#include <stderr.h>
#include <math.h>
#include <io.h>
//#include <fstream>
#include <string.h>
#include <time.h>
#include <stdlib.h>
#include <limits.h>
#include <fcntl.h>
#include <sys\stat.h>
unsigned char add(unsigned char add_1,unsigned char add_2);
unsigned char sub(unsigned char sub_1,unsigned char sub_2);
unsigned char mul(unsigned char mul_1,unsigned char mul_2);
unsigned char division(unsigned char div_1,unsigned char div_2);
void addition(unsigned char x1,unsigned char y1,unsigned char x2,unsigned char y2,unsigned char *x3,unsigned char *y3);
void square(unsigned char x1,unsigned char y1,unsigned char *x3,unsigned char *y3);
void proclaimed(unsigned char mw,unsigned char *pmx,unsigned char *pmy,unsigned char *yes);
int oula(unsigned char mwdel);
void pow_p(unsigned char bottom,unsigned char top,unsigned char *result);
void longth(unsigned char pv_key,unsigned char *bit);
void kp(unsigned char pv_key,unsigned char pb_key_x,unsigned char pb_key_y,unsigned char *pv_pb_key_x,unsigned char *pv_pb_key_y);
void encryption(unsigned char mw,unsigned char qbx,unsigned char qby,unsigned char *cx,unsigned char *cy,unsigned char *yes);
void decode(unsigned char cx,unsigned char cy,unsigned char qax,unsigned char qay,unsigned char yes,unsigned char *mx);
void SHA_b_4(unsigned char *pv_key_b);
void SHA_a_4(unsigned char *pv_key_a);
unsigned char p=29;
unsigned char px=1;
unsigned char py=5;
unsigned char a=4;
unsigned char b=20;
unsigned char qbx=0;
unsigned char qby=0;
unsigned char qax=0;
unsigned char qay=0;
unsigned char da=0;
unsigned char db=0;
unsigned char add(unsigned char add_1,unsigned char add_2)
{
unsigned char sum;
sum=add_1+add_2;
sum=sum%p;
return sum;
}
unsigned char sub(unsigned char sub_1,unsigned char sub_2)
{
unsigned char sum;
sum=sub_1-sub_2;
sum=sum%p;
return sum;
}
unsigned char mul(unsigned char mul_1,unsigned char mul_2)
{
unsigned char sum;
sum=mul_1*mul_2;
sum=sum%p;
return sum;
}
unsigned char division(unsigned char div_1,unsigned char div_2)
{
unsigned char sum;
sum=div_1/div_2;
sum=sum%p;
return sum;
}
void addition(unsigned char x1,unsigned char y1,unsigned char x2,unsigned char y2,unsigned char *x3,unsigned char *y3)
{
unsigned char x3_tmp=0;
unsigned char y3_tmp=0;
unsigned char fenzi=0;
unsigned char fenmu=0;
unsigned char kuohao=0;
unsigned char kuohao2=0;
unsigned char xiaokuohao=0;
unsigned char dakuohao=0;
if(x1==x2)
square(x1,y1,&x3_tmp,&y3_tmp); //如果两个数相同,调用倍点
else if(y1==0)
{
x3_tmp=x2;
y3_tmp=y2;
}
else if(y2==0)
{
x3_tmp=x1;
y3_tmp=y1;
}
else
{
fenzi=sub(y2,y1);//fenzi=y2-y1
fenmu=sub(x2,x1);//fenmu=x2-x1
kuohao=division(fenzi,fenmu);//kuohao=(y2-y1)/(x2-x1)
kuohao2=mul(kuohao,kuohao);//kuohao2=kuohao^2
x3_tmp=sub(kuohao2,x1);//x3
x3_tmp=sub(x3_tmp,x2);//x3
xiaokuohao=sub(x1,x3_tmp);//xiaokuohao=x1-x3
dakuohao=mul(kuohao,xiaokuohao);//dakuohao=(y2-y1)/(x2-x1)*(x1-x3)
y3_tmp=sub(dakuohao,y1);//y3
}
*x3=x3_tmp;
*y3=y3_tmp;
}
void square(unsigned char x1,unsigned char y1,unsigned char *x3,unsigned char *y3)
{
unsigned char x3_tmp=0;
unsigned char y3_tmp=0;
unsigned char fenzi=0;
unsigned char fenmu=0;
unsigned char fenmuni=0;
unsigned char kuohao=0;
unsigned char kuohao2=0;
unsigned char xiaokuohao=0;
unsigned char dakuohao=0;
unsigned char h2=2;
unsigned char h3=3;
if(y1==0)
{
x3_tmp=x1;
y3_tmp=y1;
}
else
{
fenzi=mul(x1,x1);//fenzi=x1^2
fenzi=mul(fenzi,h3);//fenzi=3*x1^2
fenzi=add(fenzi,a);//fenzi=3*x1^2+a
fenmu=mul(y1,h2);//fenmu=2*y1
kuohao=division(fenzi,fenmu);//kuohao=(3*x1^2+a)/(2*y1)
kuohao2=mul(kuohao,kuohao);//kuohao2=kuohao^2
x3_tmp=sub(kuohao2,x1);
x3_tmp=sub(x3_tmp,x1);//x3
xiaokuohao=sub(x1,x3_tmp);//xiaokuohao=x1-x3
dakuohao=mul(kuohao,xiaokuohao);//dakuohao=(3*x1^2+a)/(2*y1)*(x1-x3)
y3_tmp=sub(dakuohao,y1);//y3
}
*x3=x3_tmp;
*y3=y3_tmp;
}
void proclaimed(unsigned char mw,unsigned char *pmx,unsigned char *pmy,unsigned char *yes)
{
unsigned char h1=1;
unsigned char h20=20;
unsigned char x3=0;
unsigned char xa=0;
unsigned char bufferyes=0;
int t=0,j;
x3=mul(mw,mw);
x3=mul(x3,mw);
xa=mul(mw,a);
x3=add(x3,xa);
x3=add(x3,b);//x3=x^3+4x+20
t=oula(x3);
if(t==1) //可以嵌入
{
*pmx=mw;
*pmy=(unsigned char)sqrt(x3);
}
else
{
mw=mul(mw,h20);
mw=sub(mw,h1);
for(j=0;j<=19;j++)
{
mw=add(mw,h1);
x3=mul(mw,mw);
x3=mul(x3,mw);
xa=mul(mw,a);
x3=add(x3,xa);
x3=add(x3,b);//x3=x^3+4x+20
t=oula(x3);
if(t==1)
{
bufferyes=1;
break;
}
}
*pmx=mw;
*pmy=(unsigned char)sqrt(x3);
}
*yes=bufferyes;
}
int oula(unsigned char mwdel)
{
unsigned char p1=0;
unsigned char p1_2=0;
unsigned char result=0;
unsigned char result_p=0;
unsigned char h1=1;
unsigned char h2=2;
p1=sub(p,h1);
p1_2=division(p1,h2);//(p-1)/2
pow_p(mwdel,p1_2,&result);
if(result==1)
return 1;
else
return 0;
}
void pow_p(unsigned char bottom,unsigned char top,unsigned char *result)
{
unsigned char bit_pro[4]={0};
unsigned char mul_tmp=0;
int j;
longth(top,bit_pro); //l??????
if(bit_pro[3]==1)
mul_tmp=bottom;
else
mul_tmp=1;
for(j=2;j>=0;j--)
{
mul_tmp=mul(mul_tmp,mul_tmp);
if(bit_pro[j]==1)
mul_tmp=mul(mul_tmp,bottom);
}
*result=mul_tmp;
}
void longth(unsigned char pv_key,unsigned char *bit)
{
unsigned char bit_pro[4]={0};
unsigned char h1=1;
unsigned char result=0;
int i;
for(i=0;i<=3;i++)
{
result=pv_key&h1;
if(result==1)
bit_pro[i]=1;
pv_key=pv_key>>1;
}
//bit=bit_pro;
for(i=0;bit_pro[i]!='\0';i++)
{
bit[i]=bit_pro[i];
}
}
void kp(unsigned char pv_key,unsigned char pb_key_x,unsigned char pb_key_y,unsigned char *pv_pb_key_x,unsigned char *pv_pb_key_y)
{
int i;
unsigned char bit_pro[4]={0};
unsigned char buf_Q_x=1;
unsigned char buf_Q_y=0;
longth(pv_key,bit_pro);
for(i=0;i<=3;i++)
{
if(bit_pro[i]==1)
addition(buf_Q_x,buf_Q_y,pb_key_x,pb_key_y,&buf_Q_x,&buf_Q_y);
square(pb_key_x,pb_key_y,&pb_key_x,&pb_key_y);
}
*pv_pb_key_x=buf_Q_x;
*pv_pb_key_y=buf_Q_y;
}
void encryption(unsigned char mw,unsigned char qbx,unsigned char qby,unsigned char *cx,unsigned char *cy,unsigned char *yes)
{
unsigned char pmx=0;
unsigned char pmy=0;
unsigned char daqbx=0;
unsigned char daqby=0;
unsigned char bufferyes=0;
unsigned char cx_tmp=0;
unsigned char cy_tmp=0;
proclaimed(mw,&pmx,&pmy,&bufferyes);
*yes=bufferyes;
kp(da,qbx,qby,&daqbx,&daqby);
addition(daqbx,daqby,pmx,pmy,&cx_tmp,&cy_tmp);
*cx=cx_tmp;
*cy=cy_tmp;
}
void decode(unsigned char cx,unsigned char cy,unsigned char qax,unsigned char qay,unsigned char yes,unsigned char *mx)
{
unsigned char dbqax=0;
unsigned char dbqay=0;
unsigned char mx_tmp=0;
unsigned char my=0;
kp(db,qax,qay,&dbqax,&dbqay);
dbqay=sub(p,dbqay);
addition(cx,cy,dbqax,dbqay,&mx_tmp,&my);
*mx=mx_tmp;
}
void SHA_a_4(unsigned char *pv_key_a)
{
unsigned char pv_pro=0;
srand((unsigned)time(NULL)+1);
pv_pro= rand() ;
pv_pro=pv_pro&15;
*pv_key_a=pv_pro;
}
void SHA_b_4(unsigned char *pv_key_b)
{
unsigned char pv_pro=0;
srand((unsigned)time(NULL));
pv_pro= rand() ;
pv_pro=pv_pro&15;
*pv_key_b=pv_pro;
}
void main(void)
{
clock_t start,end,middle;
FILE *fp1,*fp2,*fp3;
int file_handle_1;
int file_handle_2;
long file_size_1;
long file_size_2;
unsigned char ch_read=0;
unsigned char ch_write=0;
unsigned char ch_low=0;
unsigned char ch_high=0;
unsigned char cx_low=0;
unsigned char cy_low=0;
unsigned char cx_high=0;
unsigned char cy_high=0;
unsigned char bufferyes_low=0;
unsigned char bufferyes_high=0;
unsigned char ch_low_fp3=0;
unsigned char ch_high_fp3=0;
unsigned char cx_low_fp3=0;
unsigned char cy_low_fp3=0;
unsigned char cx_high_fp3=0;
unsigned char cy_high_fp3=0;
unsigned char bufferyes_low_fp3=0;
unsigned char bufferyes_high_fp3=0;
unsigned char mx_high_fp3=0;
unsigned char mx_low_fp3=0;
int j;
unsigned char sp={0x0};
start=clock();
SHA_a_4(&da);
kp(da,px,py,&qax,&qay);
SHA_b_4(&db);
kp(db,px,py,&qbx,&qby);
if((file_handle_1=open("D:\\ceshi\\ceshi.doc",O_RDONLY))==-1)
printf("error open 1");
else
{
file_size_1=filelength(file_handle_1);
printf("the file_size_1 in bytes is %ld\n",file_size_1);
close(file_handle_1);
}
fp1=fopen("D:\\ceshi\\ceshi.doc","rb");
fp2=fopen("D:\\ceshi\\jiami.doc","wb");
if(fp1!=NULL)
{
for(j=0;j<file_size_1;j++)
{
ch_read=fgetc(fp1);
ch_low=ch_read&15;
ch_high=ch_read>>4;
ch_high=ch_high&15;
encryption(ch_low,qbx,qby,&cx_low,&cy_low,&bufferyes_low);
fputc(cx_low,fp2);
fputc(cy_low,fp2);
fputc(bufferyes_low,fp2);
encryption(ch_high,qbx,qby,&cx_high,&cy_high,&bufferyes_high);
fputc(cx_high,fp2);
fputc(cy_high,fp2);
fputc(bufferyes_high,fp2);
ch_read=sp;
ch_low=sp;
ch_high=sp;
bufferyes_low=sp;
bufferyes_high=sp;
cx_low=sp;
cx_high=sp;
cy_low=sp;
cy_high=sp;
}
}
else
printf("err1");
fclose(fp1);
fclose(fp2);
middle=clock();
if((file_handle_2=open("D:\\ceshi\\jiami.doc",O_RDONLY))==-1)
printf("error open 2");
else
{
file_size_2=filelength(file_handle_2);
printf("the file_size_2 in bytes is %ld\n",file_size_2);
close(file_handle_2);
}
fp2=fopen("D:\\ceshi\\jiami.doc","rb");
fp3=fopen("D:\\ceshi\\jiemi.doc","wb");
if(fp2!=NULL)
{
for(j=0;j<(file_size_2/6);j++)
{
cx_low_fp3=fgetc(fp2);
cy_low_fp3=fgetc(fp2);
bufferyes_low_fp3=fgetc(fp2);
decode(cx_low_fp3,cy_low_fp3,qax,qay,bufferyes_low_fp3,&mx_low_fp3);
cx_high_fp3=fgetc(fp2);
cy_high_fp3=fgetc(fp2);
bufferyes_high_fp3=fgetc(fp2);
decode(cx_high_fp3,cy_high_fp3,qax,qay,bufferyes_high_fp3,&mx_high_fp3);
mx_high_fp3=mx_high_fp3<<4;
ch_write=(mx_high_fp3)^(mx_low_fp3);
fputc(ch_write,fp3);
cx_low_fp3=sp;
cy_low_fp3=sp;
bufferyes_low_fp3=sp;
bufferyes_high_fp3=sp;
cx_high_fp3=sp;
cy_high_fp3=sp;
ch_write=sp;
mx_high_fp3=sp;
}
}
else
printf("err2");
fclose(fp2);
fclose(fp3);
end=clock();
printf("总体时间: %f\n",(float)(end-start)/CLK_TCK); //???????
printf("加密时间: %f\n",(float)(middle-start)/CLK_TCK); //???????
printf("解密时间: %f\n",(float)(end-middle)/CLK_TCK);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -