PHPackages                             al-saloul/encryption - 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. al-saloul/encryption

ActiveLibrary[Security](/categories/security)

al-saloul/encryption
====================

Simple number encryption and decryption package

v1.2.0(1y ago)191.1k4MITPHP

Since Jan 15Pushed 1y ago1 watchersCompare

[ Source](https://github.com/al-saloul/encryption-number)[ Packagist](https://packagist.org/packages/al-saloul/encryption)[ RSS](/packages/al-saloul-encryption/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)DependenciesVersions (4)Used By (0)

**Laravel Number Encryption Package**

License: MIT

---

**Overview**

The **Laravel Number Encryption Package** provides robust encryption and decryption functionality for numeric values within Laravel applications. Building upon simple number-to-string mappings, this package offers enhanced security features, configurability, and comprehensive error handling to ensure reliable and secure operations.

---

**Features**

- **Encrypt Numbers**: Transform numeric values into non-readable, encrypted strings.
- **Decrypt Numbers**: Revert encrypted strings back to their original numeric form.
- **Cryptographically Secure Randomness**: Utilizes `random_int()` for secure random string generation.
- **Configurable Padding Lengths**: Customize the length of prefix and suffix padding added during encryption.
- **Input Validation**: Ensures that only valid numeric inputs are processed.
- **Customizable Mappings**: Modify digit-to-character mappings to suit specific requirements.
- **Enhanced Error Handling**: Provides descriptive exceptions for easier debugging and reliability.
- **Logging Capability**: Integrate with Laravel's logging system to monitor encryption and decryption processes.
- **Utility Methods**: Includes methods to verify if a string is encrypted and to customize encryption settings.

---

**Installation**

1. **Install via Composer**

    Run the following command in your terminal to install the package:

    ```
    composer require al-saloul/encryption

    ```

---

**Usage**

1. **Using the Encryption Class**

    **Using Helper Functions**

    For quick and straightforward usage, helper functions are available globally throughout your Laravel project after installing the package.

    ```
     // Encrypt a number using the helper function
     $encrypted = encrypt_numbers(12345);
     // Output: "UQ9rovS*(Yr5inVd"

     // Check if the encrypted string is indeed encrypted
     $isEncrypted = is_encrypted($encrypted);
     // Output: true

     // Decrypt the encrypted string using the helper function
     $decrypted = decrypt_numbers($encrypted);
     // Output: 12345

     // Output results
     echo "Encrypted: " . $encrypted . PHP_EOL;
     echo "Is Encrypted: " . ($isEncrypted ? 'Yes' : 'No') . PHP_EOL;
     echo "Decrypted: " . $decrypted . PHP_EOL;
    ```

    *Note: Ensure that the helper functions are properly registered and autoloaded in your Laravel application.*

    **Using Encryption Class**

    Leverage the `Encryption` class to encrypt and decrypt numeric values seamlessly.

    ```
    use Alsaloul\Encryption\Encryption;
    use Illuminate\Validation\ValidationException;

    try {
        $number = 1234567890;

        // Encrypt the number
        $encrypted = Encryption::encryptNumbers($number);
        echo "Encrypted: " . $encrypted . PHP_EOL;

        // Decrypt the encrypted string
        $decrypted = Encryption::decryptNumbers($encrypted);
        echo "Decrypted: " . $decrypted . PHP_EOL;

        // Check if a string is encrypted
        $isEncrypted = Encryption::isEncrypted($encrypted);
        echo "Is Encrypted: " . ($isEncrypted ? 'Yes' : 'No') . PHP_EOL;

    } catch (ValidationException $e) {
        // Handle decryption errors
        echo "Decryption failed: " . $e->getMessage() . PHP_EOL;
    } catch (\Exception $e) {
        // Handle other exceptions
        echo "An error occurred: " . $e->getMessage() . PHP_EOL;
    }
    ```
2. **Configuring Padding Lengths**

    Customize the prefix and suffix padding lengths to enhance security or meet specific requirements.

    ```
    use Alsaloul\Encryption\Encryption;

    // Set custom padding lengths
    Encryption::setPaddingLengths(10, 5);

    // Now, encryption will use a prefix of 10 characters and a suffix of 5 characters
    $encrypted = Encryption::encryptNumbers(12345);
    ```
3. **Customizing Variable Mappings**

    Modify the digit-to-character mappings to define your own encryption patterns.

    ```
    use Alsaloul\Encryption\Encryption;

    // Define custom mappings
    $customMappings = [
        "1" => "A1B2C3",
        "2" => "D4E5F6",
        "3" => "G7H8I9",
        "4" => "J0K1L2",
        "5" => "M3N4O5",
        "6" => "P6Q7R8",
        "7" => "S9T0U1",
        "8" => "V2W3X4",
        "9" => "Y5Z6A7",
        "0" => "B8C9D0",
    ];

    // Apply custom mappings
    Encryption::setVars($customMappings);

    // Encrypt and decrypt using custom mappings
    $encrypted = Encryption::encryptNumbers(67890);
    $decrypted = Encryption::decryptNumbers($encrypted);
    ```
4. **Integrating Logging**

    To enable logging of encryption and decryption processes, inject a `LoggerInterface` instance into the `Encryption` class.

    ```
    use Alsaloul\Encryption\Encryption;
    use Monolog\Logger;
    use Monolog\Handler\StreamHandler;

    // Set up logger (using Monolog as an example)
    $logger = new Logger('encryption_logger');
    $logger->pushHandler(new StreamHandler(storage_path('logs/encryption.log'), Logger::INFO));

    // Instantiate the Encryption class with the logger
    $encryption = new Encryption($logger);

    try {
        $number = 9876543210;

        // Encrypt the number
        $encrypted = $encryption->encryptNumbers($number);
        echo "Encrypted: " . $encrypted . PHP_EOL;

        // Decrypt the encrypted string
        $decrypted = $encryption->decryptNumbers($encrypted);
        echo "Decrypted: " . $decrypted . PHP_EOL;

    } catch (\Exception $e) {
        // Handle exceptions
        echo "An error occurred: " . $e->getMessage() . PHP_EOL;
    }
    ```

    *Note: Logging is optional and can be enabled as needed. Ensure that logging sensitive information complies with your application's security policies.*

---

**Methods**

*Encryption Class Methods*

- **encryptNumbers(int|string $value): string**

    Encrypts the input numeric value and returns an encrypted string.

    ```
    $encrypted = Encryption::encryptNumbers(12345);
    ```
- **decryptNumbers(string $value): string**

    Decrypts the encrypted string and returns the original numeric value.

    ```
    $decrypted = Encryption::decryptNumbers($encrypted);
    ```
- **setPaddingLengths(int $prefixLength, int $suffixLength): void**

    Sets custom lengths for prefix and suffix padding used during encryption.

    ```
    Encryption::setPaddingLengths(10, 5);
    ```
- **setVars(array $vars): void**

    Customizes the digit-to-character mappings for encryption.

    ```
    Encryption::setVars($customMappings);
    ```
- **isEncrypted(string $value): bool**

    Checks if a given string follows the encryption format.

    ```
    $isEncrypted = Encryption::isEncrypted($encryptedString);
    ```

---

**Usage Example**

Here’s a comprehensive example demonstrating various features of the package:

```
use Alsaloul\Encryption\Encryption;
use Illuminate\Validation\ValidationException;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;

// Set up logger
$logger = new Logger('encryption_logger');
$logger->pushHandler(new StreamHandler(storage_path('logs/encryption.log'), Logger::INFO));

// Instantiate the Encryption class with the logger
$encryption = new Encryption($logger);

try {
    $number = 1234567890;

    // Encrypt the number
    $encrypted = $encryption->encryptNumbers($number);
    echo "Encrypted: " . $encrypted . PHP_EOL;

    // Decrypt the encrypted string
    $decrypted = $encryption->decryptNumbers($encrypted);
    echo "Decrypted: " . $decrypted . PHP_EOL;

    // Check if a string is encrypted
    $isEncrypted = Encryption::isEncrypted($encrypted);
    echo "Is Encrypted: " . ($isEncrypted ? 'Yes' : 'No') . PHP_EOL;

    // Set custom padding lengths
    Encryption::setPaddingLengths(10, 5);

    // Encrypt with new padding
    $encryptedWithCustomPadding = Encryption::encryptNumbers($number);
    echo "Encrypted with Custom Padding: " . $encryptedWithCustomPadding . PHP_EOL;

    // Set custom variable mappings
    $customMappings = [
        "1" => "A1B2C3",
        "2" => "D4E5F6",
        "3" => "G7H8I9",
        "4" => "J0K1L2",
        "5" => "M3N4O5",
        "6" => "P6Q7R8",
        "7" => "S9T0U1",
        "8" => "V2W3X4",
        "9" => "Y5Z6A7",
        "0" => "B8C9D0",
    ];
    Encryption::setVars($customMappings);

    // Encrypt and decrypt with custom mappings
    $customEncrypted = Encryption::encryptNumbers(67890);
    echo "Custom Encrypted: " . $customEncrypted . PHP_EOL;

    $customDecrypted = Encryption::decryptNumbers($customEncrypted);
    echo "Custom Decrypted: " . $customDecrypted . PHP_EOL;

} catch (ValidationException $e) {
    // Handle decryption errors
    echo "Decryption failed: " . $e->getMessage() . PHP_EOL;
} catch (\Exception $e) {
    // Handle other exceptions
    echo "An error occurred: " . $e->getMessage() . PHP_EOL;
}
```

---

**License**

This package is open-sourced software licensed under the MIT License.

---

**Contributing**

Contributions are welcome! Please follow these steps:

1. Fork the repository.
2. Create a new branch for your feature or bugfix.
3. Commit your changes with clear messages.
4. Push to your fork and submit a pull request.

---

**Security Considerations**

While this package enhances the security of numeric data through obfuscation, it is **not** intended for encrypting highly sensitive information. For cryptographically secure encryption requirements, consider using established libraries such as OpenSSL or Sodium.

---

**Support**

If you encounter any issues or have questions, please open an issue on the GitHub repository:

---

**Changelog**

Please see the CHANGELOG.md for more information on what has changed recently.

###  Health Score

33

—

LowBetter than 75% of packages

Maintenance42

Moderate activity, may be stable

Popularity30

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity40

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 80% of commits — single point of failure

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

Every ~5 days

Total

3

Last Release

475d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/0a2776dd1dd2c91f8bac075de5cd7c2e9fb7f987083d643c4e3373724b27f542?d=identicon)[al-saloul](/maintainers/al-saloul)

---

Top Contributors

[![al-saloul](https://avatars.githubusercontent.com/u/96193006?v=4)](https://github.com/al-saloul "al-saloul (8 commits)")[![muath-ye](https://avatars.githubusercontent.com/u/34031333?v=4)](https://github.com/muath-ye "muath-ye (2 commits)")

---

Tags

securityencryptionlaravel-packagedecryptionnumber encryption

### Embed Badge

![Health badge](/badges/al-saloul-encryption/health.svg)

```
[![Health](https://phpackages.com/badges/al-saloul-encryption/health.svg)](https://phpackages.com/packages/al-saloul-encryption)
```

###  Alternatives

[phpseclib/phpseclib

PHP Secure Communications Library - Pure-PHP implementations of RSA, AES, SSH2, SFTP, X.509 etc.

5.6k434.8M1.3k](/packages/phpseclib-phpseclib)[defuse/php-encryption

Secure PHP Encryption Library

3.9k162.4M214](/packages/defuse-php-encryption)[nzo/url-encryptor-bundle

The NzoUrlEncryptorBundle is a Symfony Bundle used to Encrypt and Decrypt data and variables in the Web application or passed through URL

961.0M2](/packages/nzo-url-encryptor-bundle)[ass/xmlsecurity

The XmlSecurity library is written in PHP for working with XML Encryption and Signatures

955.6M30](/packages/ass-xmlsecurity)[tilleuls/url-signer-bundle

Create and validate signed URLs with a limited lifetime in Symfony

81340.1k](/packages/tilleuls-url-signer-bundle)[ercsctt/laravel-file-encryption

Secure file encryption and decryption for Laravel applications

642.6k](/packages/ercsctt-laravel-file-encryption)

PHPackages © 2026

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