Avatar of Carlos Llanos
Carlos LlanosFlag for United States of America

asked on 

Help needed to understand PHP Libsodium and how to export keys.

I'm trying to implement a solution using PHP and Libsodium.

I'm working off of this example:

// On Alice's computer:

$alice_box_kp = sodium_crypto_box_keypair();
$alice_sign_kp = sodium_crypto_sign_keypair();

    // Split the key for the crypto_box API for ease of use
    $alice_box_secretkey = sodium_crypto_box_secretkey($alice_box_kp);
    $alice_box_publickey = sodium_crypto_box_publickey($alice_box_kp);
    
    // Split the key for the crypto_sign API for ease of use
    $alice_sign_secretkey = sodium_crypto_sign_secretkey($alice_sign_kp);
    $alice_sign_publickey = sodium_crypto_sign_publickey($alice_sign_kp);

// On Bob's computer:

$bob_box_kp = sodium_crypto_box_keypair();
$bob_sign_kp = sodium_crypto_sign_keypair();

    // Split the key for the crypto_box API for ease of use
    $bob_box_secretkey = sodium_crypto_box_secretkey($bob_box_kp);
    $bob_box_publickey = sodium_crypto_box_publickey($bob_box_kp);
    
    // Split the key for the crypto_sign API for ease of use
    $bob_sign_secretkey = sodium_crypto_sign_secretkey($bob_sign_kp);
    $bob_sign_publickey = sodium_crypto_sign_publickey($bob_sign_kp);

// Optionally, you can reassemble a keypair string from a secret key and 
// public key pair:

$keypair = sodium_crypto_box_keypair_from_secretkey_and_publickey(
    $alice_box_secretkey,
    $alice_box_publickey
);

Open in new window


Basically, stuck on the part where I need to export the public keys so I can share them with another user.

Server A has a private and public key generating in code, but I don't know how to export it.
Server B has a private and public key generating in code, but I don't know how to export it.

Once exported, I will need to know how to import the keys so I can decrypt the messages.

I used to use open_ssl library which was straightforward, but my company desire to use the libsodium libraries now and I can't seem to find any functions to export the keys.
PHP* Cryptography

Avatar of undefined
Last Comment
Carlos Llanos
Avatar of David Favor
David Favor
Flag of United States of America image

That's a really good question.

I just brought up a PHP-7.2 LXD container this morning to start testing WordPress.

My guess is, follow the https://wiki.php.net/rfc/libsodium example.

Using the variable names from the example, save...

$key into a private key file for future use.

$message into a public key file for future use.

Also http://blog.siphos.be/2015/08/switching-openssh-to-ed25519-keys suggests you may be able to generate your keys via ssh-keygen.
Avatar of Carlos Llanos
Carlos Llanos
Flag of United States of America image

ASKER

Yeah, I'm stuck at this exact location...

Using the variable names from the example, save...

$key into a private key file for future use.

$message into a public key file for future use.

I can get everything to display on the pages...but how do I put stuff into a private key file? Do you know where I might be able to find documentation on that?
Avatar of David Favor
David Favor
Flag of United States of America image

I think... because PHP-7.2 has only been out a few days...

I'd likely export/save these files using a naming convention of $name-sodium.key + $name-sodium.pub in some directory... maybe...

/etc/apache/sodium/...

Since your private + public keys are just strings, you'd just pick a naming convention, using directories readable/writable by your PHP FPM process owner, which will likely be same as /etc/apache2 owner... if you're using Debian/Ubuntu...

When you determine a convention you'll be using, be sure + publish what you do in this thread.

You're breaking new ground, we'll all have to traverse shortly.

If you publish your notes, you may save someone a massive amount of headaches/time when they have to navigate the same terrain.
Avatar of Carlos Llanos
Carlos Llanos
Flag of United States of America image

ASKER

Sounds good, I'll keep you posted!
Avatar of David Favor
David Favor
Flag of United States of America image

Thanks!
ASKER CERTIFIED SOLUTION
Avatar of Carlos Llanos
Carlos Llanos
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Carlos Llanos
Carlos Llanos
Flag of United States of America image

ASKER

Solution provided in text above.
PHP
PHP

PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.

125K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo