c - 从文本文件扫描

标签 c encryption

对于我的解密程序,我必须能够从文本文件中扫描替换表,该文本文件在最底部的替换表之后有一个加密文本。我必须使用该替换表来解密消息。所以现在我认为最好的方法是首先扫描替换表并将两列存储到 2 个不同的数组中,然后在最后扫描加密的消息。但是,我不知道该怎么做。

我知道替换表无论如何都不会超过 62 行 (26+26+10)。但是,加密消息可以有任意多行。

加密的文本文件 (projectinput.txt) 大致如下所示:

a m
b n
  etc...
z q
A P
B O
  etc...
Z X
0 5
1 4
  etc...
9 0
Ekrmov 04320
SDFVSDFV

当前解密代码:

#include <stdio.h>

char input[256];
char encrypted[256];
char line[100];
int c;
int j;
int main(){
FILE *file1=fopen("projectoutput.txt", "r");
FILE *file2=fopen("projectinput.txt", "w");

    while(fgets(line,100,file1)!=NULL){
    sscanf(line, "%c %c", input, encrypted);
    fprintf(file2, "%c %c\n", input[j], encrypted[j]);
    }


        while((c=fgetc(file1))!=EOF){
            int i=0;
            while(encrypted[i]!=c){
                i++;
            }

            fputc(input[i], file2);

        }

return 0;

}

我刚刚更新了我的代码,我的包含 fgets 的 while 循环能够将替换表存储到 2 个数组中,但是,它还在底部存储了加密消息。我想阻止 while 循环读取超过 62 行,我应该怎么做?

最佳答案

for(i = 0; i < 62: i++){
    input[i] = fgetc(file1);
    fgetc(file1);
    encrypted[i] = fgetc(file1);
    fgetc(file1);
}

编辑:试试这个

char *buff;
char *decoded;
long file_Size;

fseek(file1, 0, SEEK_END);
file_Size = ftell(file1);
fseek(file1, 0, SEEK_SET);

buff = malloc(file_Size * sizeof(char));
decoded = malloc((file_Size - 62 * 4) * sizeof(char));

//writing all text file to buff array. It is faster and the array is easier to  
//manipulate
fread(buff, sizeof(unsigned char), file_Size, file1);

j = 0;
for(i = 0; i < 62; i++){
    input[i] = buff[j];
    encrypted[i] = buff[j + 2];
    j += 4;
}

//move encrypted message to decoded array
memmove(decoded, &buff[62 * 4], file_Size - 62 * 4);

//decoding process
for(i = 0; i < file_Size - 62 * 4; i++){
    for(j = 0; j < 62; j++){
        if(decoded[i] == '\n' || decoded[i] == ' '){break;}
        if(decoded[i] == input[j]){decoded[i] = encrypted[j]; break;}
    }
}

decoded写入file2和

free(decoded);
free(buff);

瓦尔特

关于c - 从文本文件扫描,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23090206/

相关文章:

C 段错误字符指针

java - Weblogic 12c - 是否有任何标志来确保 SSL 配置上的密码顺序?

c - 使用 PIC18 从 Hex 到 Dec?

c - 可以在循环的括号内定义循环变量吗?

c++ - 如何以编程方式查找进程的所有文件句柄?

algorithm - 凯撒密码加密算法

grails - Jailspt插件具有grails的加密/解密

c# - Entity Framework - 数据库优先 - 拦截 DbContext

c++ - 堆栈变量已损坏

c - fscanf() fclose() 或读取文件退出并结束程序