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!

5 comentarios:

  1. Hola. Garcais por el post anteriior, excelente.
    Estoy tratando de usar esta función pero me causa error en el load, porque ninguna de las versiones lleva esa funcion existe la de loadKey, pero me devuelve false,
    Algun comentario? Gracias. Excelente trabajo haces. Te aradezco de antemano.

    ResponderEliminar
  2. Hola. Hay varias versiones de PHPSecLib. Necesitas descargar la correcta, para tu version de PHP.

    ResponderEliminar
  3. NO pude hacerlo funcionar convertir el archivo .key a .pem con la libreria phpseclib, pero al fin lo pude lograr con otra librería llamada: cfdiutils (https://github.com/eclipxe13/CfdiUtils/tree/v3)

    Te paso el codigo, por si sirve de ayuda a alguien, esta librería tiene mucho mas cosas, pero me concentro en la solución de este tema en especifico:

    Aquí el codigo en dropbox listo para usar:
    (https://www.dropbox.com/s/i95nh9bfapebvtw/cfdi.rar?dl=0)

    El index.php quedó así:
    derKeyProtect($keyDerFile, $keyDerPass, $keyPemFile, $keyPemPass);

    //==============

    Gracias.

    ResponderEliminar
  4. To skilled surveillance individuals, he's a known benefit player; at any time he could possibly be} noticed, matched to his image in 온라인 카지노 a database of such players and requested to go away a on line casino. If that happens, the security guard could also read him the trespass act, that means Grosjean would danger arrest if he tried to return. Getting away, the opposite hand|however|then again}, would give him a possibility to return back on some future day and perhaps go unnoticed. So if safety was ready for him on the bottom, Grosjean wanted to be able to|to have the flexibility to} run back up in the opposite direction|the other way|the wrong way} with the hope of avoiding a confrontation.

    ResponderEliminar