Search code examples
c++c++11libgcrypt

C++, how to copy the return value of gcry_cipher_encrypt in gcrypt library to a variable?


In the code I attached below, it uses gcry_cipher_encrypt. At the end of the code it output the content in encBuffer as hex value string. I need to make it in a variable like char[], char*, or string, whatever and use it.

According to handbook of gcrypt, encBuffer, the second item of the function should be an unsigned char* type variable. It should points to an unsigned char array, I consider. But when I do:

for(int i = 0; i < txtLength-1;i++){
   cout<<encBuffer[i];
}

I get mass code. How can I get readable content from encBuffer please? Thank you very much.

#include <stdio.h>
#include <gcrypt.h>

int main () {
gcry_error_t     gcryError;
gcry_cipher_hd_t gcryCipherHd;
size_t           index;
char * salsaKey = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; // 32 bytes
char * iniVector = "AAAAAAAA"; // 8 bytes

gcryError = gcry_cipher_open(
    &gcryCipherHd, // gcry_cipher_hd_t *
    GCRY_CIPHER_SALSA20,   // int
    GCRY_CIPHER_MODE_STREAM,   // int
    0);            // unsigned int
if (gcryError)
{
    printf("gcry_cipher_open failed:  %s/%s\n",
           gcry_strsource(gcryError),
           gcry_strerror(gcryError));
    return;
}
printf("gcry_cipher_open worked\n");

gcryError = gcry_cipher_setkey(gcryCipherHd, salsaKey, 32);
if (gcryError)
{
    printf("gcry_cipher_setkey failed:  %s/%s\n",
           gcry_strsource(gcryError),
           gcry_strerror(gcryError));
    return;
}
printf("gcry_cipher_setkey worked\n");

gcryError = gcry_cipher_setiv(gcryCipherHd, iniVector, 8);
if (gcryError)
{
    printf("gcry_cipher_setiv failed:  %s/%s\n",
           gcry_strsource(gcryError),
           gcry_strerror(gcryError));
    return;
}
printf("gcry_cipher_setiv worked\n");

size_t txtLength = 101;
char * encBuffer = malloc(txtLength);
char * textBuffer = malloc(txtLength);
memset(textBuffer, 0, 101);

gcryError = gcry_cipher_encrypt(
    gcryCipherHd, // gcry_cipher_hd_t
    encBuffer,    // void *
    txtLength,    // size_t
    textBuffer,    // const void *
    txtLength);   // size_t
if (gcryError)
{
    printf("gcry_cipher_decrypt failed:  %s/%s\n",
           gcry_strsource(gcryError),
           gcry_strerror(gcryError));
    return;
}
printf("gcry_cipher_decrypt worked\n");

printf("encBuffer = ");
for (index = 0; index<txtLength-1; index++)
    printf("%02X", (unsigned char)encBuffer[index]);
printf("\n");
return 0;
}

Solution

  • I made the following function using malloc (because your code does).

    char *buffer2hex(char *encBuffer, int txtLength){
        char *encHexText = (char *)malloc(txtLength*2+1), 
             *eht = encHexText;
        for (int i = 0; i < txtLength; i++){
            int c = (unsigned char)encBuffer[i];
            #define tohex(n) ((n)>9?(n)-10+'A':(n)+'0')
            *eht++ = tohex(c>>4);
            *eht++ = tohex(c&0xf);
            #undef tohex
        }
        *eht = '\0';
        return encHexText; 
    }
    

    At the end of your method you can call it like this:

        char *hex = buffer2hex(encBuffer, txtLength);
        printf("%s\n", hex); // Use it
        free(hex);           // and free it!