Skip to content

Commit 3f5075a

Browse files
authored
Encryption and decryption functions
1 parent 837083b commit 3f5075a

File tree

5 files changed

+120
-14
lines changed

5 files changed

+120
-14
lines changed

config.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,15 @@
8585

8686
define('ENFORCE_SSL', FALSE);
8787

88+
/*
89+
|--------------------------------------------------------------------------
90+
| Configuration for Encryption and Decryption
91+
|--------------------------------------------------------------------------
92+
*/
93+
94+
define('PASS_PHRASE', '12345');
95+
define('CIPHER_METHOD', 'aes-256-cbc');
96+
8897
/*
8998
|--------------------------------------------------------------------------
9099
| Set BASE_URL
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
/**
4+
* In the controller file, you can handle and process variables,
5+
* classes and functions; use if-elseif statements; load models, and
6+
* include files. The variables can then be used in the view file.
7+
*/
8+
9+
class EncryptionController
10+
{
11+
12+
public function index()
13+
{
14+
15+
$page_title = 'Data Encryption';
16+
17+
$data = compact('page_title');
18+
view('encryption', $data);
19+
20+
}
21+
22+
}

functions.php

Lines changed: 63 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,20 @@
77
|
88
| These are core functions necessary to run the nano-framework:
99
|
10-
| 1. url_path() - retrieves the URL path substring separated by '/'
11-
| 2. route_rpc() - JSON-RPC v2.0 compatibility layer
12-
| 3. route_auto() - automatic routing of URL path to Class and method
13-
| 4. route_class() - routes URL path request to Controllers
14-
| 5. view() - passes data and renders the View
15-
| 6. pdo_conn() - PHP Data Objects (PDO) database connection
16-
| 7. api_response() - handles API response
17-
| 8. api_call() - handles API call
18-
| 9. firewall() - web application firewall
19-
| 10. force_ssl() - force application to use SSL
20-
| 11. esc() - uses htmlspecialchars() to prevent XSS
21-
| 12. csrf_token() - uses sessions to create per request CSRF token
10+
| 1. url_path() - retrieves the URL path substring separated by '/'
11+
| 2. route_rpc() - JSON-RPC v2.0 compatibility layer
12+
| 3. route_auto() - automatic routing of URL path to Class and method
13+
| 4. route_class() - routes URL path request to Controllers
14+
| 5. view() - passes data and renders the View
15+
| 6. pdo_conn() - PHP Data Objects (PDO) database connection
16+
| 7. api_response() - handles API response
17+
| 8. api_call() - handles API call
18+
| 9. firewall() - web application firewall
19+
| 10. force_ssl() - force application to use SSL
20+
| 11. esc() - uses htmlspecialchars() to prevent XSS
21+
| 12. csrf_token() - uses sessions to create per request CSRF token
22+
| 13. encrypt() - encrypt data using AES-CBC-HMAC
23+
| 14. decrypt() - decrypt data using AES-CBC-HMAC
2224
|
2325
*/
2426

@@ -355,4 +357,53 @@ function csrf_token()
355357

356358
}
357359

360+
}
361+
362+
/**
363+
* Encrypt data using AES-CBC-HMAC
364+
*
365+
* @param string $plaintext - Plaintext to be encrypted
366+
*/
367+
368+
function encrypt($plaintext)
369+
{
370+
371+
$cipher = CIPHER_METHOD;
372+
$key = hash('sha256', PASS_PHRASE . md5(PASS_PHRASE));
373+
$key_hmac = hash('sha256', md5(PASS_PHRASE));
374+
$iv_len = openssl_cipher_iv_length($cipher);
375+
$iv = random_bytes($iv_len);
376+
377+
$ciphertext = openssl_encrypt($plaintext, $cipher, $key, 0, $iv);
378+
$hash = hash_hmac('sha256', $ciphertext, $key_hmac);
379+
380+
return base64_encode($ciphertext . '::' . $hash . '::' . $iv);
381+
382+
}
383+
384+
/**
385+
* Decrypt data using AES-CBC-HMAC
386+
*
387+
* @param string $encypted - base64_encoded ciphertext, hash and iv
388+
*/
389+
390+
function decrypt($encrypted)
391+
{
392+
393+
if ( ! isset($encrypted) || empty($encrypted) ) { return ''; }
394+
395+
$cipher = CIPHER_METHOD;
396+
$key = hash('sha256', PASS_PHRASE . md5(PASS_PHRASE));
397+
$key_hmac = hash('sha256', md5(PASS_PHRASE));
398+
399+
list($ciphertext, $hash, $iv) = explode('::', base64_decode($encrypted));
400+
$digest = hash_hmac('sha256', $ciphertext, $key_hmac);
401+
402+
if (hash_equals($hash, $digest)) {
403+
return openssl_decrypt($ciphertext, $cipher, $key, 0, $iv);
404+
}
405+
else {
406+
return 'Please verify authenticity of ciphertext.';
407+
}
408+
358409
}

views/encryption.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
// Show Header and Menu
3+
require_once 'template/header.php';
4+
require_once 'template/menu.php';
5+
6+
$plaintext = 'ABC123';
7+
$encrypted = encrypt($plaintext);
8+
$decrypted = decrypt($encrypted);
9+
?>
10+
<!-- Page Content -->
11+
<div class="container">
12+
<div class="row">
13+
<div class="col-lg-12">
14+
<h1 class="mt-5 text-center">Encryption</h1>
15+
<p>The plaintext:<br /><strong><?= $plaintext ?></strong></p>
16+
<p>The encrypted:<br /><strong><?= $encrypted ?></strong></p>
17+
<p>The decrypted:<br /><strong><?= $decrypted ?></strong></p>
18+
</div>
19+
</div>
20+
</div>
21+
<?php
22+
// Show Footer
23+
require_once 'template/footer.php';
24+
?>

views/template/menu.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
<li class="nav-item <?php if (url_path(1) == '') echo 'active'; ?>">
99
<a class="nav-link" href="<?php echo BASE_URL; ?>">Home</a>
1010
</li>
11-
<li class="nav-item <?php if (url_path(1) == 'welcome') echo 'active'; ?>">
12-
<a class="nav-link" href="<?php echo BASE_URL; ?>welcome">Welcome</a>
11+
<li class="nav-item <?php if (url_path(1) == 'encryption') echo 'active'; ?>">
12+
<a class="nav-link" href="<?php echo BASE_URL; ?>encryption">Encryption</a>
1313
</li>
1414
<li class="nav-item <?php if (url_path(1) == 'request') echo 'active'; ?>">
1515
<a class="nav-link" href="<?php echo BASE_URL; ?>request">Request</a>

0 commit comments

Comments
 (0)