2

I am trying to encrypt and decrypt a simple string by following the example here Here

#define MESSAGE ((const unsigned char *) "test")
#define MESSAGE_LEN 4
#define CIPHERTEXT_LEN (crypto_secretbox_MACBYTES + MESSAGE_LEN)

unsigned char key[crypto_secretbox_KEYBYTES];
unsigned char nonce[crypto_secretbox_NONCEBYTES];
unsigned char ciphertext[CIPHERTEXT_LEN];

crypto_secretbox_keygen(key);
randombytes_buf(nonce, sizeof nonce);
crypto_secretbox_easy(ciphertext, MESSAGE, MESSAGE_LEN, nonce, key);

unsigned char decrypted[MESSAGE_LEN];
if (crypto_secretbox_open_easy(decrypted, ciphertext, CIPHERTEXT_LEN, nonce, 
key) != 0) {
    /* message forged! */
}

My question is: How do I get back the original text? 'test' in this case? Libsodium seem to use unsigned char* throughout. All Libsodium examples seem to end with "Message forged!". A followup question question, how do you convert the cipher text into a serializable format?

Joe
  • 619
  • 1
  • 8
  • 16
  • Is it possible that you are looking at the wrong example? What you describe looks more like a sign/verify mechanism than a encrypt/decrypt. Are you aware of the difference? – Yunnosch Aug 10 '18 at 13:36
  • @Yunnosch Nope: That is where all their searches point to. Kindly check out the above link. – Joe Aug 10 '18 at 14:32

1 Answers1

3

First of all, libsodium is well documented and so easily understandable despite the fact cryto subjects can be a bit tough sometimes, so good choice.

I have added comments to your code to clarify how it works. I have also added a dump function to ease code understanding.

#include <stdlib.h>
#include <stdio.h>
#include <sodium.h>

#define MESSAGE ((const unsigned char *) "test")
#define MESSAGE_LEN 4
#define CIPHERTEXT_LEN (crypto_secretbox_MACBYTES + MESSAGE_LEN)

void dump_hex_buff(unsigned char buf[], unsigned int len)
{
    int i;
    for (i=0; i<len; i++) printf("%02X ", buf[i]);
    printf("\n");
}

int main(int argc, char *argv[])
{
    /* Variable declaration */
    unsigned char key[crypto_secretbox_KEYBYTES];
    unsigned char nonce[crypto_secretbox_NONCEBYTES];
    unsigned char ciphertext[CIPHERTEXT_LEN];
    unsigned char decrypted[MESSAGE_LEN];

    /* Generating a random key */
    crypto_secretbox_keygen(key);
    printf("secret key generated:\n");
    dump_hex_buff(key, crypto_secretbox_KEYBYTES);

    /* Using random bytes for a nonce buffer (a buffer used only once) */
    randombytes_buf(nonce, sizeof nonce);
    printf("nonce:\n");
    dump_hex_buff(nonce, sizeof nonce);

    /* Encrypt MESSAGE using key and nonce
       Encrypted message is stored in ciphertext buffer */
    crypto_secretbox_easy(ciphertext, MESSAGE, MESSAGE_LEN, nonce, key);
    printf("ciphertext:\n");
    dump_hex_buff(ciphertext, CIPHERTEXT_LEN);



    /* Decrypt ciphertext buffer using key and nounce
       Decrypted message is stored in decrypted buffer */
    if (crypto_secretbox_open_easy(decrypted, ciphertext, CIPHERTEXT_LEN, nonce, key) != 0) {
        /* message forged!, meaning decryption failed */

    } else {
        /* Successful decryption */
        printf("decrypted data (hex):\n");
        dump_hex_buff(decrypted, MESSAGE_LEN);
        printf("decrpyted data (ascii):%.4s\n", decrypted);
    }

    return 0;
}

You need to clarify what you want when you say you need to serialize decrypted data. This lib is using standard C array.

If you're using linux, you can compile code using:

gcc -o pgm main.c /usr/local/lib/libsodium.a

Ouput result of the code:

secret key generated:
87 28 B9 43 0E DA B5 37 CD 3A 67 A3 DB 4A 31 24 67 4E E3 81 AE 03 FB 81 B7 60 2E 1A F3 6A A6 F4 
nonce:
7B 11 84 24 55 24 98 7A 6B 0B 23 34 66 48 8F 1C C3 4E 20 3E 42 31 02 9B 
ciphertext:
00 BC C0 86 61 37 00 0D 90 76 46 C3 17 39 A5 00 E3 F8 A8 9D 
decrypted data (hex):
74 65 73 74 
decrpyted data (ascii):test
Martin
  • 877
  • 8
  • 20