First, generate your keys. Create genkeys.php and upload to root


<?php
echo base64_encode(openssl_random_pseudo_bytes(32)); // first key
echo base64_encode(openssl_random_pseudo_bytes(64)); // second key


Get to yourtracker.com/genkeys.php

Open /backend/config.php and add your keys


define('key_primary', 'YOUR_FIRST_GENERATED_KEY');
define('key_secondary', 'YOUR_SECOND_GENERATED_KEY');


Open mailbox-functions.php and add


function encryptMessage(string $data): string
{
   $primary_key      = base64_decode(key_primary);
   $secondary_key    = base64_decode(key_secondary);
   $method           = "AES-256-CBC";
   $iv_length        = openssl_cipher_iv_length($method);
   $iv               = openssl_random_pseudo_bytes($iv_length);
   $first_encrypted  = openssl_encrypt($data, $method, $primary_key, OPENSSL_RAW_DATA, $iv);
   $second_encrypted = hash_hmac('SHA3-512', $first_encrypted, $secondary_key, true);
   $output           = base64_encode($iv . $second_encrypted . $first_encrypted);
   return $output;
}
function decryptMessage(string $input): mixed
{
   $first_key            = base64_decode(key_primary);
   $second_key           = base64_decode(key_secondary);
   $mix                  = base64_decode($input);
   $method               = "AES-256-CBC";
   $iv_length            = openssl_cipher_iv_length($method);
   $iv                   = substr($mix, 0, $iv_length);
   $second_encrypted     = substr($mix, $iv_length, 64);
   $first_encrypted      = substr($mix, $iv_length + 64);
   $data                 = openssl_decrypt($first_encrypted, $method, $first_key, OPENSSL_RAW_DATA, $iv);
   $second_encrypted_new = hash_hmac('SHA3-512', $first_encrypted, $second_key, true);
   if (hash_equals($second_encrypted, $second_encrypted_new))
       return $data;
   return false;
}


Open mailbox.php and find


$display = "<div>".format_comment($arr['msg'])."<br /><br />";


Replace with


$display = $arr['encrypted'] === 'yes' ? "<div>".format_comment(decryptMessage($arr['msg']))."<br /><br />" : "<div>".format_comment($arr['msg'])."<br /><br />";


Find


SQL_Query_exec("INSERT INTO `messages` (`sender`, `receiver`, `added`, `subject`, `msg`, `unread`, `location`) VALUES ('$CURUSER[id]', '$sendto', '".get_date_time()."', $subject, $msg, '$status', '$to')") or die("Aargh!");


Replace with


$encrypted = (isset($_POST['encrypt']) && intval($_POST['encrypt']) === 1) ? 'yes' : 'no';
$msg = (isset($_POST['encrypt']) && intval($_POST['encrypt']) === 1) ? encryptMessage($msg) : $msg;
$subject = (isset($_POST['encrypt']) && intval($_POST['encrypt']) === 1) ? encryptMessage($subject) : $subject;
SQL_Query_exec("INSERT INTO `messages` (`encrypted`, `sender`, `receiver`, `added`, `subject`, `msg`, `unread`, `location`) VALUES ('$encrypted', '$CURUSER[id]', '$sendto', '".get_date_time()."', $subject, $msg, '$status', '$to')") or die("Aargh!");


Find


if (!isset($_GET['templates'])) $output .= "<input type=\"submit\" name=\"send\" value=\"Send\" /> <label><input type=\"checkbox\" name=\"save\" checked='checked' />Save Copy In Outbox</label>    <input type=\"submit\" name=\"draft\" value=\"Save Draft\" /> ";


Replace with


if (!isset($_GET['templates'])) $output .= "<input type=\"submit\" name=\"send\" value=\"Send\" /> <label><input type=\"radio\" name=\"encrypt\" value="1" />Encrypt Message <input type=\"checkbox\" name=\"save\" checked='checked' />Save Copy In Outbox</label> <input type=\"submit\" name=\"draft\" value=\"Save Draft\" /> ";


Finally, run SQL


ALTER TABLE messages ADD COLUMN encrypted ENUM('yes','no') NULL DEFAULT 'no';

Last edited by STorrents2020 on 2022-11-28 17:52:03