cryptographie en javascript/php

Petit nouveau ! | 1 Messages

28 avr. 2013, 09:04

salut à tous,
Nous avons mis en place un protocole de sécurité d'accès entre le serveur et le client. J'ai besoins de connaitre votre opinion sur à ce sujet afin d'améliorer les performances du protocole.
Le procédé SSL avec une clé commune aléatoire généré par le serveur ou par le client, le principe consiste à établir un canal de communication sécurisé
entre 2 machines.
voici coté javascript (client) le code source
[javascript]/*var head = document.getElementsByTagName('head')[0];
script = document.createElement('script');
script.src = url+'js/auhasard.js';
script.type = 'text/javascript';
head.appendChild(script);*/

crypter = function(cle) {
this.cle = (cle.length>0) ? cle : "";
this.mot = '';
this.integre = 0;
};
crypter.prototype = {

initialize: function() {
this.cle = (cle.length>0) ? cle : "";
this.mot = '';
this.integre = 0;
},

open_Cookie: function (name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
},

read_Cookie: function (name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca;
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
},

erase_Cookie: function (name) {
createCookie(name,"",-1);
},

creer_cle: function () {
if (this.cle.length==0)
{
var ptrmot=new auhasard(128);
this.cle=ptrmot.getRandString();
}
return this.cle;
},

mot_crypter: function (string) {
if (string.length>0)
{
var ptrmot=new auhasard();
ptrmot.mot_auhasard(string,true);
this.mot=ptrmot.motstr;
if ((this.cle.length>0) && (this.mot.length==this.cle.length))
{
this.mot=ptrmot.masquer(this.mot,this.cle);
}
return this.mot;
}
},

mot_decrypter: function (string) {
if (string.length>0)
{
var ptrmot=new auhasard();
if (this.cle.length>0)
{
this.mot=ptrmot.masquer(string,this.cle);
} else { this.mot=string;
}
ptrmot.mot_auhasard(this.mot,false);
this.mot=ptrmot.motstr;
return this.mot;
}
},

compare_cle: function (stringcrypter,chaine) {
if (stringcrypter.length==0) {
throw new Error ("Il manque la chaine de caractéres crypter");
return 0;
}
if (chaine.length==0) {
throw new Error ("Il manque le mot a décrypté");
return 0;
}
if (this.mot_decrypter(stringcrypter)==chaine) {
return 1;
}
return 0;
},

texte_crypter: function (string){
if (string.length==0) {
throw new Error ("Il manque le texte à crypter");
return '';
}
else {
ptrmot=new auhasard();
ptrmot.text_auhasard(string,true,this.cle);
this.mot=ptrmot.motstr;
return this.mot;
}
},


texte_decrypter: function (string) {
ptrmot=new auhasard();
ptrmot.text_auhasard(string,false,this.cle);
this.mot=ptrmot.motstr;
return this.mot;
}


}
[/javascript]

voici coté php(serveur) le code source
$mp=new crypter();
$cle=$mp->creer_cle();
echo $cle."<br>";
$pp=$mp->mot_crypter('bonjour');
echo $pp."<br>";
echo $mp->mot_decrypter($pp)."<br>";

echo $mp->compare_cle($pp,'bonjour comment vas tu')."<br>"; affiche 0
echo $mp->compare_cle($pp,'bonjour')."<br>"; affiche 1

$pp=$mp->texte_crypter('il fait beau aujourd hui, le temps permet de se promener dans les bois afin de rencontre le méchant loup');
echo $pp."<br>";

echo $mp->texte_decrypter($pp);
*/

class codage extends auhasard{
}

class crypter{

private $cle = '';
private $mot = '';
public $err ='';
public $ip = '';
/*
 construire la class avec la clé ou sans
 ---------------------------------------
 
	$mp=new crypter();
	
          ou
	$mp=new cypter($cle);
	d'initialiser la clé qui était sauvegardé
	
*/
public function __construct($cle='')
{
	$this->cle=$cle;
	$ptrmot=new auhasard();
	$this->ip=$ptrmot->get_ip();
}	

/*
 créer une clé aléatoie
 ----------------------
	$mp=new crypter();        
	$cle=$mp->creer_cle();    on peut la sauvegardée afin de l'utilisée plutard pour un décryptage
                              la clé est sur 128 bits	
	
*/

public function creer_cle()
{
	if (empty($this->cle))
	{
		$ptrmot=new auhasard(128);
		$this->cle=$ptrmot->getRandString();
	}
	return $this->cle;
}

	
//  ouvrir une session
	
public function open_session($cle)
{
	session_start(); 
	if( session_id() == '' )
	{
		$this->err='session_id() empty';
		return false;
	}else{
		$_SESSION['laclef'.$this->ip] = $cle; 
		return true;
	}
}

// lecture de la session

public function read_session()
{
	session_start(); 
	if(session_id() == '' )
	{
		$this->err='session_id() empty';
		return null;
	}else{
		if( isset( $_SESSION['laclef'.$this->ip] ) ){
            $cle=$_SESSION['laclef'.$this->ip];
			$this->cle=$cle;
			return $cle;
        }else{
            $this->err='[not exists]';
			return null;
        }
	}
}

public function erase_session()
{
	session_start();
	if (!(session_id() == '' ))
	{
		if( isset( $_SESSION['laclef'.$this->ip] ) ){
            unset($_SESSION['laclef'.$this->ip]);
        }
	}
}

/*
   le mot a crypté avec une clé ou sans
   si $mp=new crypter(); alors
		le cryptage se fera avec ou sans la clé, pour le décryptage  il faudra faire la même chose
	sinon $mp=new crypter($clé);
		le cryptage avec une clé
		
	le mot sortira en 128 bits
	exemple
	    $pp=$mp->mot_crypter('bonjour');
		
*/
public function mot_crypter($string='') 
{
	if (!empty($string))
	{
		$ptrmot=new auhasard();
		$ptrmot->mot_auhasard($string,true);
		$this->mot=$ptrmot->motstr;
		if (!(empty($this->cle)) && (strlen($this->mot)==strlen($this->cle)))
		{
			$this->mot=$ptrmot->masquer($this->mot,$this->cle);
		}
		$this->err=(strlen($ptrmot->err)>0) ? $ptrmot->err : '';
		return $this->mot;
	}	
}

/*
   le mot a décrypté avec une clé ou sans
   si $mp=new crypter(); alors
		le décryptage est la même condition
	sinon $mp=new crypter($clé);
		le décryptage avec une clé
		
	le mot est décrypté
	exemple
	     $mc='9vXlGGiI24Vp3OMmenQRFv8L4elBckA550I38CzjB9UKosud6guayKNaq5F8R33ZVkwsGF6wOJfez2T7IhSdQ2hYZAsOG0SccMUocXnH1xBiwCrl7jPdc6CjKUOaxOLw';
		 /* Attention c'est un exemple il peut avoir 
	    $mot=$mp->mot_decrypter($mc);
		
*/
public function mot_decrypter($string='') 
{
	if (!empty($string))
	{
		$ptrmot=new auhasard();
		$this->mot=(!empty($this->cle)) ? $ptrmot->masquer($string,$this->cle) : $string;
		$ptrmot->mot_auhasard($this->mot);
		$this->mot=$ptrmot->motstr;
		$this->err=(strlen($ptrmot->err)>0) ? $ptrmot->err : '';
		return $this->mot;
	}
}

/*
   comparer le mot crypter par à une chaine saisir
   si le mot crypté est identique alors 
    il renvoit vrai
	sinon
	faux
   
*/
public function compare_cle($stringcrypter='',$chaine='')
{
	$this->err='';
	if (empty($stringcrypter) || empty($chaine))
	{
		$this->err=(empty($stringcrypter)) ? 'Il manque la chaine de caractéres crypter' : 'Il manque le mot a décrypté';
		return 0;
	}
	$mot=$this->mot_decrypter($stringcrypter);
	if ($mot==$chaine)
	{
		return 1;
	}
	return 0;
}

/*
   le text a crypté avec une clé ou sans 
   il n'y a pas de limite de caractéres
   si $mp=new crypter(); alors
		le cryptage se fera avec ou sans la clé, pour le décryptage  il faudra faire la même chose
	sinon $mp=new crypter($clé);
		le cryptage avec une clé
		
	le mot sortira en 128 bits
	exemple
	    $pp=$mp->texte_crypter('il fait beau aujourd hui, le temps permet de se promener dans les bois afin de rencontre le méchant loup');
		
		$pp = 'kl eaiu!afbu!cvimvpe"kvk-"nf!tdoqq"sepmdw#fg#rf rpnmeogq!fblp#leq!boks!ceil#df"pdo`mmuse#og lèbiclv"omuq';
		
*/

public function texte_crypter($string='')
{
	if (empty($string))
	{
		$this->err='Il manque le texte à crypter';
		return null;
	}
	else
	{
		$ptrmot=new auhasard();
		$ptrmot->text_auhasard($string,true,$this->cle);
		$this->mot=$ptrmot->motstr;
		return $this->mot;
	}	
}
 
 /*
   le text a décrypté avec une clé ou sans
   si $mp=new crypter(); alors
		le décryptage est la même condition
	sinon $mp=new crypter($clé);
		le décryptage avec une clé
		
	le texte est décrypté
	exemple
	     $pp = 'kl eaiu!afbu!cvimvpe"kvk-"nf!tdoqq"sepmdw#fg#rf rpnmeogq!fblp#leq!boks!ceil#df"pdo`mmuse#og lèbiclv"omuq';
		 /* Attention il peut étre différent
	    $text=$mp->mot_decrypter($pp);
		
*/
public function texte_decrypter($string='')
{
	$ptrmot=new auhasard();
	$ptrmot->text_auhasard($string,false,$this->cle);
	$this->mot=$ptrmot->motstr;
	return $this->mot;
}

/*
 pour récupérer les messages d'erreurs
 $err=$mp->message_erreur();
*/
public function message_erreur()
{
	return (empty($this->err)) ? null : $this->err;
}

}

on a essayé de préserver les mêmes instructions entre le java et le php, plus amples renseigements à ce sujet
je vous invite alle vers le site http://allios.net84.net/.
Merci à tous
Anthon

Modération :
Afin d'améliorer la lisibilité de ton message,
pense à utiliser les balises [code] ou [php] ou [javascript](selon le langage utilisé).

Elles sont disponibles au-dessus de la zone de saisie de ton message
lorsque tu postes un nouveau message.

Des indications sont disponibles sur la manière de mettre en forme vos messages dans la FAQ