halite icon indicating copy to clipboard operation
halite copied to clipboard

Convert keys into strings and convert key strings back into keys?

Open trymeouteh opened this issue 9 months ago • 0 comments

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;

trymeouteh avatar Jun 05 '25 21:06 trymeouteh