1. Go to this page and download the library: Download codeaken/sshkey library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
codeaken / sshkey example snippets
// Read in the public and private keys
$publicKey = SshPublicKey::fromFile('id_rsa.pub');
$privateKey = SshPrivateKey::fromFile('id_rsa');
$encryptedPrivateKey = SshPrivateKey::fromFile('id_encrypted_rsa', 'abc123');
// Try to read a key that does not exists; will throw a FileNotFoundException
try {
$missingKey = SshPublicKey::fromFile('nosuchkey.pub');
}
catch (Codeaken\SshKey\Exception\FileNotFoundException $e)
{
echo 'Could not find the key';
}
// Try to read an encrypted private key using the wrong password; will throw a
// LoadKeyException
try {
$encryptedKey = SshPrivateKey::fromFile('id_encrypted_rsa', 'wrongpass');
}
catch (Codeaken\SshKey\Exception\LoadKeyException $e)
{
echo 'Could not decrypt the private key';
}
// In this case we will read the key data from a file for simplicity but it
// could come from a database or some other source
$publicKeyData = file_get_contents('id_rsa.pub');
$publicKey = new SshPublicKey($publicKeyData);
$encryptedPrivateKeyData = file_get_contents('id_encrypted_rsa');
$privateKey = new SshPrivateKey($encryptedPrivateKeyData, 'abc123');
// 1024 bits and no passphrase
$keyPair1 = SshKeyPair::generate(1024);
// 2048 bits and a passphrase of abc123
$keyPair2 = SshKeyPair::generate(2048, 'abc123');
echo $keyPair2->getPrivateKey()->getKeyData(SshKey::FORMAT_PKCS8);
echo $keyPair2->getPublicKey()->getKeyData(SshKey::FORMAT_OPENSSH);