Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple demo for public-key cryptography by libsodium in PHP
<?php
/**
* Installation: https://paragonie.com/book/pecl-libsodium/read/00-intro.md#installing-libsodium
* Reference from: https://paragonie.com/book/pecl-libsodium/read/05-publickey-crypto.md
*/
// Alice
$alice_secret = "a9af0bc2b215f9c55734150d296256c8423705d43ddb3823ec99cf4f8fc2d79f";
$alice_public = "e7482479ac9f850248656659ee935e0acda8796cc5354a751f165b93a06e163e";
// Bob
$bob_secret = "d725efd15ef68056cf1442ca0de7b166b01fedf5cae89d327dd2f5a4c98ff93e";
$bob_public = "9724aedd84fbac2384ca31362fee793aabbdad159a035a8102919ca7b30df32a";
$alice_keypair = \Sodium\crypto_box_keypair_from_secretkey_and_publickey(
hex2bin($alice_secret),
hex2bin($bob_public)
);
$bob_keypair = \Sodium\crypto_box_keypair_from_secretkey_and_publickey(
hex2bin($bob_secret),
hex2bin($alice_public)
);
//
// Print message
//
echo "### The keys in Alice and Bob" . PHP_EOL . PHP_EOL;
echo "Alice" . PHP_EOL;
echo "Public key = ";
echo $alice_public . PHP_EOL;
echo "Secret key = ";
echo $alice_secret . PHP_EOL;
echo "Key pair = ";
echo bin2hex($alice_keypair) . PHP_EOL;
echo PHP_EOL;
echo "Bob" . PHP_EOL;
echo "Public key = ";
echo $bob_public . PHP_EOL;
echo "secret key = ";
echo $bob_secret . PHP_EOL;
echo "Key pair = ";
echo bin2hex($bob_keypair) . PHP_EOL;
echo PHP_EOL . "### 1st Message communication phase" . PHP_EOL;
echo PHP_EOL;
sleep(1);
//
// The end of printing message
//
// Alice encrypts her message
$alice_plaintext = "This is a secret message from Alice.";
$alice_nonce = random_bytes(24);
$ciphertext_from_alice = \Sodium\crypto_box($alice_plaintext, $alice_nonce, $alice_keypair);
//
// Print message
//
echo PHP_EOL . "Alice sends the following message to Bob." . PHP_EOL;
echo 'Message = ' . $alice_plaintext . PHP_EOL;
echo PHP_EOL . "Nonce is = " . bin2hex($alice_nonce) . PHP_EOL;
echo "Encrpted message = " . bin2hex($ciphertext_from_alice) . PHP_EOL;
echo PHP_EOL . " ==> Transfering the encrypted message and nonce ...." . PHP_EOL;
sleep(2);
echo PHP_EOL . "Bob received an encrypted message from Alice" . PHP_EOL;
echo PHP_EOL . "Bob decrypts the message by his key pair and the nonce from Alice" . PHP_EOL;
sleep(2);
//
// The end of printing message
//
// Bob uses his key pair and the nonce from Alice to decrypts the encrypted message
$bob_received = \Sodium\crypto_box_open(
$ciphertext_from_alice,
$alice_nonce,
$bob_keypair
);
//
// Print message
//
echo PHP_EOL . " ==> The decrypted result " . PHP_EOL;
if ($bob_received === FALSE) {
echo "The encrypted message Bob received CAN NOT be decrypted.";
} else {
echo ' ' . $bob_received;
}
sleep(3);
echo PHP_EOL;
sleep(1);
echo PHP_EOL;
sleep(1);
echo PHP_EOL . "### 2st Message communication phase" . PHP_EOL;
echo PHP_EOL;
sleep(2);
//
// The end of printing message
//
$bob_plaintext = "Hi, Alice. I received your message!";
$bob_nonce = random_bytes(24);
$ciphertext_from_bob = \Sodium\crypto_box($bob_plaintext, $bob_nonce, $bob_keypair);
//
// Print message
//
echo PHP_EOL . "Bob returns the following message to Bob." . PHP_EOL;
echo 'Message = ' . $bob_plaintext . PHP_EOL;
echo PHP_EOL . "Nonce is = " . bin2hex($bob_nonce) . PHP_EOL;
echo "Encrpted message = " . bin2hex($ciphertext_from_bob) . PHP_EOL;
echo PHP_EOL . " ==> Transfering the encrypted message and nonce ...." . PHP_EOL;
sleep(2);
echo PHP_EOL . "Alice received an encrypted message from Bob" . PHP_EOL;
echo PHP_EOL . "Alice decrypts the message by her key pair and the nonce from Bob" . PHP_EOL;
sleep(2);
//
// The end of printing message
//
// Alice uses her key pair and the nonce from Bob to decrypts the encrypted message
$alice_received = \Sodium\crypto_box_open(
$ciphertext_from_bob,
$bob_nonce,
$alice_keypair
);
//
// Print message
//
echo PHP_EOL . " ==> The decrypted result " . PHP_EOL;
if ($alice_received === FALSE) {
echo "The encrypted message Alice received CAN NOT be decrypted.";
} else {
echo ' ' . $alice_received;
}
//
// The end of printing message
//
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.