44[ ![ CI] ( https://github.com/Rene-Roscher/laravel-node-encryption/actions/workflows/ci.yml/badge.svg )] ( https://github.com/Rene-Roscher/laravel-node-encryption/actions/workflows/ci.yml )
55[ ![ License: MIT] ( https://img.shields.io/badge/License-MIT-yellow.svg )] ( https://opensource.org/licenses/MIT )
66
7- Laravel-compatible encryption/decryption for Node.js. Fully compatible with Laravel's built-in encryption.
7+ ** 🔐 Bidirectional encryption between Laravel (PHP) and Node.js**
8+
9+ Share encrypted data seamlessly between your Laravel backend and Node.js services. Encrypt in Laravel, decrypt in Node.js - or vice versa!
10+
11+ ## Why This Package?
12+
13+ Perfect for microservices, APIs, and hybrid applications where you need to:
14+ - 🔄 ** Share encrypted data** between Laravel and Node.js applications
15+ - 🔒 ** Encrypt in Laravel** , decrypt in Node.js (and vice versa!)
16+ - 🚀 ** Build microservices** that share encrypted tokens, sessions, or sensitive data
17+ - 🛡️ ** Maintain security** across different technology stacks
818
919## Features
1020
11- - ✅ 100% Laravel compatible (8.x - 12.x)
12- - ✅ Zero configuration (auto-detects ` APP_KEY ` )
13- - ✅ AES-256-CBC encryption with HMAC-SHA256
14- - ✅ No dependencies (optional ` php-serialize ` for complex objects)
21+ - ✅ ** Fully bidirectional** - Encrypt/decrypt in both directions
22+ - ✅ ** 100% Laravel compatible** (8.x - 12.x)
23+ - ✅ ** Zero configuration** - Auto-detects ` APP_KEY `
24+ - ✅ ** Production ready** - Battle-tested AES-256-CBC with HMAC-SHA256
25+ - ✅ ** No dependencies** - Lightweight with optional ` php-serialize `
1526
1627## Installation
1728
@@ -48,29 +59,44 @@ const decrypted = encrypter.decrypt(laravelEncryptedString);
4859- ` encryptString(value) ` - Encrypts string without serialization
4960- ` decryptString(payload) ` - Decrypts string without deserialization
5061
51- ## Laravel Integration
62+ ## Bidirectional Encryption Examples
5263
53- ### PHP ( Laravel)
64+ ### 🔄 Laravel → Node.js
5465``` php
66+ // Laravel: Encrypt data
5567use Illuminate\Support\Facades\Crypt;
5668
57- // Encrypt
58- $encrypted = Crypt::encrypt('Hello from Laravel!' );
69+ $userData = ['id' => 1, 'email' => '[email protected] ']; 70+ $encrypted = Crypt::encrypt($userData );
5971
60- // Decrypt from Node.js
61- $decrypted = Crypt::decrypt($fromNodeJs);
72+ // Send $encrypted to Node.js service...
6273```
6374
64- ### Node.js
6575``` javascript
76+ // Node.js: Decrypt data from Laravel
6677const { LaravelEncrypter } = require (' laravel-node-encryption' );
6778const encrypter = new LaravelEncrypter ();
6879
69- // Decrypt from Laravel
70- const decrypted = encrypter .decrypt (fromLaravel);
80+ const userData = encrypter .decrypt (encryptedFromLaravel);
81+ console .
log (userData);
// { id: 1, email: '[email protected] ' }82+ ```
83+
84+ ### 🔄 Node.js → Laravel
85+ ``` javascript
86+ // Node.js: Encrypt data
87+ const encrypter = new LaravelEncrypter ();
88+ const token = { userId: 1 , expires: ' 2024-12-31' };
89+ const encrypted = encrypter .encrypt (token);
90+
91+ // Send encrypted to Laravel...
92+ ```
93+
94+ ``` php
95+ // Laravel: Decrypt data from Node.js
96+ use Illuminate\Support\Facades\Crypt;
7197
72- // Encrypt for Laravel
73- const encrypted = encrypter . encrypt ( ' Hello from Node.js! ' );
98+ $token = Crypt::decrypt($encryptedFromNode);
99+ // $token = ['userId' => 1, 'expires' => '2024-12-31']
74100```
75101
76102## Environment Setup
@@ -86,6 +112,32 @@ Or in `.env`:
86112APP_KEY=base64:your-app-key-here
87113```
88114
115+ ## Real-World Use Cases
116+
117+ ### 🔐 Secure API Tokens
118+ Share authentication tokens between Laravel API and Node.js microservices:
119+ ``` javascript
120+ // Node.js: Create encrypted token
121+ const token = encrypter .encrypt ({ userId: 123 , scope: ' api' });
122+ // Laravel can decrypt and validate this token
123+ ```
124+
125+ ### 🍪 Cross-Platform Sessions
126+ Share session data between Laravel web app and Node.js real-time service:
127+ ``` php
128+ // Laravel: Encrypt session
129+ $sessionData = Crypt::encrypt(session()->all());
130+ // Node.js WebSocket server can decrypt and use session
131+ ```
132+
133+ ### 📧 Queue Messages
134+ Encrypt sensitive job payloads between Laravel and Node.js workers:
135+ ``` javascript
136+ // Node.js: Encrypt job payload
137+ const job = encrypter .
encrypt ({ email
: ' [email protected] ' , action
: ' welcome' });
138+ // Laravel queue worker decrypts and processes
139+ ```
140+
89141## Advanced Usage
90142
91143### With explicit key
0 commit comments