Skip to content

Commit faafbc1

Browse files
authored
feat: release 1.0
0 parents  commit faafbc1

File tree

5 files changed

+256
-0
lines changed

5 files changed

+256
-0
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 Wildy Sheverando
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# SEncrypt
2+
SEncrypt is a secure & simple encryption library using SHA-512, AES-256-CBC, and BASE64. It provides easy-to-use methods for encrypting and decrypting data securely.
3+
4+
## Installation
5+
```bash
6+
composer require shiwildy/sencrypt
7+
```
8+
9+
## Example
10+
```php
11+
<?php
12+
require 'vendor/autoload.php';
13+
use ShiWildy\SEncrypt;
14+
15+
$plaintext = "Hello, just testing..";
16+
$password = "secret";
17+
18+
try {
19+
$encrypted = SEncrypt::encrypt($plaintext, $password);
20+
echo "Encrypted: " . $encrypted . "\n\n";
21+
22+
$decrypted = SEncrypt::decrypt($encrypted, $password);
23+
echo "Decrypted: " . $decrypted . "\n\n";
24+
25+
} catch (Exception $e) {
26+
echo "An error occurred: " . $e->getMessage() . "\n";
27+
}
28+
?>
29+
```
30+
31+
## How It Works ?
32+
### Encryption:
33+
- Salt Generation: A random salt generated to enhance security.
34+
- Key Derivation: Encryption key is derived from the provided password and generated salt using PBKDF2 Algoritm with SHA-512
35+
- IV Generation: A random initialization vector [IV] generated for use on AES-256-CBC
36+
- Combining Data: Salt, IV, and encrypted data are concatenated and then encoded using base64 to create final encrypted output.
37+
38+
### Decryption:
39+
- Base64 Decode: Encrypted data is first decoded from Base64.
40+
- Data Extraction: salt, IV, and encrypted text are extracted from decoded data.
41+
- Key Derivation: Decryption key is derived using same method in encryption.
42+
- Decryption: Encrypted data is decrypted using derived key and IV.
43+
- Output: Decrypted text returned.
44+
45+
## Contributing
46+
Contributions are welcome! Please open an issue or submit a pull request for any enhancements or bug fixes.
47+
48+
## License
49+
This project licensed under The MIT License
50+
51+
## Credits
52+
- https://www.php.net/manual/en/function.hash-pbkdf2.php
53+
- https://www.tutorialspoint.com/php/php_function_hash_pdkdf2.htm
54+
- https://www.php.net/manual/en/function.openssl-pbkdf2.php
55+
- https://stackoverflow.com/questions/12766852/pbkdf2-password-hashing-for-php
56+
- https://ppgia.pucpr.br/pt/arquivos/techdocs/php/function.hash-pbkdf2.html
57+
- https://nishothan-17.medium.com/pbkdf2-hashing-algorithm-841d5cc9178d

composer.json

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"name": "shiwildy/sencrypt",
3+
"description": "A secure and simple encryption library using SHA-512, AES-256-CBC, and BASE64.",
4+
"license": "MIT",
5+
"keywords": [
6+
"encryption",
7+
"encrypt",
8+
"sha",
9+
"sha512",
10+
"cryptography",
11+
"aes",
12+
"openssl",
13+
"crypto",
14+
"encryptor"
15+
],
16+
"type": "library",
17+
"require": {
18+
"php": ">=7.4"
19+
},
20+
"autoload": {
21+
"psr-4": {
22+
"ShiWildy\\": "src/"
23+
}
24+
},
25+
"authors": [
26+
{
27+
"name": "Wildy Sheverando",
28+
"email": "[email protected]",
29+
"homepage": "https://github.com/shiwildy.git"
30+
}
31+
],
32+
"minimum-stability": "stable",
33+
"prefer-stable": true,
34+
"version": "1.0"
35+
}

src/SEncrypt.php

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
<?php
2+
3+
//
4+
// SEncrypt
5+
// A secure and simple encryption library using SHA-512, AES-256-CBC, and BASE64.
6+
//
7+
// Author : Wildy Sheverando <[email protected]>
8+
// Version : 1.0
9+
//
10+
// https://github.com/shiwildy/SEncrypt.git
11+
//
12+
// This project Licensed under The MIT License.
13+
//
14+
15+
namespace ShiWildy;
16+
17+
class SEncrypt {
18+
private const CIPHER = 'aes-256-cbc';
19+
private const PBKDF2_ITERATIONS = 1000;
20+
private const KEY_LENGTH = 32; // 256-bit key for AES-256
21+
private const IV_LENGTH = 16; // 128-bit IV Length for AES-256-CBC
22+
23+
/**
24+
* Encrypts a plaintext string using AES-256-CBC with a derived key.
25+
*
26+
* @param string $plaintext The data to encrypt.
27+
* @param string $password The password to derive encryption keys.
28+
* @return string Base64-encoded encrypted data.
29+
* @throws Exception If encryption fails.
30+
*/
31+
public static function encrypt(string $plaintext, string $password): string {
32+
try {
33+
// Generate a random salt
34+
$salt = random_bytes(self::KEY_LENGTH);
35+
36+
// Derive encryption key
37+
$key = hash_pbkdf2(
38+
'sha512',
39+
$password,
40+
$salt,
41+
self::PBKDF2_ITERATIONS,
42+
self::KEY_LENGTH,
43+
true
44+
);
45+
46+
// Generate a random IV key
47+
$iv = random_bytes(self::IV_LENGTH);
48+
49+
// Encrypt using openssl_encrypt functions
50+
$encrypted = openssl_encrypt(
51+
$plaintext,
52+
self::CIPHER,
53+
$key,
54+
OPENSSL_RAW_DATA,
55+
$iv
56+
);
57+
58+
if ($encrypted === false) {
59+
throw new \Exception('Encryption failed.');
60+
}
61+
62+
// Combine salt, iv, encrypted then base64 encode
63+
return base64_encode($salt . $iv . $encrypted);
64+
65+
} catch (Exception $e) {
66+
throw new \Exception($e->getMessage());
67+
}
68+
}
69+
70+
/**
71+
* Decrypts a Base64-encoded encrypted string using AES-256-CBC with a derived key.
72+
*
73+
* @param string $encryptedBase64 The Base64-encoded encrypted data.
74+
* @param string $password The password to derive decryption keys.
75+
* @return string The decrypted plaintext.
76+
* @throws Exception If decryption fails.
77+
*/
78+
public static function decrypt(string $encryptedBase64, string $password): string {
79+
try {
80+
// Decode base64
81+
$combined = base64_decode($encryptedBase64, true);
82+
if ($combined === false) {
83+
throw new \Exception('Cannot decode base64.');
84+
}
85+
86+
// Extract salt, iv, encrypted from combined
87+
$salt = substr($combined, 0, self::KEY_LENGTH);
88+
$iv = substr($combined, self::KEY_LENGTH, self::IV_LENGTH);
89+
$ciphertext = substr($combined, self::KEY_LENGTH + self::IV_LENGTH);
90+
91+
if ($salt === false || $iv === false || $ciphertext === false) {
92+
throw new \Exception('Encrypted format is invalid.');
93+
}
94+
95+
// Derive decryption key
96+
$key = hash_pbkdf2(
97+
'sha512',
98+
$password,
99+
$salt,
100+
self::PBKDF2_ITERATIONS,
101+
self::KEY_LENGTH,
102+
true
103+
);
104+
105+
// Decrypt encrypted text
106+
$decrypted = openssl_decrypt(
107+
$ciphertext,
108+
self::CIPHER,
109+
$key,
110+
OPENSSL_RAW_DATA,
111+
$iv
112+
);
113+
114+
if ($decrypted === false) {
115+
throw new \Exception('Decryption failed.');
116+
}
117+
118+
return $decrypted;
119+
} catch (Exception $e) {
120+
throw new \Exception($e->getMessage());
121+
}
122+
}
123+
}
124+
125+
?>

test.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
include "src/sencrypt.php";
3+
4+
$plaintext = "Hello, just testing..";
5+
$password = "secret";
6+
7+
try {
8+
$encrypted = SEncrypt::encrypt($plaintext, $password);
9+
echo "Encrypted: " . $encrypted . "\n\n";
10+
11+
$decrypted = SEncrypt::decrypt($encrypted, $password);
12+
echo "Decrypted: " . $decrypted . "\n\n";
13+
14+
} catch (Exception $e) {
15+
echo "An error occurred: " . $e->getMessage() . "\n";
16+
}
17+
18+
?>

0 commit comments

Comments
 (0)