请各位c高手帮个忙
大家可不理会算法。
程序1完成的是一个数组的加密和解密,是调试用的,能成功;程序2是对文件的加密,程序3是文件的解密,算法和数组的一样,但却不能成功,请各位高手指出
问题所在。
本人认为有以下几大可能:1、文件读写,(用fread、fwrite或其他)。2、数值
处理,在处理文件时,是否考虑char类的处理。(比如在程序3中s=(ch-10*y)/z
改为 s=(ch-10*y)/z+1能部分解密。
希望大家有一点头绪都我告诉我,email:gdlc@163.net。千万个thanks!
程序1:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
#include<math.h>
main()
{
float x0=1,y0=1,z0=1;
float a=10,b=28,c=2.666;
float x,y,z;
float T=0.00001;
static float g[13]={100,200,300,400,500,600,700,800,900,1000,1100,1200,1300};
float s[13];
float f[13];
int n;
for(n=0;n<13;n++)
{x=x0+T*a*y0-T*a*x0+T*z0*g[n];
y=y0+T*b*x0-T*x0*z0-T*y0;
z=z0+T*x0*y0-T*c*z0;
s[n]=10*y+g[n]*z;
x0=x;
y0=y;
z0=z;
}
x0=1;
y0=1;
z0=1;
for(n=0;n<13;n++)
{x=x0+T*s[n]-T*a*x0;
y=y0+T*b*x0-T*x0*z0-T*y0;
z=z0+T*x0*y0-T*c*z0;
f[n]=(s[n]-10*y)/z;
x0=x;
y0=y;
z0=z;
}
for(n=0;n<13;n++)
{if (n%5==0) printf("\n");
printf("%15f",s[n]);}
for(n=0;n<13;n++)
{if (n%5==0) printf("\n");
printf("%13f",f[n]);}
}
程序2:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
#include<math.h>
main()
{
FILE *fp1,*fp2;
char in_fname[30];/*输入要加密的文件名*/
char out_fname[30];/*输入加密后的文件名*/
float ch;
float x0=1,y0=1,z0=1;
float a=10,b=28,c=2.666;
float x,y,z,s;
float T=0.00001;
printf("\n请输入要加密的文件名:\n");
gets(in_fname);/*得到要加密的文件名*/
printf("请输入加密后的文件名:\n");
gets(out_fname);/*得到加密后要的文件名*/
fp1=fopen(in_fname,"rb");
if(fp1==NULL)
{
printf("cannot open in-file.\n");
exit(1);/*如果不能打开要加密的文件,便退出程序*/
}
fp2=fopen(out_fname,"wb");
if(fp2==NULL)
{
printf("cannot open or create out-file.\n");
exit(1);/*如果不能建立加密后的文件,便退出*/
}
/*加密算法开始*/
while(!feof(fp1))
{
ch=fgetc(fp1);
x=x0+T*a*y0-T*a*x0+ch*z0*T;
y=y0+T*b*x0-T*x0*z0-T*y0;
z=z0+T*x0*y0-T*c*z0;
s=10*y+ch*z;
fputc(s,fp2);
x0=x;
y0=y;
z0=z;
}
fclose(fp1);
fclose(fp2);
}
程序3:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
#include<math.h>
main()
{
FILE *fp1,*fp2;
char in_fname[30];/*输入要解密的文件名*/
char out_fname[30];/*输入解密后的文件名*/
float *in,*out;
float ch,s,v;
float x0=1,y0=1,z0=1;
float a=10,b=28,c=2.666;
float x,y,z;
float T=0.00001;
printf("\n请输入要解密的文件名:\n");
gets(in_fname);/*得到要解密的文件名*/
printf("请输入加密后的文件名:\n");
gets(out_fname);/*得到解密后要的文件名*/
fp1=fopen(in_fname,"rb");
if(fp1==NULL)
{
printf("cannot open in-file.\n");
exit(1);/*如果不能打开要解密的文件,便退出程序*/
}
fp2=fopen(out_fname,"wb");
if(fp2==NULL)
{
printf("cannot open or create out-file.\n");
exit(1);/*如果不能建立解密后的文件,便退出*/
}
/*解密算法开始*/
while(!feof(fp1))
{
ch=fgetc(fp1);
x=x0+T*ch-T*a*x0;
y=y0+T*b*x0-T*x0*z0-T*y0;
z=z0+T*x0*y0-T*c*z0;
s=(ch-10*y)/z;/*改为 s=(ch-10*y)/z+1能部分解密。*/
fputc(s,fp2);
x0=x;
y0=y;
z0=z;
}
fclose(fp1);
fclose(fp2);
}