// asymmetric - crypto_box_easy is easy but it's not asymmetric, in that // Alice can also decrpt things encrypted to Bob // // CFLAGS=`pkg-config --cflags --libs libsodium` make asymmetric // ./asymmetric #include #include #define MESSAGE (const unsigned char *) "blub" #define MESSAGE_LEN 4 #define CIPHERTEXT_LEN (crypto_box_MACBYTES + MESSAGE_LEN) int main(int argc, char *argv[]) { unsigned char alice_pub[crypto_box_PUBLICKEYBYTES]; unsigned char bobby_pub[crypto_box_PUBLICKEYBYTES]; unsigned char alice_key[crypto_box_SECRETKEYBYTES]; unsigned char bobby_key[crypto_box_SECRETKEYBYTES]; if (sodium_init()) err(1, "sodium_init"); if (crypto_box_keypair(alice_pub, alice_key)) errx(1, "crypto_box_keypair"); if (crypto_box_keypair(bobby_pub, bobby_key)) errx(1, "crypto_box_keypair"); unsigned char nonce[crypto_box_NONCEBYTES]; randombytes_buf(nonce, sizeof nonce); unsigned char ciphertext[CIPHERTEXT_LEN]; unsigned char decrypted[MESSAGE_LEN]; if (crypto_box_easy(ciphertext, MESSAGE, MESSAGE_LEN, nonce, bobby_pub, alice_key)) errx(1, "crypto_box_easy"); if (crypto_box_open_easy(decrypted, ciphertext, CIPHERTEXT_LEN, nonce, alice_pub, bobby_key)) { errx(1, "crypto_box_open_easy"); } printf("%.*s\n", MESSAGE_LEN, decrypted); // but can Alice also read it? yes! if (crypto_box_open_easy(decrypted, ciphertext, CIPHERTEXT_LEN, nonce, bobby_pub, alice_key)) { errx(1, "crypto_box_open_easy"); } printf("%.*s\n", MESSAGE_LEN, decrypted); }