c - 字节到字节加密 "C"程序逻辑错误

标签 c debugging byte

美好的一天!我有这个软件来加密和解密文件。我是实时操作系统的新手,因为我通常对微 Controller 进行编程,前一段时间我正在对操作系统中的基本内容进行编程,包括这个项目。 Codeblocks gnu C 可移植编译器 IDE 告诉我没有错误,只有警告。但是当我执行代码时,它运行到某个点,然后给出程序意外停止并且命令提示符需要关闭的错误。

代码和警告在这里。从命令提示符处我得到:Process returned 255 0xFF。也许变量定义需要超过 8 位。

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

void main ()
{
    char *f1;
    char *f2;

    printf ("Please enter file for encryption\n");
    scanf  ("%s", f1);
    printf ("Please enter the name of the file after encryption\n");
    scanf  ("%s", f2);

    Encrypt(f1, f2);
}

int Encrypt(char * FILENAME, char * NEW_FILENAME)
{                                  /********VARIABLE DEFINITION BLOCK*******/
         printf("Encryption started\n");
         FILE *inFile;                                       //Declare inFile
         FILE *outFile;                                      //Declare outFile
         char *key;
         char *process;

         char Byte;
         char newByte;
         /*int  n;*/

                                    /******USER INPUT BLOCK*******/
         printf ("Please enter 'encryption or decryption'\n");
         scanf  ("%s", process);
         printf ("Please enter the key\n");
         scanf  ("%s", key);
         printf("Opening files\n");

         /*int i=0;*/
                                    /******FILE OPEN BLOCK********/
         inFile = fopen(FILENAME,"rb");
         outFile = fopen(NEW_FILENAME, "w");

                                    /*****MAIN PROGRAM BLOCK******/
         if(inFile == NULL)                     /* check if the input file is empy */
         {
            printf("Error: Can't Open inFile\n");
         }

                                                /* check if the output file is empty */
         if(outFile == NULL)
         {
            printf("Error: Can't open outFile\n");
         }
                                                /* if both files are not empty output "Encrypting" and begin encryption */
         else
         {
                 printf("File Opened, Encrypting\n");
                                                /* encrypting cycle */
                 while(1)
                 {
                         printf(".");                /* loading symbol */

                                                     /* if the current byte is not the "End of file" byte and the process is encryption */
                         if(Byte!=EOF && process == 1)
                         {                          /* Byte = function get char from "inFile" */
                                 Byte = fgetc (inFile);
                         //      printf("%d",Byte); /* the new byte = old byte plus 1 byte from the key */
                                 newByte = Byte + key;
                                                    /* we put the new byte in the new already encrypted file */
                                 fputc(newByte, outFile);

                         }
                                                    /* if the current file is not the "End of file" byte and the process is decryption */
                         if(Byte!=EOF && process == 2)
                         {                          /* we read 1 byte with the function get char from the file recorded in the string (pointer) "inFile */
                                 Byte = fgetc (inFile);
                         //      printf("%d",Byte); /* 1 new byte, equals the old byte plus 1 byte from the key */
                                 newByte = Byte + key;
                                                    /* we put the new byte with the function put char in the string (pointer) in which the output file is */
                                 fputc(newByte, outFile);

                         }
                                                    /* if the two "while conditions" are not true we print "End of file" */
                         else
                         {
                                 printf("End of File\n");
                         }
                    return 1;
                 }
         }
}

构建输出:

||=== Build: Debug in Encryption-software (compiler: GNU GCC Compiler) ===|
C:\Users\arakel-the-dragon\Documents\Encryption-software\main.c|7|warning: return type of 'main' is not 'int' [-Wmain]|
C:\Users\arakel-the-dragon\Documents\Encryption-software\main.c||In function 'main':|
C:\Users\arakel-the-dragon\Documents\Encryption-software\main.c|17|warning: implicit declaration of function 'Encrypt' [-Wimplicit-function-declaration]|
C:\Users\arakel-the-dragon\Documents\Encryption-software\main.c||In function 'Encrypt':|
C:\Users\arakel-the-dragon\Documents\Encryption-software\main.c|65|warning: comparison between pointer and integer|
C:\Users\arakel-the-dragon\Documents\Encryption-software\main.c|69|warning: assignment makes integer from pointer without a cast|
C:\Users\arakel-the-dragon\Documents\Encryption-software\main.c|75|warning: comparison between pointer and integer|
C:\Users\arakel-the-dragon\Documents\Encryption-software\main.c|79|warning: assignment makes integer from pointer without a cast|
C:\Users\arakel-the-dragon\Documents\Encryption-software\main.c|92|warning: control reaches end of non-void function [-Wreturn-type]|
C:\Users\arakel-the-dragon\Documents\Encryption-software\main.c|34|warning: 'process' is used uninitialized in this function [-Wuninitialized]|
C:\Users\arakel-the-dragon\Documents\Encryption-software\main.c|36|warning: 'key' is used uninitialized in this function [-Wuninitialized]|
C:\Users\arakel-the-dragon\Documents\Encryption-software\main.c|65|warning: 'Byte' may be used uninitialized in this function [-Wmaybe-uninitialized]|
C:\Users\arakel-the-dragon\Documents\Encryption-software\main.c||In function 'main':|
C:\Users\arakel-the-dragon\Documents\Encryption-software\main.c|13|warning: 'f1' is used uninitialized in this function [-Wuninitialized]|
C:\Users\arakel-the-dragon\Documents\Encryption-software\main.c|15|warning: 'f2' is used uninitialized in this function [-Wuninitialized]|
||=== Build finished: 0 error(s), 12 warning(s) (0 minute(s), 1 second(s)) ===|

最佳答案

首先,让我们解决这些警告。

一些警告是针对使用未初始化的变量的。其中每一个都涉及将 char * 传递给 scanfscanf%s 格式说明符需要一个指向字符数组的指针。相反,您传入的是一个不指向任何地方的指针。取消引用未初始化的指针会调用 undefined behavior ,在本例中表现为崩溃。

将这些变量声明为 char 数组,并为您期望的输入提供足够的空间。另外,向 %s 添加长度说明符,指定要读取的最大字符数。

整数/指针比较错误是由于您使用process的方式造成的。该变量是一个 char *,预期指向一个以 null 结尾的字符串,但您将它与一个没有意义的整数进行比较。

相反,请使用strcmp函数将其与“加密”“解密”进行比较。

“赋值从指针生成整数而不进行强制转换”是因为您尝试将 key(它是一个 char *)添加到 Byte,这是一个 char。您可能想要索引 key 以从中获取特定字符,即 key[1]。您还需要为当前键索引设置一个单独的计数器,并且当它大于或等于键的长度时,您需要将其重置为 0。

第一次进入 while 循环时,Byte 尚未设置,但您仍然将其与 EOF 进行比较。当您读取下一个字节时,您还会覆盖正在比较的 Byte 值,但随后您不会检查它是否为 EOF

您需要将 BytenewByte 的类型更改为 int,因为这是 fgetc 返回的内容。 EOF 值通常超出 char 的范围,因此除非更改类型,否则您永远找不到它。您还应该将读取和比较作为循环条件的一部分来执行,而不是使用 while(1) 并从循环体中删除读取。

隐式声明警告是因为您在定义或声明它之前调用了Encrypt函数。结果,创建了 int Encrypt() 的隐式声明(即采用未知数量的参数并返回 int 的函数)。这恰好与它的定义方式一致,但仍然不匹配。

要么在 main 之前添加此函数的声明,要么将整个函数移到 main 之前。

除了警告之外,return 1 语句在 while 循环的底部执行,因此循环在退出之前仅执行一次迭代。上述测试条件中 Byte 值的建议应该可以解决这个问题。

您的解密当前与加密的操作相同。您可能想在这里进行减法而不是加法。在这两种情况下,您还需要检查是否有环绕。因此,如果加密值大于255,则减去256。解密时类似,如果值小于0,则加256。

应用这些修复后,您的代码应如下所示:

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

// declare function before it's called
void Encrypt(char * FILENAME, char * NEW_FILENAME);

int main ()
{
    char f1[100];   // array big enough to hold expected input
    char f2[100];   // array big enough to hold expected input

    printf ("Please enter file for encryption\n");
    scanf  ("%99s", f1);    // set max characters to read to prevent buffer overflow
    printf ("Please enter the name of the file after encryption\n");
    scanf  ("%99s", f2);

    Encrypt(f1, f2);
    return 0;
}

void Encrypt(char * FILENAME, char * NEW_FILENAME)
{
         printf("Encryption started\n");
         FILE *inFile;
         FILE *outFile;
         char key[50];         // array big enough to hold expected input
         char process[50];     // array big enough to hold expected input
         int keylen, keyidx;

         int Byte;       // fgetc returns int, so use an int
         int newByte;

         printf ("Please enter 'encryption or decryption'\n");
         scanf  ("%49s", process);
         printf ("Please enter the key\n");
         scanf  ("%49s", key);
         keylen = strlen(key);
         keyidx = 0;    // starting index into key
         printf("Opening files\n");

         inFile = fopen(FILENAME,"rb");
         outFile = fopen(NEW_FILENAME, "w");

         if(inFile == NULL)
         {
            printf("Error: Can't Open inFile\n");
         }
         else if(outFile == NULL)
         {
            printf("Error: Can't open outFile\n");
         }
         else
         {
                 printf("File Opened, Encrypting\n");
                 while((Byte = fgetc(inFile)) != EOF)    // read a byte, check if EOF
                 {
                         if (!strcmp(process,"encryption"))
                         {
                                 newByte = Byte + key[keyidx];    // use key index
                                 if (newByte > 255) newByte -= 256;   // check for overflow
                         }
                         else if (!strcmp(process,"decryption"))
                         {
                                 newByte = Byte - key[keyidx];
                                 if (newByte < 0) newByte += 256;
                         }
                         else
                         {
                            newByte = Byte;
                         }
                         fputc(newByte, outFile);
                         keyidx++;
                         // loop to the start of the key if needed
                         if (keyidx >= keylen) keyidx = 0;
                 }
         }
         if (inFile != null) fclose(inFile);     // close your files when you're done
         if (outFile != null) fclose(outFile);
}

关于c - 字节到字节加密 "C"程序逻辑错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42818653/

相关文章:

c - C语言中n个数相加

c++ - 为什么会有单独的 "trie_node"和 "trie"结构?

java - 为变量分配某个值时中断

python - Python 2.6 和 3 中的字节与字节数组

c++ - 如何将wstring转换为字节 vector

c - 如何使用 SWIG 包装带有可变参数的 C 函数

java - 在 Eclipse IDE 中查看变量内容

node.js - 如何在 Eclipse 中使用 'V8 debugger for node' 设置源映射

Java 字节数组和带有套接字的图像

c - AVL 树插入(在 C 中)失败