Skip to content

Instantly share code, notes, and snippets.

@maxenko
Last active October 9, 2023 06:42
Show Gist options
  • Save maxenko/fd5c0287d44c44e68f4ff9d37cb3d83c to your computer and use it in GitHub Desktop.
Save maxenko/fd5c0287d44c44e68f4ff9d37cb3d83c to your computer and use it in GitHub Desktop.

Revisions

  1. Max Enko renamed this gist Apr 30, 2016. 1 changed file with 0 additions and 0 deletions.
  2. Max Enko revised this gist Apr 30, 2016. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions String encryption using libsodium.
    Original file line number Diff line number Diff line change
    @@ -18,9 +18,9 @@ int main()

    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
    crypto_box_keypair(mr_pk, mr_sk); // generates random key pair

    unsigned char ciphertext[CIPHERTEXT_LEN]; // encrypted msg
    unsigned char ciphertext[CIPHERTEXT_LEN]; // encrypted msg
    crypto_box_seal(ciphertext, MESSAGE, MESSAGE_LEN, mr_pk); // encrypt

    /*
  3. Max Enko revised this gist Apr 30, 2016. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions String encryption using libsodium.
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,6 @@ using namespace std;
    #define CIPHERTEXT_LEN (crypto_box_SEALBYTES + MESSAGE_LEN)



    int main()
    {
    if (sodium_init() < 0) {
    @@ -34,8 +33,9 @@ int main()
    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) {
    cout << (decr_msg == 0 ? "Success \n" : "Failure \n");
    decrypted[CIPHERTEXT_LEN - crypto_box_SEALBYTES] = 0;
    cout << "Decrypted: " << decrypted;
    }
  4. Max Enko created this gist Apr 30, 2016.
    44 changes: 44 additions & 0 deletions String encryption using libsodium.
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,44 @@
    //#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

    if (decr_msg == 0) {
    cout << (decr_msg == 0 ? "Success \n" : "Failure \n");
    decrypted[CIPHERTEXT_LEN - crypto_box_SEALBYTES] = 0;
    cout << "Decrypted: " << decrypted;
    }

    return 0;
    }