} } } echo "<tr><td>PHP_VERSION</td><td>" . (version_compare(PHP_VERSION, '4.2.0', '>=') ? '>=4.2.0' : '<4.2.0') . '</td></tr>'; echo "<tr><td>openssl</td><td>" . (extension_loaded('openssl') ? 'extension loaded' : 'extension not loaded') . '</td></tr>'; echo "<tr><td>openssl_pkey_get_details</td><td>" . (function_exists('openssl_pkey_get_details') ? 'exists' : 'doesn\'t exist') . '</td></tr>'; echo "<tr><td>Private key</td><td>" . ($privateKey != '' ? 'Found' : 'Not found') . '</td></tr>'; echo "<tr><td>Public key</td><td>" . ($publicKey != '' ? 'Found' : 'Not found') . '</td></tr>'; 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.
/** * Encriptar datos con la clave pública * * @param string $data los datos a encriptar * @return string */ public function encryptRSA($data) { $Rsa = new \phpseclib\Crypt\RSA(); $Rsa->setEncryptionMode(\phpseclib\Crypt\RSA::ENCRYPTION_PKCS1); $Rsa->loadKey($this->getPublicKey()); return $Rsa->encrypt($data); }
public function encrypt() { $rsa = new \phpseclib\Crypt\RSA(); $rsa->loadKey($this->key); $plaintext = file_get_contents($this->getFileUploadDir() . '/' . $this->file_name); $ciphertext = $rsa->encrypt($plaintext); $hash = md5($plaintext); $this->save_name = 'EncryptedFile_' . $hash; file_put_contents($this->getFileRootDir() . '/' . $this->save_name, $ciphertext); unlink($this->file->getPathname()); return new CryptoFile($hash, $this->getWebPath() . '/' . $this->save_name); }
$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); echo 'Ciphertext (RAW): ' . $ciphertext_raw . PHP_EOL; //Encode as base64 for better management $ciphertext = base64_encode($ciphertext_raw); echo 'Ciphertext (base64): ' . $ciphertext . PHP_EOL; //Decode from base64 then decrypt with private key $decrypted = $rsa_private->decrypt(base64_decode($ciphertext)); echo 'Decrypted: ' . $decrypted . PHP_EOL; //Is everything ok? var_dump($plaintext == $decrypted);