【问题标题】:How to fix error in code to make encryption of a text file?如何修复代码中的错误以加密文本文件?
【发布时间】:2015-04-08 15:00:56
【问题描述】:

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

输入.txt:

“你能走快一点吗?”一条鳕鱼对蜗牛说, “我们身后有一只海豚,它踩着我的 尾巴。 看看龙虾和海龟是多么急切地前进! 他们在木瓦上等待——你会来加入吗? 跳舞吗?

Keys.txt:

M

下面的代码包含两个打印语句,用于测试代码是否正确获取文本文件。我注意到键被很好地抓住了,但是 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

转为十六进制的程序(xxd myProgram.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!");
  }
}

【问题讨论】:

  • char str2[2]; .. str[i]^=str2[2]; : str2[2] 无效。你需要char str2[3]; 或跳过空格
  • str 应该有一个终止零。数组可能未初始化
  • char str2[3] 对我来说似乎不正确,因为在代码的顶部: char str2[2];
  • 如果你接受了你的previous question,你为什么还要问同样的问题?
  • 上一个问题与我在这里尝试做的不同

标签: c arrays encryption fgetc


【解决方案1】:
#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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-05
    • 2013-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多