I've wrote the below code snippet that passes a string to openssl by echo'ing, using popen; however the result is different than if I manually pass the arguments in PowerShell. Any help is appreciated...
void encrypt_message(const char * message_in, char * encrypted_return)
{
char encryption_cmd[1024] = { '\0' };
strcat(encryption_cmd, "echo '");
strcat(encryption_cmd, message_in);
strcat(encryption_cmd, "' | openssl.exe dgst -sha256 -hmac ");
strcat(encryption_cmd, SEC_KEY);
printf("CMD= %s\n",encryption_cmd);
char psBuffer[1024] = { '\0' };
FILE *pPipe;
if ((pPipe = _popen(encryption_cmd, "rt")) == NULL)
return;
while (fgets(psBuffer, 1024, pPipe));
if (!feof(pPipe))
printf("Error: Failed to read the pipe to the end.\n");
sscanf(psBuffer, "(stdin)= %s", encrypted_return);
printf("RESULT= %s\n",encrypted_return);
}
This is the output of the function:
However when I manually invoke in PowerShell I get a different result
It looks as if popen is sprinkling something in that openssl is interpretting, yielding a different result. But I can't narrow what exactly! Thanks
UPDATE: I get the same result as my program code when I pass arguments in cmd.exe instead of PowerShell.... so it appears it's PowerShell that is doing something strange.