Skip to content

Instantly share code, notes, and snippets.

@jornh
Created February 24, 2017 11:59
Show Gist options
  • Save jornh/cb7f5a3d350d394d01aa1e8d304a7cd4 to your computer and use it in GitHub Desktop.
Save jornh/cb7f5a3d350d394d01aa1e8d304a7cd4 to your computer and use it in GitHub Desktop.
#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 receiving_end(unsigned char *ciphertext, unsigned char *nonce, unsigned char *key) {
unsigned char decrypted[MESSAGE_LEN];
printf("ciphertext: %s\n", ciphertext);
if (crypto_secretbox_open_easy(decrypted, ciphertext, CIPHERTEXT_LEN, nonce, key) != 0) {
printf("message forged!\n");
}
else {
printf("message fine!: %s\n", decrypted);
}
}
int main(void)
{
unsigned char nonce[crypto_secretbox_NONCEBYTES];
unsigned char key[crypto_secretbox_KEYBYTES];
unsigned char ciphertext[CIPHERTEXT_LEN];
randombytes_buf(nonce, sizeof nonce);
randombytes_buf(key, sizeof key);
crypto_secretbox_easy(ciphertext, MESSAGE, MESSAGE_LEN, nonce, key);
printf("ciphertext sent: %s\n", ciphertext);
receiving_end(ciphertext, nonce, key);
ciphertext[3] += 1;
printf("ciphertext sent: %s\n", ciphertext);
receiving_end(ciphertext, nonce, key);
return 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment