加密在一台计算机上有效,但在另一台计算机上无效,我从老师那里复制了一些代码

问题描述 投票:-1回答:1
void encryptFile(char infile[FNAMELEN])
{
    FILE *fip;
    FILE *fop;
    char text[TEXTLEN], outfile[FNAMELEN];
    int i, k;
    char encryptionkey[11] = {'s','6','3','G','A','f','F','3','\0'};
    strcat(outfile, "encrypt");
    strcat(outfile, infile);
    printf("\n\nthe output file is: %s\n", outfile);
    printf("\nThe original file name is: %s", infile);
    if (!(fip = fopen(infile, "r")) || !(fop = fopen(outfile, "w")))
    {
        printf("\n\nUnable to open files! \n\n");
        exit(1);
    }
    printf("test 1");
    while ((fgets(text, TEXTLEN, fip) != NULL))
    {
        printf("test 2");
        for(i = 0; text[i]; i++)
        {
            printf("\ntest %c", i + 3);
            text[i] = (text[i] ^ encryptionkey[k]) + 40; /*breaks here */
            printf("test 4");
            k++;
            if (!encryptionkey[k])
            {
                k = 0;
            }
        }
        fprintf(fop, "%s", text);
    }
    fclose(fip);
    fclose(fop);
    if(remove(infile) == 0)
    {
        printf("Unencrypted file deleted");
    }
    else
    {
        printf("Unable to delete file");
    }
}

您可以看到我的测试语句,并且在它中断并退出代码的地方写下来。

text[i] = (text[i] ^ encryptionkey[k]) + 40;

这是我所教的代码。可能就像上次我没有正确声明数组或其他东西,但我无法计算出什么。

c encryption file-handling
1个回答
0
投票

加k = 0;在代码的开头(如user3121023所建议)。另外,您可能要使用strncpy而不是strcat。

© www.soinside.com 2019 - 2024. All rights reserved.