phpseclib

phpseclib

  • Docs
  • API
  • Support
  • GitHub

›Public Keys

Introduction

  • Why phpseclib?
  • Installation
  • Speed
  • Versioning

SSH2

  • Connecting
  • Authenticating
  • Running Commands
  • SFTP
  • Diagnosing Issues

Public Keys

  • Overview
  • RSA
  • DSA
  • Elliptic Curves
  • (EC)DH
  • Example: JWT

Symmetric Keys

  • Overview

X.509

  • X.509
  • CSR
  • SPKAC
  • CRL

Interoperability

  • Overview
  • Python
  • Java
  • JavaScript
  • Node.js
  • Go
  • Ruby
  • C#
  • C
  • PHP

(EC)DH

Loading and saving keys is discussed in Public Keys: Overview.

Supported Formats

ECDH keys can be in any of the formats discussed in Elliptic Curves: Supported Formats. Calling \phpseclib3\Crypt\PublicKeyLoader::load() on keys of that formats will return a \phpseclib3\Crypt\EC object.

Regular DH keys can be of the following formats:

  • PKCS1 [1]
    • Keys start with -----BEGIN DH PARAMETERS-----
  • PKCS8 [1]
    • Keys start with -----BEGIN PRIVATE KEY----- or -----BEGIN ENCRYPTED PRIVATE KEY----- or -----BEGIN PUBLIC KEY-----

A more in-depth discussion of these two formats can be found in Common Key Formats.

DH Public / Private keys will not be instances of \phpseclib3\Crypt\Common\PublicKey or \phpseclib3\Crypt\Common\PrivateKey but rather of phpseclib3\Crypt\DH\PublicKey and phpseclib3\Crypt\DH\PrivateKey.

Creating Keys

ECDH keys can be created using the technique described in Elliptic Curves: Creating Keys.

Regular DH keys are created thusly:

use phpseclib3\Crypt\DH;

$params = DH::createParameters(...);
$private = DH::createKey($params, 160);
$public = $private->getPublicKey();

The second parameter is optional and can be used to optionally speed up the computation of the key. Quoting RFC4419 § 6.2. Private Exponents, "To increase the speed of the key exchange, both client and server may reduce the size of their private exponents. It should be at least twice as long as the key material that is generated from the shared secret"

A discussion of the parameters that DH::createParameters() accepts follows:

Specifying Prime and Base

use phpseclib3\Crypt\DH;
use phpseclib3\Math\BigInteger;

$prime = new BigInteger('...');
$base = new BigInteger(2);

$params = DH::createParameters($prime, $base);
$key = DH::createKey($params);

Size of Prime in bits

The base, in this case, is assumed to be 2.

use phpseclib3\Crypt\DH;

$params = DH::createParameters(1024);
DH::createKey($params);

By Name

use phpseclib3\Crypt\DH;

$params = DH::createParameters('diffie-hellman-group1-sha1');
DH::createKey($params);

The following named primes are supported:

NameRef
diffie-hellman-group1-sha1RFC2409
diffie-hellman-group14-sha1RFC3526
diffie-hellman-group14-sha256RFC3526
diffie-hellman-group15-sha512RFC3526
diffie-hellman-group16-sha512RFC3526
diffie-hellman-group17-sha512RFC3526
diffie-hellman-group18-sha512RFC3526

Computing Shared Secrets

Shared secrets can be computed by calling DH::computeSecret($private, $public).

The private key must be an instance of either \phpseclib3\Crypt\DH\PrivateKey or \phpseclib3\Crypt\EC\PrivateKey.

With ECDH

The public key can be either an instance \phpseclib3\Crypt\EC\PublicKey or a string representing an encoded coordinate.

With DH

The public key can either be an instance of \phpseclib3\Crypt\DH\PublicKey, a string (that will ultimately be parsed as a base-256 BigInteger) or an instance of \phpseclib3\Math\BigInteger.

← Elliptic CurvesExample: JWT →
  • Supported Formats
  • Creating Keys
    • Specifying Prime and Base
    • Size of Prime in bits
    • By Name
  • Computing Shared Secrets
    • With ECDH
    • With DH
phpseclib
Docs
IntroductionSSH2 / SFTPPublic Key CryptoSymmetric Key CryptoX.509 / CSR / SPKAC / CRLInteroperability
Support
Docs (1.0 / 2.0)Stack OverflowGitHubStar
Sponsor
PatreonGitHubPayPal
Copyright © 2025 Jim Wigginton