Chiffrement AES-256 texte...

Résolu/Fermé
Snox5 Messages postés 1475 Date d'inscription samedi 25 juin 2016 Statut Contributeur Dernière intervention 11 juin 2021 - Modifié par Snox5 le 26/02/2017 à 13:16
Snox5 Messages postés 1475 Date d'inscription samedi 25 juin 2016 Statut Contributeur Dernière intervention 11 juin 2021 - 27 févr. 2017 à 09:08
Salut,

Je cherche une manière de chiffrer (pas hasher) un texte dans un script PHP.
J'aimerai le faire avec un fonction perso, qui chiffre une chaine de caractère avec l’algorithme AES-256 avec une clé rentrée par un utilisateur dans un formulaire précédemment, avant de stocker le texte...

contexte : un pastebin perso, je veut chiffrer un texte dans une variable, afin de crypter le contenu. Le code à ajouter pour ce faire, sera je présume, à la ligne 35.

Les variables chaine de caractères à chiffrer sont
$text
et
$title

La clé rentrée par l'utilisateur est la variable :
$pass


Je débute en PHP, donc je ne sais pas trop comment m'y prendre... :-)

Mon jolie script actuel :
<?php
////CONNEXION MYSQL >>>
$dbhost = 'mysql:dbname=paste;host=localhost;';
$user = 'root';
$bddpass = '';
$bdd = new PDO($dbhost, $user, $bddpass);
$bdd->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$bdd->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);

$title = isset($_POST['title']) ? $_POST['title'] : NULL;
$text = isset($_POST['paste']) ? $_POST['paste'] : NULL;
$pass = isset($_POST['password']) ? $_POST['password'] : NULL;

if (!empty($title && $text)) {
/*Anti-HTML tags/XSS HTML*/
$tag = array("<",">","/",'\'');
/*$htmlentities = array("<",">","/","\"); bug CCM... normalement non-commenté*/
$cleantext = str_replace($tag,$htmlentities,$text);
$cleantitle = str_replace($tag,$htmlentities,$title);
$text = $cleantext;
$title = $cleantitle;
/* GENERATION D'UN FICHIER TXT */
function generateRandomString($length = 10) {
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $charactersLength = strlen($characters);
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[rand(0, $charactersLength - 1)];
    }
    return $randomString;
}
$randomstr = generateRandomString();
/** mot de passe **/
if (!empty($pass)) {
  /* code pour crypter le mdp si mot de passe entrée dans le formulaire précédement */
}
$file = fopen('txt/'.$randomstr.'.txt', 'w+');
fputs($file, $text);
fclose($file);
/* chemin d'accès du fichier*/
$path = 'txt/' . $randomstr . '.txt';

/* STOCKAGE DB CHEMIN D'ACCES / NOM ($title) / SUFFIX DU LIEN */
$sql = "INSERT INTO paste(suffix, pathfile, title) VALUES(:randomstr, :path , :title)";
$data = array(":randomstr"=>$randomstr , ":path"=>$path , ":title"=>$title);
$prep = $bdd->prepare($sql);
$res = $prep->execute($data);

header ('Location: template.php?paste='.$randomstr.'');
} else {
 header ('Location: index.php?r=error');
}
?>

Merci à vous !
+++
Deux choses sont infinies : l’Univers et la version d'essai de WinRAR. Mais, en ce qui concerne l’Univers, je n’en ai pas encore acquis la certitude absolue.

1 réponse

zucrezel Messages postés 30 Date d'inscription mardi 7 février 2017 Statut Membre Dernière intervention 19 juillet 2017 4
26 févr. 2017 à 13:40
Bonjour,

Regardes les lignes 1 à 26 dans cet exemple :
http://blog.turret.io/the-missing-php-aes-encryption-example/
2
Snox5 Messages postés 1475 Date d'inscription samedi 25 juin 2016 Statut Contributeur Dernière intervention 11 juin 2021 213
27 févr. 2017 à 09:08
Ça m'a bien aidé, j'ai utilisé openSSL ça à fonctionné.
Merci.
0