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

时间:2017-03-15 19:08:21

标签: c debugging byte

美好的一天!我有这个软件加密和解密文件。我不熟悉实时操作系统,因为我通常编程微控制器,我在不久前编写了Operatins系统的基本内容,包括这个项目。 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)) ===|

1 个答案:

答案 0 :(得分:2)

首先,让我们解决警告。

有几个警告是使用未初始化的变量。其中每个都涉及将char *传递给scanf%s的{​​{1}}格式说明符需要指向字符数组的指针。你传入的是一个指向任何地方的指针。取消引用未初始化的指针会调用undefined behavior,在这种情况下会显示为崩溃。

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

整数/指针比较错误是因为您使用%s的方式。这个变量是process,期望指向一个以空字符结尾的字符串,但是你要将它与一个没有意义的整数进行比较。

相反,请使用char *功能将其与strcmp"encryption"进行比较。

&#34;赋值在没有强制转换的情况下从指针生成整数&#34;是因为您尝试将"decryption" key添加到char *,这是Byte。您可能希望将char编入索引以从中获取特定字符,即key。您还需要一个单独的计数器用于当前密钥索引,并且当它变得大于或等于密钥长度时,您希望将其重置为0.

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

您需要将EOFByte的类型更改为newByte,因为int返回的是fgetcEOF值通常超出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);
}