Forked from maxenko/String encryption using libsodium.cpp
Created
October 9, 2023 06:42
-
-
Save abiiranathan/f632c542f7285c070d33a76e69c5113c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//#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