c - 文件输入和输出,循环创建新行

标签 c encryption file-io

我的代码有问题。我有 for 循环,可以根据用户输入的 key 解密/加密短语。该程序通过以下方式从命令行运行:

./program [-d] [-e] key sourcefile destinationfile

[-d]和[-e]表示解密或加密,用户只能指定一个选项。该程序从源文件中获取文本短语,对其进行操作,然后将新短语写入新文件。但是,我注意到出于某种原因,我的 for 循环根据 key 拆分了短语并搞砸了新文件中的序列。查看我加密时得到的结果:

key    source                      dest
2      Testing1234567890ABCDEF     Tsig24680BDFetn13579ACE      correct
3      Testing1234567890ABCDEF     Ttg369BEei1470CFsn258AD      correct
4      Testing1234567890ABCDEF     Ti260Den37AEsg47BFt159C      correct
5      Testing1234567890ABCDEF     Tn49Deg50Es16AFt27B          incorrect
                                   i38C
6      Testing1234567890ABCDEF     Tg6Be17Cs28Dt39Ei40Fn5A      correct
7      Testing1234567890ABCDEF     T18Ee29Fs30                  incorrect
                                   t4Ai5Bn6Cg7D
8      Testing1234567890ABCDEF     T20e3As4Bt5Ci6Dn7Eg8F19       correct
9      Testing1234567890ABCDEF     T3be4Cs5Dt6Ei7Fn8
                                   g9102A                       incorrect
10     Testing1234567890ABCDEF     T4De5Es6Ft7
                                   i8n9g01A2B3C                 incorrect

请注意如何使用 5、7、9 和 10 将短语分成两行?那不应该发生,我什至在循环中没有换行符。我不知道是什么原因造成的。

我的代码如下:

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

#define BUFFER_SIZE 100

int main ( int argc, char* argv[ ] ) {
        FILE* sourcefile;
        FILE* destfile;
        int i, j, filesize, key;
        int pos = 0;
        char phrase[ BUFFER_SIZE ];
        char* newPhrase;

        /* if user types -d or -e continue */
        if ( ( argc == 5 && ( strcmp ( argv[ 1 ], "-d") == 0 ) ) || ( argc == 5 && ( strcmp ( argv[ 1 ], "-d") == 0 ) ) ) {

                /* exit if key is not an int */
                key = atoi ( argv[ 2 ] );
                if ( key < 1 ) {
                        perror ( "Error: key is not a valid int" );
                        exit ( EXIT_FAILURE );
                }

                /* open source & destination files */
                sourcefile = fopen ( argv[ 3 ], "r" );
                /* display error message if source file does not exist */
                if ( sourcefile == NULL ) {
                        perror ( "" );
                        exit ( EXIT_FAILURE );
                }
                destfile = fopen ( argv[ 4 ], "w" );

                /* extract the text in the source file */
                fgets ( phrase, BUFFER_SIZE, sourcefile );
                fseek ( sourcefile, 0, SEEK_END );
                filesize = ftell ( sourcefile );

                /* allocate memory for new string that will go in destination file */
                newPhrase = ( char* ) malloc ( filesize );

                /* decrypt */
                if ( strcmp ( argv[ 1 ], "-d" ) == 0 ) {
                        for (i = 0; i < key; i++) {
                                for (j = i; j < strlen( phrase ); j += key)
                                        newPhrase[ j ] = phrase[ pos++ ];
                        }
                }
                /* encrypt */
                else {
                        for (i = 0; i < key; i++) {
                                for (j = i; j < strlen( phrase ); j += key)
                                        newPhrase[ pos++ ] = phrase[ j ];
                        }
                }
                /* write new phrase to file, close files */
                fputs( newPhrase, destfile );
                fclose ( sourcefile );
                fclose ( destfile );
        }
        return 0;
}

最佳答案

您的程序运行良好。

令您感到困惑的是,输入文件中第一行末尾的尾随换行符包含在该短语中。

因为短语(包括尾随换行符)是 24 个字符,并且您使用的是选择“第 n 个”字符的“加密”,所以当您使用除以 24 的“n”时一切正常,因为尾随换行符结束于阶段结束。所以 2、3、4、6 和 8 都可以正常工作(有趣的是你选择了一个长度有这么多除数的相位......)

但对于“n”的其他值,终止输入字符串的尾随换行符在加密字符串的中间结束。一旦您看到这一点,“不正确”的输出就是正确的。

[编辑:]这已经更新;最初我认为是 C 中终止字符串的 0 导致了问题。原来的答案是错误的,抱歉。

如果你想“修复”这个,你可以在阅读后缩短字符串:

while (strlen(phrase) && phrase[strlen(phrase)-1] == '\n') phrase[strlen(phrase)-1] = '\0'

ps 查看您的历史记录,您没有将答案标记为正确。通常情况下,如果有人发布了正确的答案,您点击答案上方和左侧的“勾号”标记,然后回答者(我)就会获得互联网积分,这为我们原本悲伤的生活赋予了意义。

关于c - 文件输入和输出,循环创建新行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20176627/

相关文章:

c - 在C中强制转换为指针类型时会发生什么情况?

node.js - 在 NodeJs 中使用 AWS KMS 解密文本

java - 将对象写入java文件

perl - 这是标准的 Perl 语言构造还是自定义 : open HANDLE, ">$fname"

c - 变量设置不正确

c - printf 中的撇号在 C 中被替换为 Æ

c# - 解码/解密期间 Base-64 字符数组的长度无效

C# AES 加密 - 流模式自动添加 IV

c++ - 如何修复导致程序崩溃的错误

c - 如何获取所有 openssl 或 libcrypto 错误代码的列表