PHPackages                             m1n64/vernam-cipher - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. m1n64/vernam-cipher

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

m1n64/vernam-cipher
===================

1.0.0(1y ago)15PHPPHP &gt;=8.1

Since Nov 10Pushed 1y ago1 watchersCompare

[ Source](https://github.com/m1n64/vernam-cipher)[ Packagist](https://packagist.org/packages/m1n64/vernam-cipher)[ RSS](/packages/m1n64-vernam-cipher/feed)WikiDiscussions main Synced yesterday

READMEChangelog (1)Dependencies (1)Versions (2)Used By (0)

Vernam Cipher Implementation
============================

[](#vernam-cipher-implementation)

Description
-----------

[](#description)

The [VernamCipher](https://en.wikipedia.org/wiki/One-time_pad) package provides a simple and secure implementation of the Vernam cipher, also known as the one-time pad cipher. This encryption method requires a key that is the same length as the plaintext, ensuring maximum security. The key is applied using the XOR operation on each character of the plaintext. Read more about this cipher [this](https://en.wikipedia.org/wiki/One-time_pad).

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

[](#installation)

Make sure you have PHP version 8.1 or higher installed. You can install this package via Composer:

```
composer require m1n64/vernam-cipher
```

Usage
-----

[](#usage)

### Example

[](#example)

```
require 'vendor/autoload.php';

use Vasqo\VernamCipher\Vernam;

$vernam = new Vernam();

// Encrypt a message with a key
$plaintext = "Hello world!";
$key = $vernam->generateKeyFromPlaintext($plaintext); // Generates a key of the same length

$encrypted = $vernam->encrypt($plaintext, $key);
echo "Encrypted: " . $encrypted . "\n";

// Decrypt the message with the same key
$decrypted = $vernam->decrypt($encrypted, $key);
echo "Decrypted: " . $decrypted . "\n";

// Base64 encoding for encrypted data
$encryptedBase64 = $vernam->encryptBase64($plaintext, $key);
echo "Encrypted (Base64): " . $encryptedBase64 . "\n";

// Decrypt from Base64 encoding
$decryptedBase64 = $vernam->decryptBase64($encryptedBase64, $key);
echo "Decrypted (Base64): " . $decryptedBase64 . "\n";
```

### Example with Facade Usage

[](#example-with-facade-usage)

For a more concise and elegant approach, you can use the VernamFacade. The facade allows you to call the same methods as the `Vernam` class without needing to instantiate it directly.

```
