echo "<tr><td>OpenSSL Library</td><td>" . (isset($versions['Library']) ? $versions['Library'] : 'Not found') . '</td></tr>'; echo "<tr><td>OpenSSL Header</td><td>" . (isset($versions['Header']) ? $versions['Header'] : 'Not found') . '</td></tr>'; $rsa = new \phpseclib\Crypt\RSA(); echo "<tr><td>CRYPT_RSA_MODE</td><td>" . (CRYPT_RSA_MODE == 1 ? 'MODE_INTERNAL' : 'MODE_OPENSSL') . '</td></tr>'; $rsa->setEncryptionMode(phpseclib\Crypt\RSA::ENCRYPTION_PKCS1); $plaintext = 'Jorani is the best open source Leave Management System'; $rsa->loadKey($publicKey); $ciphertext = $rsa->encrypt($plaintext); $rsa->loadKey($privateKey, phpseclib\Crypt\RSA::PRIVATE_FORMAT_PKCS1); $time_start = microtime(true); echo "<tr><td>Decrypted message</td><td>" . $rsa->decrypt($ciphertext) . '</td></tr>'; $time_end = microtime(true); $time = $time_end - $time_start; echo "<tr><td>Decryption speed</td><td>" . $time . '</td></tr>'; //Generate public and private keys for a single usage extract($rsa->createKey(KEY_SIZE)); ?> </tbody> </table> <h2 id="pair">Private and public key pair</h2> <p>This section will help you to create <code>assets/keys/private.pem</code> and <code>assets/keys/public.pem</code> files. Beware that is a pair of keys that must be set together (i.e. you must update the both files at the same time with the corresponding content).</p> <h3>Private Key</h3> <p>You can copy/paste the content below into <code>assets/keys/private.pem</code>.</p> <div class="row-fluid"> <div class="span6">
/** * Crea el par de claves pública y privada */ public function createKeys() { $Rsa = new \phpseclib\Crypt\RSA(); $keys = $Rsa->createKey(1024); $priv = file_put_contents($this->getPrivateKeyFile(), $keys['privatekey']); $pub = file_put_contents($this->getPublicKeyFile(), $keys['publickey']); chmod($this->getPrivateKeyFile(), 0600); return $priv && $pub; }
/** * @Route("/rsa/create",name="RSA_CREATE_KEY") */ function rsaCreateKeyAction() { $form = $this->createFormBuilder()->setMethod('POST')->setAction($this->generateUrl('RSA_CREATE_KEY'))->add('Length', 'number')->add('submit', 'submit')->getForm(); $request = $this->get('request'); $form->handleRequest($request); if ($request->isXmlHttpRequest()) { $length = intval($form['Length']->getData()); $rsa = new \phpseclib\Crypt\RSA(); $key = $rsa->createKey($length); return new JsonResponse(array('public_key' => $key['publickey'], 'private_key' => $key['privatekey'])); } if ($request->getMethod() == 'POST') { $length = intval($form['Length']->getData()); $rsa = new \phpseclib\Crypt\RSA(); $key = $rsa->createKey($length); return $this->render("@Assignment1Cryptography/Crypto/RSA/createkey.html.twig", array('form' => $form->createView(), 'key' => $key)); } return $this->render("@Assignment1Cryptography/Crypto/RSA/createkey.html.twig", array('form' => $form->createView())); }
/** * this sets up the keys for said system */ static function setSessionKeys() { //if the user doesnt have a session create one if (!Session::has('serverKeys')) { //generate keypair for server $rsa = new \phpseclib\Crypt\RSA(); $rsa->setEncryptionMode($rsa::ENCRYPTION_PKCS1); $serverKeyPair = $rsa->createKey(); //save keys to session (encrypted on server) Session::put('serverKeys', $serverKeyPair); Session::put('serverPublic', $serverKeyPair['publickey']); Session::put('serverPrivate', $serverKeyPair['privatekey']); //generate random AES key $key = md5((string) rand(1, 20000) . (string) rand(1, 20000) . (string) rand(1, 20000)); Session::put('serverAES', $key); //static for now } }
<?php require_once dirname(__FILE__) . '/../lib/openpgp.php'; require_once dirname(__FILE__) . '/../lib/openpgp_crypt_rsa.php'; $rsa = new \phpseclib\Crypt\RSA(); $k = $rsa->createKey(512); $rsa->loadKey($k['privatekey']); $nkey = new OpenPGP_SecretKeyPacket(array('n' => $rsa->modulus->toBytes(), 'e' => $rsa->publicExponent->toBytes(), 'd' => $rsa->exponent->toBytes(), 'p' => $rsa->primes[2]->toBytes(), 'q' => $rsa->primes[1]->toBytes(), 'u' => $rsa->coefficients[2]->toBytes())); $uid = new OpenPGP_UserIDPacket('Test <*****@*****.**>'); $wkey = new OpenPGP_Crypt_RSA($nkey); $m = $wkey->sign_key_userid(array($nkey, $uid)); // Serialize private key print $m->to_bytes(); // Serialize public key message $pubm = clone $m; $pubm[0] = new OpenPGP_PublicKeyPacket($pubm[0]); $public_bytes = $pubm->to_bytes();
<?php include '../phpseclib/vendor/autoload.php'; $rsa = new \phpseclib\Crypt\RSA(); extract($rsa->createKey(2048)); $publickey->setHash('sha512'); $publickey->setMGFHash('sha512'); $privatekey->setHash('sha512'); $privatekey->setMGFHash('sha512'); $password = substr(base64_encode(openssl_random_pseudo_bytes(45)), 0, 32); $privatekey->setPassword($password); phpseclib\Crypt\RSA\PKCS1::setEncryptionAlgorithm('AES-256-CBC'); $fileprivate = fopen('private.pem', 'w'); fwrite($fileprivate, $privatekey->getPrivateKey('PKCS1')); fclose($fileprivate); $filepublic = fopen('public.pem', 'w'); fwrite($filepublic, $publickey->getPublicKey('PKCS1')); fclose($filepublic); echo 'Keys has been generated' . "\r\n"; echo 'Password is: ' . $password;