我找到了一些演示(wiki,github),但他们没有工作。运行我编写的代码,请参考上面链接中的代码Segmentation fault(core dumped)
int main(int argc, char *argv[]){
char *ciphertext;
char *key;
char *iv;
char *plaintext;
key = "AES256-GCM-KEY-1234567890abcdefg";
iv = "8a9cef0205a3";
ciphertext = "uatBMo8k7EAnDQuNK4CHASJerQZpCWv86PGditzdYnQV9uqotQ==";
plaintext = "";
decrypt(ciphertext, NULL, 0, NULL, key, iv, plaintext);
return 0;
}
int decrypt(unsigned char *ciphertext, unsigned char *aad, int aad_len, unsigned char *tag, unsigned char *key, unsigned char *iv, unsigned char *plaintext){
EVP_CIPHER_CTX *ctx;
int len, ret;
ctx = EVP_CIPHER_CTX_new();
EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN,strlen(iv),NULL);
EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv);
EVP_DecryptUpdate(ctx,plaintext, &len, ciphertext, strlen(ciphertext));//"segmentation fault"
ret = EVP_DecryptFinal_ex(ctx, plaintext, &len);
EVP_CIPHER_CTX_free(ctx);
printf(ret > 0 ? "success!\n" : "failed!\n");
return 0;
}
我很困惑,如何在不使用Authenticate的情况下使用此api进行解密?
答案 0 :(得分:0)
您需要为指针(所有这些)分配内存
key = malloc(sizeof(*key) * strlen("AES256-GCM-KEY-1234567890abcdefg") + 1);
你应该用theses标志编译你的main(如果gcc是编译器):-W -Wall -Wextra
它会告诉你你的指针未初始化