[英]Decrypt string via OpenSSL
My goal is to decrypt a base64 encoded string.我的目标是解密 base64 编码字符串。
Running the command fully works:运行该命令完全有效:
openssl enc -base64 -d -aes-128-cbc -iv '{iv}' -K {hex_key} <<< {hex_enc_password}
I'd like to do the exact same via the C api.我想通过 C api 做同样的事情。 I know there're many similar questions on stackoverflow, but nothing seems to work for me, so I kindly ask you not to flag this as duplicate.
我知道在 stackoverflow 上有很多类似的问题,但似乎没有什么对我有用,所以我恳请您不要将其标记为重复。
Info:信息:
hex_enc_password is base64 encoded hex_enc_password是 base64 编码的
hex_key is a string as byte-string (via hexlify()) (example: 0x1a,0xc4 -> "1ac4") hex_key是字节串形式的字符串(通过 hexlify())(例如:0x1a,0xc4 -> "1ac4")
My current Code:我目前的代码:
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;
}
Any ideas what could be wrong?有什么想法可能是错的吗?
I finally got it to work.我终于让它工作了。 As described in the comments above, nothing should be encoded in base64.
如上面评论中所述,base64 中不应编码任何内容。 Calling the command line binary with the
-base64
simply makes openssl decode it before continuing.使用
-base64
调用命令行二进制文件只会让 openssl 在继续之前对其进行解码。 Same goes for hexlifying, just skip that. hexlifying也是如此,跳过它。
Everything besides that should be the same as above.除此之外的所有内容都应与上述相同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.