从 char 函数中删除 printf 后的 C : Program prints weird stuff in output,

标签 c encryption

我正在用 c 语言编写一个用于 vignere 密码的程序,当在一个函数中生成与输入名称长度相同的 key 时,我遇到了一个错误,如果我删除显示输入字符串长度的“printf”行, ,在屏幕上打印奇怪的东西,只有当我从 GenKey() 函数中删除“printf”行时才会发生这种情况。

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

char *GenKey(char *key, char *source){
    int i=0,j=0;
    char ReturnKey[strlen(source)];

    printf("%d\n",strlen(source));      // THIS LINE HERE CAUSES PROBLEM

    for(i=0;i<strlen(source)-1;i++){
        if(j==strlen(key)){
            j=0;
        }
        ReturnKey[i]=key[j];
        j++;
        }
        return ReturnKey;
}

int main()
{
    int i;
    char name[10000];
    char container[10000];
    char VigKey[]="INFERNO";
    char *NamePtr;
    char *KeyPtr;
    printf("give a name: ");
    fgets(name,10000,stdin);

    char GeneratedKey[strlen(name)];
    KeyPtr=VigKey;
    NamePtr=name;
    strcpy(GeneratedKey,GenKey(KeyPtr,NamePtr));
    printf("%s",GeneratedKey);
}

输出(删除该行之前):

give a name: ATTACKATDAWN
13
INFERNOINFER

现在我删除该行

char *GenKey(char *key, char *source){
    int i=0,j=0;
    char ReturnKey[strlen(source)];

   // NOW I HAVE DELETED THAT LINE

    for(i=0;i<strlen(source)-1;i++){
        if(j==strlen(key)){
            j=0;
        }
        ReturnKey[i]=key[j];
        j++;
        }
        return ReturnKey;
}

输出(删除该行后):

give a name: ATTACKATDAWN
INFERNOINFERα╫`

最佳答案

尝试使用 malloc 在堆上创建 ReturnKey 字符数组,如下所示:

char *GenKey(char *key, char *source){
    int i=0,j=0;
    char *ReturnKey = malloc(sizeof(char) * strlen(source));
    for(i=0;i<strlen(source)-1;i++){
        if(j==strlen(key)){
            j=0;
        }
        ReturnKey[i]=key[j];
        j++;
    }
    return ReturnKey;
}

在创建 ReturnKey 之前,您是将其作为仅存在于该函数上下文中的局部变量。即使您会看到您的单词仍然在那里,那只是因为它仍然位于内存中的那个位置,但不再被对象引用。

当您使用 malloc 这样的函数创建动态数组时,您是在所谓的“堆”上创建它,当它超出范围时,它不会被释放,这样您就可以从函数返回它(您实际上返回一个指向内存中该位置的指针)。

使用 malloc 时请注意,内存未释放,因此您必须在稍后的某个时刻通过调用 free 自行释放它,否则会泄漏内存。

完整的代码可能是这样的:

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

char *GenKey(char *key, char *source){
    int i=0, j=0;

    // malloc takes the size of the data type * the length
    char *ReturnKey = malloc(sizeof(char) * strlen(source));

    for(i=0;i<strlen(source)-1;i++){
        if(j==strlen(key)){
            j=0;
        }
        ReturnKey[i]=key[j];
        j++;
    }
    return ReturnKey;
}

int main()
{
    int i;
    char name[10000];
    char container[10000];
    char VigKey[]="INFERNO";
    char *NamePtr;
    char *KeyPtr;
    printf("give a name: ");
    fgets(name,10000,stdin);

    KeyPtr=VigKey;
    NamePtr=name;
    char *GeneratedKey = GenKey(KeyPtr,NamePtr);
    printf("%s",GeneratedKey);
    free(GeneratedKey);  // IMPORTANT!!!
}

这是一篇关于使用 malloc 和 free 的更深入的文章:

https://www.codingunit.com/c-tutorial-the-functions-malloc-and-free

关于从 char 函数中删除 printf 后的 C : Program prints weird stuff in output,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46499559/

相关文章:

使用 CBC 和 PKCS7Padding 的 Java AES 加密

java - 我注意到 log4j2.xml 中某些属性前面的前缀 'sd:'——它们代表什么?

C 在函数内部重新分配

c - 在辅助函数上用字符串填充数组。为什么它会打印乱码?

c - 理解 extern 和 void 函数指针

c - “noreturn”函数确实返回

java - 如何使用 AES/CBC/PKCS5Padding 生成 AES key 进行加密和解密

PHP - 加密其他站点的用户名和密码

security - 在开发过程中防止源代码被盗

c - C语言中Printf和Strcpy的Bug