PHPackages                             swissmakers/php-openssh - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Security](/categories/security)
4. /
5. swissmakers/php-openssh

ActiveLibrary[Security](/categories/security)

swissmakers/php-openssh
=======================

Allows you to generate 4096 Bit RSA private/public key-pairs, which can be used as authentication method and to encrypt / decrypt strings.

v1.0(2y ago)09[2 PRs](https://github.com/swissmakers/php-openssh/pulls)GPL-3.0-or-laterPHPPHP ^8.1CI passing

Since Jun 20Pushed 6mo ago1 watchersCompare

[ Source](https://github.com/swissmakers/php-openssh)[ Packagist](https://packagist.org/packages/swissmakers/php-openssh)[ Docs](https://github.com/swissmakers/php-openssh)[ RSS](/packages/swissmakers-php-openssh/feed)WikiDiscussions main Synced today

READMEChangelog (1)Dependencies (6)Versions (4)Used By (0)

php-openssh Library
===================

[](#php-openssh-library)

This library allows you to generate 4096-bit OpenSSH private/public key pairs, which can be used for SSH authentication and to encrypt/decrypt strings.

```
use Swissmakers\OpenSSH\PrivateKey;
use Swissmakers\OpenSSH\PublicKey;

// Generate a new OpenSSH key pair
$privateKey = PrivateKey::generate();
$publicKey = $privateKey->getPublicKey();

// Encrypt and decrypt data using the key pair
$data = 'my secret data';
$encryptedData = $publicKey->encrypt($data); // Returns unreadable data
$decryptedData = $privateKey->decrypt($encryptedData); // Returns 'my secret data' in plaintext
```

Installation
------------

[](#installation)

Install the package via Composer:

```
composer require swissmakers/php-openssh
```

Usage
-----

[](#usage)

Generate a private key and save it to a file:

```
use Swissmakers\OpenSSH\PrivateKey;

$privateKey = PrivateKey::generate();
$privateKey->toFile('/home/foo/bar');
```

### Loading Keys

[](#loading-keys)

Load a key from a file:

```
use Swissmakers\OpenSSH\PrivateKey;
use Swissmakers\OpenSSH\PublicKey;

$privateKey = PrivateKey::fromFile($pathToPrivateKey);
$publicKey = PublicKey::fromFile($pathToPublicKey);
```

Load a key from a string:

```
use Swissmakers\OpenSSH\PrivateKey;
use Swissmakers\OpenSSH\PublicKey;

$privateKey = PrivateKey::fromString($privateKeyContent);
$publicKey = PublicKey::fromString($publicKeyContent);
```

Obtain the public key from a private key:

```
use Swissmakers\OpenSSH\PrivateKey;

$privateKey = PrivateKey::fromString($privateKeyContent);
$publicKey = $privateKey->getPublicKey();
```

### Encrypting and Decrypting Data

[](#encrypting-and-decrypting-data)

Encrypt data with a public key and decrypt it with the private key:

```
use Swissmakers\OpenSSH\PrivateKey;
use Swissmakers\OpenSSH\PublicKey;

$data = 'my secret data';

$publicKey = PublicKey::fromFile($pathToPublicKey);
$encryptedData = $publicKey->encrypt($data);

$privateKey = PrivateKey::fromFile($pathToPrivateKey);
$decryptedData = $privateKey->decrypt($encryptedData); // Returns 'my secret data'
```

If decryption fails, a `\Swissmakers\OpenSSH\Exceptions\BadDecryptionException` will be thrown.

### Checking Decryption Capability

[](#checking-decryption-capability)

Determine if data can be decrypted with the private key:

```
use Swissmakers\OpenSSH\PrivateKey;

$privateKey = PrivateKey::fromFile($pathToPrivateKey);
$canDecrypt = $privateKey->canDecrypt($data); // Returns a boolean
```

### Signing and Verifying Data

[](#signing-and-verifying-data)

Sign data with a private key and verify it with a public key:

```
use Swissmakers\OpenSSH\PrivateKey;
use Swissmakers\OpenSSH\PublicKey;

$privateKey = PrivateKey::fromFile($pathToPrivateKey);
$signature = $privateKey->sign('my message'); // Returns a string

$publicKey = PublicKey::fromFile($pathToPublicKey);
$isVerified = $publicKey->verify('my message', $signature); // Returns true
$isModifiedVerified = $publicKey->verify('my modified message', $signature); // Returns false
```

### Validating Inputs (Laravel)

[](#validating-inputs-laravel)

Validate form inputs to check for valid public or private keys:

```
use Swissmakers\OpenSSH\Rules\PublicKeyRule;
use Swissmakers\OpenSSH\Rules\PrivateKeyRule;

public function rules(): array
{
    return [
        'public_key' => [
            new PublicKeyRule(),
        ],
        'private_key' => [
            new PrivateKeyRule(),
        ],
    ];
}
```

Testing
-------

[](#testing)

Run tests using:

```
composer test
```

License
-------

[](#license)

The GNU General Public License v3.0. See the [License File](LICENSE.md) for more information.

###  Health Score

30

—

LowBetter than 62% of packages

Maintenance46

Moderate activity, may be stable

Popularity4

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity51

Maturing project, gaining track record

 Bus Factor2

2 contributors hold 50%+ of commits

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

743d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/173353244?v=4)[Swissmakers GmbH](/maintainers/swissmakers)[@swissmakers](https://github.com/swissmakers)

---

Top Contributors

[![swissmakers](https://avatars.githubusercontent.com/u/173353244?v=4)](https://github.com/swissmakers "swissmakers (8 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (7 commits)")[![tuxrem](https://avatars.githubusercontent.com/u/219455248?v=4)](https://github.com/tuxrem "tuxrem (2 commits)")

---

Tags

opensshphp8phplaravelsecuritysshrsaopenssh

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/swissmakers-php-openssh/health.svg)

```
[![Health](https://phpackages.com/badges/swissmakers-php-openssh/health.svg)](https://phpackages.com/packages/swissmakers-php-openssh)
```

###  Alternatives

[laravel/socialite

Laravel wrapper around OAuth 1 &amp; OAuth 2 libraries.

5.7k108.5M883](/packages/laravel-socialite)[spatie/laravel-csp

Add CSP headers to the responses of a Laravel app

86611.1M25](/packages/spatie-laravel-csp)[tzsk/otp

A secure, database-free One-Time Password (OTP) generator and verifier for PHP and Laravel.

244672.1k1](/packages/tzsk-otp)[dgtlss/warden

A Laravel package that proactively monitors your dependencies for security vulnerabilities by running automated composer audits and sending notifications via webhooks and email

9062.1k](/packages/dgtlss-warden)[laravel-chronicle/core

Tamper-evident audit ledger for Laravel applications.

1213.1k3](/packages/laravel-chronicle-core)[mwguerra/web-terminal

A web-based terminal component for Filament/Laravel with command whitelisting and multiple connection types

296.1k](/packages/mwguerra-web-terminal)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
