|
| 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 | +?> |
0 commit comments