c - 加密更新时出现 OpenSSL AES 分段错误
问题描述
我有设置大小的字符串并尝试对其进行 AES 加密,但我得到分段错误EVP_EncryptUpdate
size_t dec_len = 20;
char *dec = malloc(dec_len + 1);
//Fill dec
...
//Encrypt
EVP_CIPHER_CTX *ctx;
ctx = EVP_CIPHER_CTX_new();
unsigned char *key = (unsigned char *)" no ";
EVP_EncryptInit_ex(ctx, EVP_aes_128_ecb(), NULL, &key, NULL);
EVP_CIPHER_CTX_set_padding(ctx, 0);
unsigned char *ciphertext;
int ciphertext_len;
EVP_EncryptUpdate(ctx, ciphertext, &ciphertext_len, dec, dec_len);
EVP_EncryptFinal_ex(ctx, ciphertext + ciphertext_len, &ciphertext_len);
EVP_CIPHER_CTX_free(ctx);
我不知道是什么原因造成的。谢谢你。
解决方案
根据OpenSSL 文档,声明为
int EVP_EncryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
ENGINE *impl, const unsigned char *key, const unsigned char *iv);
请注意,key
声明为const unsigned char *key
.
但是你的代码是
unsigned char *key = (unsigned char *)" no ";
EVP_EncryptInit_ex(ctx, EVP_aes_128_ecb(), NULL, &key, NULL);
您将指针的地址传递给函数 -而不是. 你想传递字符串的地址,它指向:key
unsigned char **
const unsigned char *
key
const unsigned char *key = (const unsigned char *)" no ";
EVP_EncryptInit_ex(ctx, EVP_aes_128_ecb(), NULL, key, NULL);
推荐阅读
- r - 对数据框列向量的下一个元素应用操作
- hadoop - 在 Hadoop 中导航文件系统
- java - 如何对十六进制行键进行 HBase 范围扫描?
- r - 如何用不同的变量处理覆盖两个图?
- python - 在xml python中查找子元素的子元素
- python - 代码不适用于列 CONTENT 每一行的停用词
- sql - 将非结构化字符串转换为特定格式
- elasticsearch - Elasticsearch - 包括在_source中排除的突出显示字段
- javascript - 对象内部的数组拆分
- android - 我可以声明一个 OnClickListener 派生类并将其实例传递给 Frida 中视图的 setOnClickListener 方法吗