Convert .Key file to .PEM in PHP

Hi tyros, im answering a question from a follower. Here is the way to convert a ".key" file to ".pem", using "phpseclib" in php

spl_autoload_register(function ($class_name) {
    $url = str_replace("\\", "/", $class_name);
    include_once __DIR__ . "/" . $url . ".php";
});

use phpseclib\Crypt\RSA as RSA;
use phpseclib\Crypt\Common\Keys\PKCS1 as PKCS1;


    public function KeyPEM() {
        $rsa = new RSA();
        $rsa->setPassword($this->password_key); //"$this->password_key": your .key file password
        $rsa->load(file_get_contents($this->path_archivo_key)); //"$this->path_archivo_key": your path to .key file
        $private = openssl_pkey_get_private($rsa->getPrivateKey(), $this->password_key);
        $result = "";
        openssl_pkey_export($private, $result);
        return $result;
    }

Here you can get PHPSecLib: Get it here
Note: "phpseclib" needs "ParagonIE" library to works correctly, Get it here.
Thanks!

No hay comentarios:

Publicar un comentario