Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executing the openssl command with the c system function is different from executing the openssl command output on the terminal

Tags:

c

openssl

I'm using MacOS/10.13

My code on terminal:

echo -n this | openssl enc -aes-128-cbc -K 0 -iv 0 -base64

c code:

int main(){
   system("echo -n this | openssl enc -aes-128-cbc -K 0 -iv 0 -base64");
}

Running ScreenShot

enter image description here

enter image description here

like image 393
Jenny Avatar asked Feb 18 '26 21:02

Jenny


1 Answers

The echo command has the problem that its behavior is not portable between different shells or environments. It is better to use printf instead, which is portable. In your case, replace the echo -n statement with printf, resulting in the following code:

#include <stdlib.h>
int main() {
    system("printf this | openssl enc -aes-128-cbc -K 0 -iv 0 -base64");
}

Building and running:

$ gcc test.c -o test
$ ./test
gc8X3os/mFxDE73AebmweQ==

as desired.

like image 70
Reinier Torenbeek Avatar answered Feb 20 '26 10:02

Reinier Torenbeek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!