halite
halite copied to clipboard
Convert keys into strings and convert key strings back into keys?
I would like to know how to convert an asymmetric public and private key into a base64 string and be able to convert the base64 string back into a key.
I was able to convert the public key into a base64 string but have not been successful in converting the base64 string back into a halite public key. And I was not successful in converting a private key into a base64 string and therefore unable to convert a base64 string into a halite private key.
<?php
require 'vendor/autoload.php';
const MY_TEXT = 'My Text';
//Create private and public key
$publicPrivateKeyPairs = \ParagonIE\Halite\KeyFactory::generateEncryptionKeyPair();
$publicKey = $publicPrivateKeyPairs->getPublicKey();
$privateKey = $publicPrivateKeyPairs->getSecretKey();
$publicKeyString = base64_encode($publicKey);
echo $publicKeyString . PHP_EOL;
$privateKeyString = base64_decode($privateKey);
echo $privateKeyString . PHP_EOL;
//Encrypt
$encryptedText = \ParagonIE\Halite\Asymmetric\Crypto::seal(
new \ParagonIE\HiddenString\HiddenString(MY_TEXT),
$publicKey,
);
echo $encryptedText . PHP_EOL;
//Decrypt
$decryptedTextHiddenString = \ParagonIE\Halite\Asymmetric\Crypto::unseal($encryptedText, $privateKey);
//Convert HiddenString type into a string
echo $decryptedTextHiddenString->getString() . PHP_EOL;