public function connect($test = false) { if (!$this->connection or $test) { $server = $this->server; require_once 'Crypt/RSA.php'; require_once 'Net/SFTP.php'; $this->connection = new \phpseclib\Net\SFTP($server['host'], $server['port'], 10); $logged_in = false; if (isset($server['sftp_key'])) { $key = new \phpseclib\Crypt\RSA(); if (isset($server['pass']) && !empty($server['pass'])) { $key->setPassword($server['pass']); } $key->loadKey(file_get_contents($server['sftp_key'])); $logged_in = $this->connection->login($server['user'], $key); if (!$logged_in) { Helpers::error("Could not login to {$this->host}. It may be because the key requires a passphrase, which you need to specify it as the 'pass' attribute."); } } else { $logged_in = $this->connection->login($server['user'], $server['pass']); if (!$logged_in) { Helpers::error("Could not login to {$this->host}"); } } if (!$this->connection->chdir($server['path'])) { Helpers::error("Could not change the directory to {$server['path']} on {$this->host}"); } Helpers::logmessage("Connected to: {$this->host}"); $this->current_commit = $this->get_file('REVISION', true); } if ($test) { $this->disconnect(); } }
<?php include '../phpseclib/vendor/autoload.php'; $privKey = new \phpseclib\Crypt\RSA(); $private = file_get_contents('private.pem'); $privKey->setPassword('VdcpDTWTc5Aehxgv2uL9haaFddDBhrc8uCMG3ykg'); $privKey->load($private); $pubKey = new \phpseclib\Crypt\RSA(); $public = file_get_contents('public.pem'); $pubKey->load($public); $subject = new \phpseclib\File\X509(); $subject->setDNProp('id-at-organizationName', 'www.test.com'); $subject->setDNProp('name', 'Name Inc.'); $subject->setDNProp('emailaddress', '*****@*****.**'); $subject->setDNProp('postalcode', '90210'); $subject->setDNProp('state', 'California'); $subject->setDNProp('streetaddress', 'Infinite Loop 1'); $subject->setPublicKey($pubKey); $issuer = new \phpseclib\File\X509(); $issuer->setPrivateKey($privKey); $issuer->setDN($subject->getDN()); $x509 = new \phpseclib\File\X509(); $x509->setStartDate(date('Y-m-d H:i:s')); $x509->setEndDate(date('Y-m-d H:i:s', strtotime('+1 year'))); $result = $x509->sign($issuer, $subject, 'sha512WithRSAEncryption'); $certificate = $x509->saveX509($result); $filepublic = fopen('cert.crt', 'w'); fwrite($filepublic, $certificate); fclose($filepublic); echo 'Cert has been generated' . PHP_EOL; echo $certificate . PHP_EOL;
<?php include '../phpseclib/vendor/autoload.php'; $rsa_signer = new \phpseclib\Crypt\RSA(); $private = file_get_contents('private.pem'); $rsa_signer->setPassword('VdcpDTWTc5Aehxgv2uL9haaFddDBhrc8uCMG3ykg'); $rsa_signer->load($private); $rsa_signer->setHash('sha512'); $rsa_signer->setMGFHash('sha512'); $message = 'Litwo Ojczyzno moja, ty jesteś jak zdrowie'; $signature = $rsa_signer->sign($message, phpseclib\Crypt\RSA::PADDING_PSS); $signature_base64 = base64_encode($signature); echo 'Message: ' . $message . "\r\n"; echo 'Signature (RAW): ' . $signature . "\r\n"; echo 'Signature (base64): ' . $signature_base64 . "\r\n"; echo '------------------------------------DECODING------------------------------------------' . "\r\n"; $rsa_verifier = new \phpseclib\Crypt\RSA(); $rsa_verifier->setHash('sha512'); $rsa_verifier->setMGFHash('sha512'); $public = file_get_contents('public.pem'); $rsa_verifier->load($public); $verification = $rsa_verifier->verify($message, $signature); echo 'Verified: ' . ($verification ? 'TRUE' : 'FALSE');
<?php ini_set('max_execution_time', 300); include '../phpseclib/vendor/autoload.php'; $plaintext = 'This is something secret'; $password = '******'; //Create new RSA Object - private key $rsa_private = new \phpseclib\Crypt\RSA(); //Get private key (in this case content of file) $private = file_get_contents('private.pem'); //This private key is password protected, so load key $rsa_private->setPassword($password); //load the private key $rsa_private->load($private); //set hash (I chose sha512 because sha1 apparently has collisions) $rsa_private->setHash('sha512'); //set MGF hash $rsa_private->setMGFHash('sha512'); //Create new RSA Object - public key $rsa_public = new \phpseclib\Crypt\RSA(); //Get public key (in this case content of file) $public = file_get_contents('public.pem'); //load the public key $rsa_public->load($public); //set hash $rsa_public->setHash('sha512'); //set MGF hash $rsa_public->setMGFHash('sha512'); echo 'Plaintext: ' . $plaintext . PHP_EOL; //encrypt with public key and OAEP as padding $ciphertext_raw = $rsa_public->encrypt($plaintext, phpseclib\Crypt\RSA::PADDING_OAEP);