c语言字符串api,通过OpenSSL C API解密字符串

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

完全运行命令:

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

我想通过C api做同样的事情。我知道stackoverflow上有很多类似的问题,但似乎没有什么对我有用,所以我恳请你

将此标记为重复。

信息:

十六进制密码

是base64编码的

六角键

我的当前代码:

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;

}

有什么问题吗?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值