当条件改变时,char *给出不同的结果

时间:2019-01-26 19:33:06

标签: c char md5

我正在使用C创建一个简单的类似于md5的蛮力程序。我唯一的问题是,如果我要替换if语句的一部分,则TextInput的输出会完全改变。

declare @t table(id int, doc nvarchar(max))

insert into @t(id,doc) values
(1,'
{
 "tags":[
{"name":"blue dragon", "weight":"0.80"},
{"name":"Game", "weight":"0.90"}
]
}'),(2,'
{
 "tags":[
{"name":"Green dragon", "weight":"0.70"},
{"name":"fantasy", "weight":"0.80"}
]
}')

select  t.id, dv.[key], dv.value
from @t t
cross apply openjson(doc,'$.tags') as d
cross apply openjson(d.value) dv
where dv.value like '%dragon%'

每次编译和执行时,输出的第一行通常类似于Found String:

但是,如果我将#include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdlib.h> #include <assert.h> #include <time.h> #include <stdbool.h> #if defined(__APPLE__) # define COMMON_DIGEST_FOR_OPENSSL # include <CommonCrypto/CommonDigest.h> # define SHA1 CC_SHA1 #else # include <openssl/md5.h> #endif char *str2md5(const char *str, int length) { int n; MD5_CTX c; unsigned char digest[16]; char *out = (char*)malloc(33); MD5_Init(&c); while (length > 0) { if (length > 512) { MD5_Update(&c, str, 512); } else { MD5_Update(&c, str, length); } length -= 512; str += 512; } MD5_Final(digest, &c); for (n = 0; n < 16; ++n) { snprintf(&(out[n*2]), 16*2, "%02x", (unsigned int)digest[n]); } return out; } typedef struct md5data { char* output; char* strin; } md5data; md5data getrand() { for (int i = 0; i < 10; ++i) { rand(); srand(rand()); } unsigned char strin[50]; for (int i = 0; i < 50; i++) { strin[i] = (rand()%94)+32; } strin[49] = '\0'; char* string = &strin; char *output = str2md5(string, strlen(string)); md5data out; out.output = output; out.strin = string; return out; } bool starts_with(const char* a, const char* b) { if(strncmp(a, b, strlen(b)) == 0) return 1; return 0; } int main() { char input; printf("%s","Enter Search String: "); scanf("%s",&input); srand(time(NULL)); while(1 == 1) { md5data md5 = getrand(); if(starts_with(md5.output,&input)) { printf("Found String: %s\nMD5: %s\n",md5.strin,md5.output); break; } } return 0; } 更改为Found String: 0????之类的内容,则输出类似于starts_with(md5.output,&input)

是什么导致输出变化以及为什么发生?

2 个答案:

答案 0 :(得分:1)

问题出在网上:

char input; printf("%s","Enter Search String: "); scanf("%s",&input);

input应该是一个char数组(一个缓冲区),而不是一个char。 例如:

char input[256]; printf("%s","Enter Search String: "); scanf("%s",&input);

在当前状态下,scanf导致堆栈上的缓冲区溢出,从而导致未定义的结果。

答案 1 :(得分:0)

最后我通过添加全局变量,删除md5data并使用strcpy将值形式的getrand复制到全局变量中来解决了此问题。 代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <time.h>
#include <stdbool.h>
#if defined(__APPLE__)
#  define COMMON_DIGEST_FOR_OPENSSL
#  include <CommonCrypto/CommonDigest.h>
#  define SHA1 CC_SHA1
#else
#  include <openssl/md5.h>
#endif
char *str2md5(const char *str, int length) {
    int n;
    MD5_CTX c;
    unsigned char digest[16];
    char *out = (char*)malloc(33);

    MD5_Init(&c);

    while (length > 0) {
        if (length > 512) {
            MD5_Update(&c, str, 512);
        } else {
            MD5_Update(&c, str, length);
        }
        length -= 512;
        str += 512;
    }

    MD5_Final(digest, &c);

    for (n = 0; n < 16; ++n) {
        snprintf(&(out[n*2]), 16*2, "%02x", (unsigned int)digest[n]);
    }

    return out;
}
unsigned char stringglobal[50];
char* outputglobal;
void getrand() {
        for (int i = 0; i < 10; ++i)
        {
            rand();
            srand(rand());
        }
        unsigned char strin[50] = {0};
        for (int i = 0; i < 50; i++)
        {
        strin[i] = (rand()%94)+32;
        }
        strin[49] = '\0';
        char* string = &strin[0];
        char *output = str2md5(string, strlen(string));
        outputglobal = output;
        strcpy(stringglobal,string);
    }
bool starts_with(const char* a, const char* b)
{
   if(strncmp(a, b, strlen(b)) == 0) return 1;
   return 0;
}
int main() {
    char input[256];
    printf("%s","Enter Search String: ");
    scanf("%s",&input);
    srand(time(NULL));
    while(1 == 1) {
        getrand();
        if(starts_with(outputglobal,&input)) {
            printf("Found! String: %s\nMD5: %s\n",stringglobal,outputglobal);
            break;
        }
    }
    return 0;
}