Skip to content

Instantly share code, notes, and snippets.

@maxenko
Last active October 9, 2023 06:42
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save maxenko/fd5c0287d44c44e68f4ff9d37cb3d83c to your computer and use it in GitHub Desktop.
Save maxenko/fd5c0287d44c44e68f4ff9d37cb3d83c to your computer and use it in GitHub Desktop.
//#include "stdafx.h"
#include <sodium.h>
#include <iostream>
using namespace std;
#define MESSAGE (const unsigned char *) "test"
#define MESSAGE_LEN 4
#define CIPHERTEXT_LEN (crypto_box_SEALBYTES + MESSAGE_LEN)
int main()
{
if (sodium_init() < 0) {
cout << "sodium cannot initialize, exiting...";
return 1;
}
unsigned char mr_pk[crypto_box_PUBLICKEYBYTES]; // public key
unsigned char mr_sk[crypto_box_SECRETKEYBYTES]; // private key
crypto_box_keypair(mr_pk, mr_sk); // generates random key pair
unsigned char ciphertext[CIPHERTEXT_LEN]; // encrypted msg
crypto_box_seal(ciphertext, MESSAGE, MESSAGE_LEN, mr_pk); // encrypt
/*
unsigned char encrypted_display[CIPHERTEXT_LEN+1];
memcpy(encrypted_display, ciphertext, CIPHERTEXT_LEN);
encrypted_display[CIPHERTEXT_LEN] = 0;
cout << "encrypted: " << encrypted_display << "\n";
*/
unsigned char decrypted[CIPHERTEXT_LEN - crypto_box_SEALBYTES + 1];
auto decr_msg = crypto_box_seal_open(decrypted, ciphertext, CIPHERTEXT_LEN, mr_pk, mr_sk); // decrypt
cout << (decr_msg == 0 ? "Success \n" : "Failure \n");
if (decr_msg == 0) {
decrypted[CIPHERTEXT_LEN - crypto_box_SEALBYTES] = 0;
cout << "Decrypted: " << decrypted;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment