MySQL - Datenbankinhalte mit AES verschlüsseln und auslesen


<?php

// Datenbankinhalte mit AES verschlüsseln und auslesen

/*
CREATE TABLE `secrets` (
 `id` INT(11) NOT NULL AUTO_INCREMENT,
 `content` LONGBLOB NULL,
 PRIMARY KEY (`id`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB;
*/

$key "mCgCizeXzYrXXTvxrNmrIsKO8vdaFs3ZzTo7UcQ8mr+Q/6KIpJDMdgeKH57weCqLH5vJ6aquAuI=";

$db = new PDO('mysql:host=localhost;dbname=test;charset=utf8''root'''); 

if (
$_SERVER["REQUEST_METHOD"] == "POST") {
 
$insert $db->prepare("INSERT INTO `secrets` (`content`) VALUES (AES_ENCRYPT(:content, :key));");
 
$insert->bindValue(':content'$_POST["content"]);
 
$insert->bindValue(':key'$key);
 
$insert->execute();
}

$select $db->prepare("SELECT AES_DECRYPT(`content`, :key) AS `decrypt` FROM `secrets`");
$select->bindValue(':key'$key);
$select->execute();
$contents $select->fetchAll();

$output '';
foreach (
$contents as $content) {
 
$output .= '<p>' $content["decrypt"] . '</p>';
}
?>
<!DOCTYPE html>
<html lang="de">
 <head>
  <meta charset="UTF-8">
  <title>Datenbankinhalte mit AES verschlüsseln und auslesen</title>
 </head>
<body>

<form method="post">
<label>Content:<br>
 <textarea name="content" required="required"></textarea></label><br>
 <input type="submit" value="Submit">
</form>

<?=$output;?>

</body>
</html>

Informationen zum verschlüsseln mit AES unter: agile-coding.net

Bausteine  Alle Anzeigen

Eine zufällige Auswahl von Codeschnipseln aus den Bereichen HTML, CSS, PHP, JavaScript und MySQL.

<th> Tabellenkopf

CSS - CSS-Variablen

PHP - Zufallszahl anzeigen

JavaScript - Individuelle Fehlermeldung ausgeben

MySQL - Suchen unter Berücksichtigung von Groß- und Kleinschreibung