So I'm working on encrypting all data being sent to the database. It's sending everything to the database encrypted using the key, but when I go to encrypt it and display it using the key, nothing shows up. Can someone point me in the right direction, please? Here is my code. The password for encryption/decryption is in the init.php document.
require_once 'init.php';
$itemsQuery = $db->prepare("SELECT id, name, done FROM tasks WHERE user = :user AND folder = :folder");
$itemsQuery->execute([
'user' => $_SESSION['user_id'],
'folder' => "inbox"
]);
$method = 'aes-256-cbc';
$key = substr(hash('sha256', $password, true), 0, 32);
$iv = chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0);
$decrypted = openssl_decrypt(base64_decode($itemsQuery), $method, $key, OPENSSL_RAW_DATA, $iv);$items = $decrypted->rowCount() ? $decrypted : [];
This is being displayed out in the table using the $items tag.
This is the code that is echoing out the supposed decrypted text:
<ul id="myul" class="items">
<?php foreach($items as $item): ?><?php if (!$item['done']):?><li><a href="functions.php?as=inboxdone&item=<?php echo $item['id'] ?>" class="done-button"><span class="dot"></span></a> <div class="task-dropdown"><!--Button to show more in the task dropdown--><button class="task-dropbtn"><!--More image--><img src="assets/images/more-707070.svg" class="more"></button><div class="task-dropdown-content"><!--Personal move--><a href="functions.php?as=inboxtopersonal&item=<?php echo $item['id'] ?>" class="done-button"><img src="assets/images/user-active.svg" id="bookmark" height="15px" width="15px"></a><!--Work move--><a href="functions.php?as=inboxtowork&item=<?php echo $item['id'] ?>" class="done-button"><img src="assets/images/work-active.svg" id="bookmark" height="15px" width="15px"></a><!--Bookmark--><a href="functions.php?as=inboxbookmark&item=<?php echo $item['id'] ?>" class="done-button"><img src="assets/images/bookmark-active.svg" id="bookmark" height="15px" width="15px"></a><!--Delete task--><a href="functions.php?as=inboxdelete&item=<?php echo $item['id'] ?>" class="done-button"><img src="assets/images/trash-warn.svg" id="bookmark" height="15px" width="15px"></a></div></div> <span class="item<?php echo $item['done'] ? 'done' : '' ?>"><?php echo $item['name']; ?></span></li><?php endif; ?><?php endforeach; ?>
</ul>