#include
#include
#include
#include
FILE *fptr;
FILE *kptr;
FILE *dptr;
int key[1000]={0};
char original[1000],encrypted[1000];
void initial()
{
fptr=fopen("encrypted.txt","wb+");
kptr=fopen("key.txt","wb+");
}
void encryption(char original[],char entercrypted[],int key[])
{
int i,len;
len=strlen(original);
printf("The original text is:");
puts(original);
for(i=0;i
entercrypted[i]=0;
for(i=0;i
printf("%c",fputc(entercrypted[i],fptr));
}
for(i=0;i<1000;i++)
fprintf(kptr,"%d",key[i]);
printf("Encrypted is:%s\n\n",entercrypted);
}
void decryption(char original[],char entercrypted[],int key[])
{
int i,len;
len=strlen(original);
for(i=0;i
original[i]=0;
printf("Original is:%s\n\n",original);
}
int main()
{
int i;
int choice;
initial();
puts("input original message:");
gets(original);
srand(time(NULL));
for(i=0;i<1000;i++)
key[i] = rand()%72;
while(1)
{
printf("If you want to encrypte please enter 1\n");
printf("If you want to decrypte please enter 2\n");
printf("If you want to stop the programm please enter 3\n");
printf("Your choice is:");
scanf("%d",&choice);
switch(choice)
{
case 1:
encryption( original, encrypted, key);
break;
case 2:
decryption( original, encrypted, key);
break;
default: return 0;
}
}
}
已经帮你改好了!但是你的程序有很大的问题,因为程序只有在执行完之后才会在文件文件中显示输入文件中的内容,所以无法再执行过程中入读密文。如果第二次打开后再读入密文的话,那么程序又会随机生成新的密钥,所以解密时我直接读取的entercrypted字符数组。你的加密方式其实并不安全,建议你可以选择des加密,个人使用的话安全性没问题,而且速度非常之快,如果你对安全性有更高的要求话,你可以选择pgp,那是目前安全性最高的加密软件。
你上面的代码,框架有了,但细节问题很多。。。 已你的另一个帖子里回答了,代码供参考:
//
#include
#include
#include
#include
const size_t MAX_KEY_LENGTH = 1000;
int encode(char const *datafile, char const *keyfill);
int decode(char const *datafile, char const *keyfile);
int loadKey(char const *keyfile, int *keys, size_t size);
int saveKey(char const *keyfile, int *keys, size_t size);
int generateKey(int *keys, size_t size);
void safeflush();
int main(int argc, char const *argv[])
{
char datafile[] = "encrypted.txt";
char keyfile[] = "key.txt";
int loop = 1;
char original[MAX_KEY_LENGTH];
int choice;
while(1)
{
printf("a) Encryption, Enter 1\n");
printf("b) Decryption, Enter 2\n");
printf("c) Exit, Enter 3\n");
printf("Your choice is:");
scanf("%d%*c",&choice);
switch(choice)
{
case 1:
{
int ret = encode(datafile, keyfile);
if (ret != 0) printf("error, %d\0", ret);
break;
}
case 2:
{
int ret = decode(datafile, keyfile);
if (ret != 0) printf("error, %d\0", ret);
break;
}
case 3:
loop = 0;
break;
}
if (0 == loop) break;
}
return 0;
}
int generateKey(int *keys, size_t size)
{
char str[]="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz,./;\"'<>?";
size_t str_len = sizeof(str)/sizeof(str[0]);
srand(time(NULL));
for (int i = 0; i < size; ++i)
keys[i] = str[rand() % str_len];
return 0;
}
int loadKey(char const *keyfile, int *keys, size_t size)
{
int i = 0;
FILE *pfile;
int retcode = 0;
pfile = fopen(keyfile, "r");
if (pfile == NULL) return -1;
while ( !feof(pfile) ) {
if (i < size)
fscanf(pfile, "%d ", &keys[i++]);
else
break;
}
fclose(pfile);
return i;
}
int saveKey(char const *keyfile, int *keys, size_t size)
{
FILE *pfile;
pfile = fopen(keyfile, "w");
if (pfile == NULL) return -1;
for(int i = 0; i < size; ++i) {
fprintf(pfile, "%d ", keys[i]);
}
fclose(pfile);
return 0;
}
int encode(char const *datafile, char const *keyfile)
{
char original[MAX_KEY_LENGTH] = {'\0'};
char encrypted[MAX_KEY_LENGTH] = {'\0'};
int keys[MAX_KEY_LENGTH];
FILE *pdatafile, *pkeyfile;
pkeyfile = fopen(keyfile, "w");
if (NULL == pkeyfile) return -1;
fclose(pkeyfile);
if (0 != generateKey(keys, MAX_KEY_LENGTH)) return -2;
if (0 != saveKey(keyfile, keys, MAX_KEY_LENGTH) ) return -3;
pdatafile = fopen(datafile, "w");
if (NULL == pdatafile) return -4;
puts(" input message:");
//scanf("%s",original);
//fgets(original, MAX_KEY_LENGTH, stdin);
gets(original);
for (int i = 0; i < strlen(original); ++i)
encrypted[i] = original[i] + keys[i];
for (int i = 0; i < strlen(original); ++i) {
fputc(encrypted[i], pdatafile);
//fputc(original[i], pdatafile);
fputc(encrypted[i], stdout);
}
printf("\n");
fclose(pdatafile);
return 0;
}
int decode(char const *datafile, char const *keyfile)
{
FILE *pdatafile, *pkeyfile;
int keys[MAX_KEY_LENGTH] = {0};
char original[MAX_KEY_LENGTH] = {'\0'};
char encrypted[MAX_KEY_LENGTH] = {'\0'};
int size;
pkeyfile = fopen(keyfile, "r");
if (NULL == pkeyfile) return -1;
fclose(pkeyfile);
pdatafile = fopen(datafile, "r");
if (NULL == pdatafile) return -2;
fscanf(pdatafile,"%s",encrypted);
fclose(pdatafile);
size = loadKey(keyfile, keys, MAX_KEY_LENGTH);
if (size < 1) return -3;
for (int i = 0; i < strlen(encrypted); ++i) {
original[i] = encrypted[i]-keys[i];
fputc(original[i], stdout);
}
printf("\n");
return 0;
}