1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| function changeBit($input,$current_bit)
{
$lettre_binaire = hexa2bin ($input);
$lettre_binaire[7] = $current_bit;
$lettre_codee = bin2hexa($lettre_binaire);
return $lettre_codee;
}
function hideText($image_file,$text_to_hide)
{
if(!$fichier_in = fopen ($image_file, 'rb')) return 0; // Ouverture de l'image source
if(!$fichier_out = fopen ("temp_file", 'wb')) return 0; // Ouverture de l'image temporaire
if(!$data = fread ($fichier_in, 54)) return 0; // On lit les 54 premiers octets de l'image (correspond à la taille du header)
$binary_string = str_replace (" ","",text2bin ($text_to_hide)); // Transformation du texte en chaine binaire
$limit = strlen ($binary_string); // Limite de la chaine pour boucle
fwrite ($fichier_out,$data); // On écrit ces 54 bytes dans l'image temp
$i = 0; // Variable à incrémenter pour parcourir la chaine binaire
while(!feof($fichier_in)) // On boucle jusqu'à la fin du fichier source
{
$current_byte = fread ($fichier_in,1); // Lecture byte à byte
if($i < $limit)
{
$header_format = 'H1Byte'; // On définit le header
$header = unpack ($header_format, $current_byte); // Unpack du byte
$current_bit = $binary_string[$i]; // On prend un bit à coder
$lettre_codee = changeBit($header['Byte'],$current_bit);
fwrite ($fichier_out,pack('H',$lettre_codee)); // Ecriture du byte dans le fichier temporaire
$i++;
}
else
fwrite ($fichier_out,$current_byte); // Ecriture du byte dans le fichier temporaire
$variable_byte = fread ($fichier_in,3);
fwrite ($fichier_out,$variable_byte);
}
fclose($fichier_in);
fclose($fichier_out);
return 1;
}
function retrieveText($image)
{
if(!$fichier = fopen($image,"rb")) return 0; // Ouverture du fichier source
if(!$header = fread ($fichier, 54)) return 0; // On déplace le curseur après le header
$chaine_codee = ""; // Variable contenant le texte codé
while(!feof($fichier)) // On boucle jusqu'à la fin du fichier source
{
$current_byte = fread ($fichier,1); // Lecture d'un byte
$format = 'H1Byte';
$data = unpack($format,$current_byte); // On extrait sous forme hexadécimale
$chaine_temp = hexa2bin($data['Byte']); // Conversion hexa vers binaire
$chaine_codee .= $chaine_temp[7]; // Concaténation du LSB
}
echo bin2text($chaine_codee);
} |
Partager