如何修复代码中的错误以加密文本文件?

How to fix error in code to make encryption of a text file?

我正在编写一个程序,它将文本文件的加密版本 (input.txt) 写入输出文件 (out.txt)。加密对输入文件和包含 (keys.txt) 两个密钥的文件使用按位异或运算,这些密钥之间有换行符。一切似乎都正常,除了在我的加密输出结束时我得到了一系列不可读的字符。

Input.txt:

"Will you walk a little faster?"一条鳕鱼对一只蜗牛说, “我们身后有一只海豚,他踩着我的脚 尾巴。 看看龙虾和海龟是多么急切地前进! 他们正在等待 shingle-will 你的加入 跳舞?

Keys.txt:

!

下面的代码包含两个打印语句,用于测试代码是否正确获取文本文件。我注意到密钥被很好地抓住了,但是 input.txt(str) 并没有抓住整个消息。有人能帮我吗?这是什么原因??

注意

我的错误输出是这样的:

ov$M!4N8:@!Jm@mM$U9M(+@>U(SrmR,H),:I$U$O*9Nm@mR#@$Ma+mou%D?DjRm@ mQ"S=N$R(.M"R(/D%H#EmT> m@#EmI(>9S(@)H#FmN# Xm+mmmmU,H!Gmr(DmI"VmD,F(S!XmU%DmM"C>U(S>,O)9I(9T?U!D>,M!,E ;@#B(

正确的输出应该是:

m@#EmI(>9S(@)H#FmN# XGmmmmU,H!Gmr(DmI"VmD,F(S!XmU%DmM"C>U(S>,O)9I(9T?U!D>,M !,E;@#B(Gmu%D4,S(:@$U$O*"OmU%DmR%H#F!D`V$M!4N8.N Dm@#EmK"H#9I(+mmmm)@#B(f

转十六进制的程序(xxdmyProgram.c)用来说明。

用户应键入:

gcc myProgram.c

./a.out e input.txt keys.txt

('e'代表加密)

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main(int args, char *argc[]){
  int i=0;
  int j=0;
  int len=0;
  char str[501];
  char str2[2];
  char c;
  FILE *finp;
  FILE *keyFile;
  FILE *fout;

  if ( strcmp(argc[1], "e") == 0 )
  {
    if ( (finp = fopen(argc[2],"r")) == NULL )
    {
      printf("Could Not Open file %s\n", argc[2]);
      exit(1);
    }

    if ( (keyFile = fopen(argc[3],"r")) == NULL )
    {
      printf("Could Not Open file %s\n", argc[3]);
      exit(1);
    }

    while((c = fgetc(finp))!=EOF)
    {
      str[j++] = c;
    }

    while((c = fgetc(keyFile)) != EOF)
    {
      str2[i++] = c;    
    }

    /*Print Statement to test the keys*/
    //  printf("%c%c", str2[0], str2[2]);
    /*Print Statement to test the input*/
    //  printf("%s\n", str);

    /* *** START CODE THAT USES INPUT.TXT FILE and KEYS.TXT *** */

    len = strlen(str);
    for(i=0;i<len;i++)
    {
      str[i]^=str2[2];
      str[++i]^=str2[0];
    }

    fout=fopen("out.txt","w");
    if(fout==NULL)
    {
      printf("ERROR");
      exit(1);
    }

    fprintf(fout, "%s", str);
    fclose(finp);
    return 0;
  } else {
    printf("SORRY!");
  }
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *args[]){
    FILE *finp, *keyFile, *fout;
    int i, ch;
    char key[2];

    if(argc == 4 && strcmp(args[1], "e") == 0){
        //Read two key character
        if ((keyFile = fopen(args[3], "r")) == NULL){
            printf("Could Not Open file %s\n", args[3]);
            exit(1);
        }
        if(1!=fscanf(keyFile, " %c", &key[0]) || 1!=fscanf(keyFile, " %c", &key[1])){
            printf("Could Not Read Key properly.\n");
            exit(1);
        }
        fclose(keyFile);

        if((finp = fopen(args[2], "rb")) == NULL){
            printf("Could Not Open file %s\n", args[2]);
            exit(1);
        }
        if((fout = fopen("out.dat", "wb")) == NULL){
            printf("Could Not Open file out.dat\n");
            exit(1);
        }

        i = 1;
        while((ch = fgetc(finp))!=EOF){
            fputc(ch ^ key[i], fout);
            i = !i;
        }
        fclose(fout);
        fclose(finp);
    } else {
        printf("SORRY!\n");
    }

    return 0;
}