C 实现openssl evp加密

C 实现openssl evp加密,c,encryption,openssl,evp-cipher,C,Encryption,Openssl,Evp Cipher,我目前正在使用C中的EVP对brute强制执行一些简单文本进行一些测试 提供了明文、密文、密钥大小、加密方法和方法。我们只需要尝试不同的键,这是一个已知的字典单词 主要问题是,如果我尝试直接从MAIN调用EVP方法,如下所示: int main(int argc, char const *argv[]) { evp((unsigned char *)"This is a top secret."); return 0; } 找不到密钥(无法生成相同的密码) 但是调用clock(

我目前正在使用C中的EVP对brute强制执行一些简单文本进行一些测试

提供了明文、密文、密钥大小、加密方法和方法。我们只需要尝试不同的键,这是一个已知的字典单词

主要问题是,如果我尝试直接从MAIN调用EVP方法,如下所示:

int main(int argc, char const *argv[]) {
    evp((unsigned char *)"This is a top secret.");
    return 0;
}
找不到密钥(无法生成相同的密码)

但是调用
clock()调用:

int main(int argc, char const *argv[])
{
    clock_t begin = clock();
    evp((unsigned char *)"This is a top secret.");
    return 0;
}
很好用!为什么会这样?我做错了什么?有人能解释一下问题是什么吗

加密使用aes-128-cbc对明文进行加密,而iv为16字节NULL,密钥是长度小于16的单个字典单词,当长度小于16时,将以空空格馈送

以下代码用于执行整个过程:

void evp(unsigned char * plaintext) {
    FILE *fp = fopen("words.txt", "r");
    const char cipher[]  ="8d20e5056a8d24d0462ce74e4904c1b513e10d1df4a2ef2ad4540fae1ca0aaf9";
    unsigned char iv[16];
    unsigned char ciphertext[256];

    /**Init OpenSSL**/
    ERR_load_crypto_strings();
    OPENSSL_config(NULL);
    OpenSSL_add_all_algorithms();

    unsigned char word[17];
    while(read_word(fp, word)==0) {
        if(strlen((char *)word) < 16) {
            add_space(word, sizeof(word));
            encrypt(plaintext, strlen((char *)plaintext), word, iv, ciphertext);

            if(strcmp((char *)ciphertext, cipher) == 0 ) {
                printf("The key is : %s\n", word);
                fclose(fp);
                exit(0);
            };
        }
    }

    fclose(fp);
}
read\u word
方法从字典中读取一个单词:

int read_word(FILE *file, unsigned char * word) {
    //Using fscanf
    unsigned char word_buf[255];

    if(fscanf(file, "%s", word_buf) != EOF) {
        if( strlen( (char *)word_buf ) < 15 ) {
            strcpy((char *)word,(char *) word_buf);
        }
        return 0;
    } 
    return 1;
}

你从来没有真正设置过
iv
。因为
iv
是一个局部变量,它的内容未初始化,随后读取它会导致未定义的行为

调用
clock
时可能会发生的情况是,此函数使用的堆栈空间可能在堆栈上的某个位置留下了一些空字节,恰好是调用
encrypt
函数时
iv
将驻留的位置。然而,你不能依赖于这种行为

iv
初始化为全零。然后你会得到你期望的行为

unsigned char iv[16] = { 0 };

谢谢这很有魅力!我想如果我什么都不做,它会为我初始化NULL!!谢谢你的快速回复!!!似乎需要更多的研究:D
void encrypt(unsigned char *plaintext, int plain_len ,unsigned char *key, unsigned char * iv, unsigned char * ciphertext) {
    EVP_CIPHER_CTX *ctx;
    int out_len;
    int ciphertext_len;
    char *buff;

    if(! (ctx = EVP_CIPHER_CTX_new()) ) {
        handleErrors();
    }

    if( EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv) != 1 )
        handleErrors();

    if( EVP_EncryptUpdate(ctx, ciphertext, &out_len, plaintext, plain_len ) != 1 ) {
        handleErrors();
    }
    ciphertext_len = out_len;

    if( EVP_EncryptFinal_ex(ctx, ciphertext+out_len, &out_len) != 1 ) {
        handleErrors();
    }
    ciphertext_len += out_len;

    EVP_CIPHER_CTX_cleanup(ctx);

    buff = malloc(ciphertext_len*2+1);
    memset(buff, 0, ciphertext_len*2+1);

    for (int i = 0; i < ciphertext_len; ++i)
    {
        char tmp[3];
        sprintf(tmp, "%02x", ciphertext[i]);
        strcat(buff, tmp);
    }

    strcpy((char *)ciphertext, buff);
    free(buff);
}
unsigned char iv[16] = { 0 };