c - 通过 OpenSSL 解密字符串

标签 c encryption openssl

我的目标是解密 Base64 编码的字符串。

运行命令完全有效:

openssl enc -base64 -d -aes-128-cbc -iv '{iv}' -K {hex_key} <<< {hex_enc_password}

我想通过 C api 执行完全相同的操作。我知道 stackoverflow 上有很多类似的问题,但似乎没有什么对我有用,所以我恳请您不要将其标记为重复。

信息:

  • hex_enc_password 采用 Base64 编码

  • hex_key 是一个字节字符串(通过 hexlify())(示例:0x1a,0xc4 -> "1ac4")

我当前的代码:


char* key = "lbzA54tAAg3E/jPxiJc34e==";
unsigned char *hex_key = hexlify(key);

char encrypted[] = {0x3d,0xd2,0x02,0xc3,0xae,0x1e,0xf5,0x7e,0x33,0xc3,0x1d,0x7b,0x1e,0x48,0x73,0x84};
size_t output_length;
unsigned char *hex_enc_password = base64_encode(encrypted, sizeof(encrypted), &output_length);

decrypt(hex_enc_password, strlen(hex_enc_password),hex_key, iv);

char* decrypt(unsigned char *ciphertext, int ciphertext_len, unsigned char *key, unsigned char *iv ) {
    int outLen1 = 0; int outLen2 = 0;
    unsigned char *outdata = malloc(ciphertext_len+1);
    bzero(outdata, ciphertext_len+1);
    //setup decryption
    EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
    EVP_DecryptInit(ctx,EVP_aes_128_cbc(),key,iv);                     //returns 1
    EVP_DecryptUpdate(ctx,outdata,&outLen1,ciphertext,ciphertext_len); //returns 1
    EVP_DecryptFinal(ctx,outdata + outLen1,&outLen2);                  //returns 0

    return outdata;
}

有什么想法可能是错误的吗?

最佳答案

我终于让它工作了。 正如上面评论中所述,任何内容都不应该以 base64 进行编码。使用 -base64 调用命令行二进制文件只会使 openssl 在继续之前对其进行解码。妖术化也是如此,只是跳过它。

除此之外的所有内容都应该与上面相同。

关于c - 通过 OpenSSL 解密字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58931983/

相关文章:

python - RSA 解密返回额外数据

encryption - 便宜的 SSL 与昂贵的 SSL

android - 我想在 android 中将音频服务器 (/frameworks/av/media/audioserver) 构建为 64 位,但它无法启动

c - 在制作大缓冲区时,使用 malloc 分配缓冲区是否比使用静态分配的缓冲区更快?

c - rand 应该用于 IV 吗?

asp.net - Request.Querystring 从加密文本中删除字符

c - 分析用 C 编写的函数的时间复杂度

ssl - Jenkins 2 和 Atlassian Crowd(crowd2 插件)与双向 SSL 集成

linux - Fedora 配置以启用 TLS 1.2

openssl - BIO中的B代表什么?