I followed the description here:
Public-key authenticated encryption: crypto_box
and created an example that encrypts a message with Bob's public key and signs it with Alice's secret key :
cipher = crypto_box(secret_string, nonce, bob_pk, alice_sk); The cipher text can be decrypted and verified with Bob's secret key and Alice's public key:
message = crypto_box_open(cipher, nonce, alice_pk, bob_sk); However, the message can also be decrypted with Bob' public key and Alice's secret key:
message = crypto_box_open(cipher, nonce, bob_pk, alice_sk); This is somehow unexpected. I couldn't find much documentation for the crypto box. What is the math behind it?
My full code to reproduce:
#include <crypto_box.h> #include <string> #include <iostream> #include <fstream> #include <randombytes.h> #include <string> #include <iostream> int main() { std::string alice_pk; std::string alice_sk; std::string bob_pk; std::string bob_sk; std::string nonce; std::string cipher; std::string message; std::string secret_string = "my secret string"; unsigned char buffer[crypto_box_NONCEBYTES]; randombytes((unsigned char*)buffer, crypto_box_NONCEBYTES); nonce.assign((const char*)buffer, (size_t)crypto_box_NONCEBYTES); alice_pk = crypto_box_keypair(&alice_sk); bob_pk = crypto_box_keypair(&bob_sk); cipher = crypto_box(secret_string, nonce, bob_pk, alice_sk); message = crypto_box_open(cipher, nonce, bob_pk, alice_sk); std::cout << message << std::endl; message = crypto_box_open(cipher, nonce, alice_pk, bob_sk); std::cout << message << std::endl; }