The Higher Education and Research forge

Home My Page Projects Code Snippets Project Openings

Snippet Library

mélange cryptographique et clé de fibonacci

Type:
Sample Code (HOWTO)
Category:
Math Functions
License:
GNU General Public License
Language:
PHP

Description:
Création d'une clé mélangeant deux ip (serveur et client) ainsi qu'un timestamp et d'une clé crypté avec l'algorythme de César

Versions Of This Snippet:

Snippet ID Download Version Date Posted Author Delete
173.02022-05-21 22:07Emmanuel ROY Delete

Download a raw-text version of this code by clicking on “Download Version”


Latest Snippet Version: 3.0

<?php

$ip_client = $_SERVER['REMOTE_ADDR'];
$ip_serveur = $_SERVER['SERVER_ADDR'];
$time = $_SERVER['REQUEST_TIME'];
$token = encrypter_Cesar("B25", 19);
$clemap_encrypt = encrypterCleMapDialogue($ip_serveur, $ip_client, $time, $token);

echo $clemap_encrypt;

$clemapNumeraire = decrypterCleMapDialogue($clemap_encrypt);

echo "<br />";
if ($ip_client !== $clemapNumeraire[3]) {
    echo "IP CLIENT $clemapNumeraire[3]($ip_client) : SCAM";
} else {
    echo "IP CLIENT $clemapNumeraire[3]($ip_client) : OK !";
}
echo "<br />";
if ($ip_serveur !== $clemapNumeraire[1]) {
    echo "IP SERVEUR $clemapNumeraire[1]($ip_serveur) : SPAM";
} else {
    echo "IP SERVEUR $clemapNumeraire[1]($ip_serveur) : OK !";
}
echo "<br />";
if ($time === $clemapNumeraire[0]) {
    echo "TIME SERVER $clemapNumeraire[0]($time) : erreurParadoxale";
} else {
    echo "TIME SERVER $clemapNumeraire[0]($time) : OK !";
}
echo "<br />";
if ($token !== $clemapNumeraire[2]) {
    echo "TOKEN $clemapNumeraire[2]($token) : erreurH4x0r";
} else {
    echo "TOKEN $clemapNumeraire[2]($token) : OK !";
}
echo "<br />";



function decrypter_Cesar($aDeCrypter, $decalage)
{
    $decalage = (36+$decalage)%36;
    //echo $decalage."   |";
    $caracteres = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    $aDeCrypter = strtoupper($aDeCrypter);
    $tab_code = preg_split('//', $aDeCrypter, -1, PREG_SPLIT_NO_EMPTY);
    $PassPhrase = '';
    foreach ($tab_code as $elmt) {
        $position = strpos($caracteres, $elmt);
        $new_pos = ($position+$decalage)%36;
        $PassPhrase .= substr($caracteres, $new_pos, 1);
    }
    return $PassPhrase;
}
function encrypter_Cesar($aCrypter, $decalage)
{
    $decalage = 36 - (36+$decalage)%36;
    //echo $decalage."   |";
    $caracteres = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    $aCrypter = strtoupper($aCrypter);
    $tab_code = preg_split('//', $aCrypter, -1, PREG_SPLIT_NO_EMPTY);
    $PassPhrase = '';
    foreach ($tab_code as $elmt) {
        $position = strpos($caracteres, $elmt);
        $new_pos = ($position+$decalage)%36;
        $PassPhrase .= substr($caracteres, $new_pos, 1);
    }
    return $PassPhrase;
}
function invertionCaracteres($chaine)
{
    $chaine_tab = preg_split('//', $chaine, -1, PREG_SPLIT_NO_EMPTY);
    $taille_chaine = count($chaine_tab)-1;
    $chaine_inv = '';
    //echo "passe".$taille_chaine;
    for ($i = $taille_chaine ; $i >= 0 ; $i--) {
        $chaine_inv .= $chaine_tab[$i];
    }
    return $chaine_inv;
}
function expulserDernierElement($tab)
{
    $taille = count($tab);
    $nouveau_tab = array();
    for ($itx = 0 ; $itx < $taille-1 ; $itx++) {
        $nouveau_tab[] = $tab[$itx];
        //echo "i:".$itx;
    }
    return $nouveau_tab;
}
function expulserPremierElement($tab)
{
    $taille = count($tab);
    $nouveau_tab = array();
    for ($itx = 1 ; $itx < $taille ; $itx++) {
        $nouveau_tab[] = $tab[$itx];
        //echo "i:".$itx;
    }
    return $nouveau_tab;
}
function transformerChaineEnTableau($ChaineCaracteres)
{
    return preg_split('//', $ChaineCaracteres, -1, PREG_SPLIT_NO_EMPTY);
}
function transformerTableauEnChaine($tableau)
{
    $chaine = "";
    foreach ($tableau as $element) {
        $chaine .= $element;
    }
    return $chaine;
}
function encrypterCleMapDialogue($ip_serveur, $ip_client, $time, $token)
{
    $time_inv = invertionCaracteres($time);
    $ip2_en_chiffre_acoller_tab = explode('.', $ip_client);
    $ip2_en_chiffre_acoller = implode($ip2_en_chiffre_acoller_tab);
    $ip2_inv = invertionCaracteres($ip2_en_chiffre_acoller);
    /*TODO: ENCRYPTER UNE NOUVELLE CRYPTOGRAPHIE*/
    $ip2_encrypt = encrypter_Cesar($ip2_inv, 18);
    $dimension_ip2 = strlen($ip2_en_chiffre_acoller_tab[0])."".strlen($ip2_en_chiffre_acoller_tab[1])."".strlen($ip2_en_chiffre_acoller_tab[2])."".strlen($ip2_en_chiffre_acoller_tab[3]);
    $ip2_encrypt .= ".".encrypter_Cesar($dimension_ip2, 7);
    /*END TODO*/
    $ip_en_chiffre_acoller = explode('.', $ip_serveur);
    $dimension_ip = strlen($ip_en_chiffre_acoller[0])."".strlen($ip_en_chiffre_acoller[1])."".strlen($ip_en_chiffre_acoller[2])."".strlen($ip_en_chiffre_acoller[3]);
    $dimension_ip_inv = invertionCaracteres($dimension_ip);
    //$ip_en_chiffre_acoller_add_taille = $dimension_ip.implode($ip_en_chiffre_acoller);
    $ip_en_chiffre_acoller = implode($ip_en_chiffre_acoller);
    $ip_inv = invertionCaracteres($ip_en_chiffre_acoller);
    //$ip_inv_add_taille = invertionCaracteres($ip_en_chiffre_acoller_add_taille);
    $ip_tab_inv = preg_split('//', $ip_inv, -1, PREG_SPLIT_NO_EMPTY);
    $time_tab_inv = preg_split('//', $time_inv, -1, PREG_SPLIT_NO_EMPTY);
    $taille_ip = count($ip_tab_inv);
    $taille_time = count($time_tab_inv);
    $clemap = '';
    //mélange
    for ($i = 0; ($i < $taille_ip && $i < $taille_time) ; $i++) {
        $clemap .= $ip_tab_inv[0].$time_tab_inv[0];
        $ip_tab_inv = expulserPremierElement($ip_tab_inv);
        $time_tab_inv = expulserPremierElement($time_tab_inv);
    }
    $clemap .= implode($ip_tab_inv);
    $clemap .= implode($time_tab_inv);
    //ajout de fin d'élément
    $clemap .= $token;
    $clemap .= $taille_time;
    //fin du mélange
    //$taille_time = '10000000000000072635098764359649420365426520929365074263850640643';
    $time_taille_tab = preg_split('//', $taille_time, -1, PREG_SPLIT_NO_EMPTY);
    $taille_caracteres_time = count($time_taille_tab);
    $taille_caracteres_time_itx = $taille_caracteres_time;
    //création de la clé de fibonnaci afin de connaitre le nombre de caracteres de la chaine (taille de la chaine time)
    $cleFibonacci = '';
    $i = 0;
    while (isset($taille_caracteres_time_itx) && $taille_caracteres_time_itx > 1) {
        $time_taille_tab_itx = preg_split('//', $taille_caracteres_time_itx, -1, PREG_SPLIT_NO_EMPTY);
        $taille_caracteres_time_itx = count($time_taille_tab_itx);
        $taille_caracteres_time .= $taille_caracteres_time_itx;

        $i++;
    }
    $cleFibonacci = $i+1;

    $cleNumeraire = $taille_caracteres_time.$cleFibonacci;
    //echo "<br />".$time."/".$cleNumeraire."<br/>";
    //echo $cleNumeraire;die();
    $cleNumeraire = encrypter_Cesar($cleNumeraire, 4);
    $clemap_encrypt = encrypter_Cesar($clemap, 3);
    $clemap_encrypt = $clemap_encrypt . $cleNumeraire;
    $clemap_encrypt_with_ip_dimension = $dimension_ip_inv . $clemap_encrypt;
    $clemap_with_ip_dimension_encrypt = encrypter_Cesar($clemap_encrypt_with_ip_dimension, 3);
    //$clemap_with_ip_dimension_encrypt_tab = preg_split('//',$clemap_with_ip_dimension_encrypt, -1,PREG_SPLIT_NO_EMPTY);
    $clemap_with_ip_dimension_encrypt .= $ip2_encrypt;
    /*$taille_tab = count($clemap_with_ip_dimension_encrypt_tab);
    $i = 0;
    $final_clemap_encrypt = "";
    do{
        $final_clemap_encrypt .= $clemap_with_ip_dimension_encrypt_tab[$i];
        if ($i % 5 == 0){
            $final_clemap_encrypt .= ' ';
        }
        $i++;
    }while ( $i < $taille_tab );
    return $final_clemap_encrypt;*/
    return $clemap_with_ip_dimension_encrypt;
}

function decrypterCleMapDialogue($clemap_encrypt)
{
    /*$clemap_with_ip_dimension_encrypt_with_block_tab = preg_split('//',$clemap_encrypt, -1,PREG_SPLIT_NO_EMPTY);
    $taille_tab = count($clemap_with_ip_dimension_encrypt_with_block_tab);
    $i = 0;
    $final_clemap_encrypt_no_block = "";
    do{
        if ($clemap_with_ip_dimension_encrypt_with_block_tab[$i] !== ' '){
        $final_clemap_encrypt_no_block .= $clemap_with_ip_dimension_encrypt_with_block_tab[$i];
        }
        $i++;
    }while ( $i < $taille_tab );

    $final_clemap_encrypt_no_block = explode(".",$final_clemap_encrypt_no_block);*/
    $final_clemap_encrypt_no_block = explode(".", $clemap_encrypt);
    if (!isset($final_clemap_encrypt_no_block[1])) {
        $tab_retour = array();
        $tab_retour[0] = "";
        $tab_retour[1] = "";
        $tab_retour[2] = "";
        $tab_retour[3] = "";
        return $tab_retour;
    }
    $taille_ipclient = decrypter_Cesar($final_clemap_encrypt_no_block[1], 7);
    $ip2_dimension = array();
    $ip2_dimension[] = substr($taille_ipclient, 0, 1);
    $ip2_dimension[] = substr($taille_ipclient, 1, 1);
    $ip2_dimension[] = substr($taille_ipclient, 2, 1);
    $ip2_dimension[] = substr($taille_ipclient, 3, 1);
    $ip2_total_dimension = $ip2_dimension[0]+$ip2_dimension[1]+$ip2_dimension[2]+$ip2_dimension[3];
    $ip2_notSeparated_encrypt = substr($final_clemap_encrypt_no_block[0], -$ip2_total_dimension);
    $ip2_notSeparated_decrypt = decrypter_Cesar($ip2_notSeparated_encrypt, 18);
    $ip2_notSeparated_notInverted = invertionCaracteres($ip2_notSeparated_decrypt);

    $ip2 = substr($ip2_notSeparated_notInverted, 0, $ip2_dimension[0]).".";
    $ip2 .= substr($ip2_notSeparated_notInverted, $ip2_dimension[0], $ip2_dimension[1]).".";
    $ip2 .= substr($ip2_notSeparated_notInverted, $ip2_dimension[0]+$ip2_dimension[1], $ip2_dimension[2]).".";
    $ip2 .= substr($ip2_notSeparated_notInverted, $ip2_dimension[0]+$ip2_dimension[1]+$ip2_dimension[2], $ip2_dimension[3]);

    $final_clemap_encrypt_begin_with_no_block = substr($final_clemap_encrypt_no_block[0], 0, -$ip2_total_dimension);

    $ip_post = decrypter_Cesar($final_clemap_encrypt_begin_with_no_block, 3);
    $ip_dimension = array();
    $ip_dimension[] = substr($ip_post, 0, 1);
    $ip_dimension[] = substr($ip_post, 1, 1);
    $ip_dimension[] = substr($ip_post, 2, 1);
    $ip_dimension[] = substr($ip_post, 3, 1);

    $true_taille_ip = $ip_dimension[0]+$ip_dimension[1]+$ip_dimension[2]+$ip_dimension[3];

    $clemap_encrypt_sans_dimension_ip = substr($final_clemap_encrypt_begin_with_no_block, 4);

    $clemapNumeraire = decrypter_Cesar($clemap_encrypt_sans_dimension_ip, 3);
    $clemapNumeraire = decrypter_Cesar($clemapNumeraire, 4);
    $clemapNumeraire_tab = preg_split('//', $clemapNumeraire, -1, PREG_SPLIT_NO_EMPTY);
    $clemapNumeraire_tab_taille = count($clemapNumeraire_tab)-1;
    $i=1;
    $tempIndice = $clemapNumeraire_tab_taille;
    $cleFibonacci = $clemapNumeraire_tab[$clemapNumeraire_tab_taille];
    $clemapNumeraire_tab = expulserDernierElement($clemapNumeraire_tab);

    $nbElement = 1;
    for ($i = 1 ; $i <= $cleFibonacci ; $i++) {
        $nbElement_temp = '';
        for ($j = 0 ; $j < $nbElement ; $j++) {
            $tempIndice = count($clemapNumeraire_tab)-1;
            $nbCaracteres = $clemapNumeraire_tab[$tempIndice];
            $nbElement_temp = $nbCaracteres.$nbElement_temp;
            $clemapNumeraire_tab = expulserDernierElement($clemapNumeraire_tab);
        }
        $nbElement = $nbElement_temp;
    }
    $taille_time = $nbElement;

    $clemap_encrypt_sans_dimension = implode($clemapNumeraire_tab);
    $clemap_reencrypt_sans_dimension = encrypter_Cesar($clemap_encrypt_sans_dimension, 4);
    $clemap = decrypter_Cesar($clemap_reencrypt_sans_dimension, 3);
    $clemap_tab = preg_split('//', $clemap, -1, PREG_SPLIT_NO_EMPTY);
    $element = '';


    for ($j = 0 ; $j < $taille_time ; $j++) {
        $tempIndice = count($clemap_tab)-1;
        if ($tempIndice < 0) {
            break;
        }
        $caractere = $clemap_tab[$tempIndice];
        $element = $caractere.$element;
        $clemap_tab = expulserDernierElement($clemap_tab);
    }

    $true_taille_time = $element;
    $element = '';

    for ($j = 0 ; $j < 3 ; $j++) {
        $tempIndice = count($clemap_tab)-1;
        if ($tempIndice < 0) {
            break;
        }
        $caractere = $clemap_tab[$tempIndice];
        $element = $caractere.$element;
        $clemap_tab = expulserDernierElement($clemap_tab);
    }

    $token_encrypt = $element;
    $token_decrypt= $token_encrypt;

    if ($true_taille_time > $true_taille_ip) {
        $difference = $true_taille_time - $true_taille_ip;
        $flagLePlusLong = 'time';
    } else {
        $difference = $true_taille_ip - $true_taille_time;
        $flagLePlusLong = 'ip';
    }
    $element = '';

    for ($i = 0 ; $i < $difference ; $i++) {
        $tempIndice = count($clemap_tab)-1;
        $caractere = $clemap_tab[$tempIndice];
        $element = $caractere.$element;
        $clemap_tab = expulserDernierElement($clemap_tab);
    }

    $tempip = '';
    $temptime = '';

    if ($flagLePlusLong = 'ip') {
        $tempip = $element;
    } else {
        $temptime = $element;
    }

    $ip = '';
    $time = '';

    while (isset($clemap_tab[0]) && $clemap_tab[0] != null) {
        $ip .= $clemap_tab[0];
        $clemap_tab = expulserPremierElement($clemap_tab);
        $time .= $clemap_tab[0];
        $clemap_tab = expulserPremierElement($clemap_tab);
    }

    $ip .= $tempip;
    $time .= $temptime;

    $time_post_without_ip_dimension = invertionCaracteres($time);
    $ip_not_inverted = invertionCaracteres($ip);

    $ip_complete = substr($ip_not_inverted, 0, $ip_dimension[3]).".";
    $ip_complete .= substr($ip_not_inverted, $ip_dimension[3], $ip_dimension[2]).".";
    $ip_complete .= substr($ip_not_inverted, $ip_dimension[3]+$ip_dimension[2], $ip_dimension[1]).".";
    $ip_complete .= substr($ip_not_inverted, $ip_dimension[3]+$ip_dimension[2]+$ip_dimension[1], $ip_dimension[0]);

    $tab_retour = array();
    $tab_retour[0] = $time_post_without_ip_dimension;
    $tab_retour[1] = $ip_complete;
    $tab_retour[2] = $token_decrypt;
    $tab_retour[3] = $ip2;

    //	print_r($tab_retour);

    return $tab_retour;
}


		

Add a new version

You can submit a new version of this snippet if you have modified it and you feel it is appropriate to share with others.