程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> PHP基礎知識 >> PHP版3DES加解密類,可與java的3DES(DESede)加密方式兼容

PHP版3DES加解密類,可與java的3DES(DESede)加密方式兼容

編輯:PHP基礎知識
 

<?php
/**
*
* PHP版3DES加解密類
*
* 可與java的3DES(DESede)加密方式兼容
*
* @Author: Luo Hui (farmer.luo at gmail.com)
*
* @version: V0.1 2008.12.04
*
*/
class Crypt3Des
{
public $key = "01234567890123456789012345678912";
public $iv = "23456789"; //like java: private static byte[] myIV = { 50, 51, 52, 53, 54, 55, 56, 57 };

//加密
public function encrypt($input)
{
$input = $this->padding( $input );
$key = base64_decode($this->key);
$td = mcrypt_module_open( MCRYPT_3DES, '', MCRYPT_MODE_CBC, '');
//使用MCRYPT_3DES算法,cbc模式
mcrypt_generic_init($td, $key, $this->iv);
//初始處理
$data = mcrypt_generic($td, $input);
//加密
mcrypt_generic_deinit($td);
//結束
mcrypt_module_close($td);
$data = $this->removeBR(base64_encode($data));
return $data;
}

//解密
public function decrypt($encrypted)
{
$encrypted = base64_decode($encrypted);
$key = base64_decode($this->key);
$td = mcrypt_module_open( MCRYPT_3DES,'',MCRYPT_MODE_CBC,'');
//使用MCRYPT_3DES算法,cbc模式
mcrypt_generic_init($td, $key, $this->iv);
//初始處理
$decrypted = mdecrypt_generic($td, $encrypted);
//解密
mcrypt_generic_deinit($td);
//結束
mcrypt_module_close($td);
$decrypted = $this->removePadding($decrypted);
return $decrypted;
}

//填充密碼,填充至8的倍數
public function padding( $str )
{
$len = 8 - strlen( $str ) % 8;
for ( $i = 0; $i < $len; $i++ )
{
$str .= chr( 0 );
}
return $str ;
}

//刪除填充符
public function removePadding( $str )
{
$len = strlen( $str );
$newstr = "";
$str = str_split($str);
for ($i = 0; $i < $len; $i++ )
{
if ($str[$i] != chr( 0 ))
{
$newstr .= $str[$i];
}
}
return $newstr;
}

//刪除回車和換行
public function removeBR( $str )
{
$len = strlen( $str );
$newstr = "";
$str = str_split($str);
for ($i = 0; $i < $len; $i++ )
{
if ($str[$i] != '\n' and $str[$i] != '\r')
{
$newstr .= $str[$i];
}
}

return $newstr;
}
}
//test
$input = "1qaz2ws";
echo "plainText:" . $input."<br/>";
$crypt = new Crypt3Des();
echo "Encode:".$crypt->encrypt($input)."<br/>";
echo "Decode:".$crypt->decrypt($crypt->encrypt($input));
?>
結果:
plainText:1qaz2ws
Encode:0GsXgYA8BuM=
Decode:1qaz2ws
 

 

 

<?php
/**
*@param DES/3DES加密解密
*@param 如果是3des,將MCRYPT_DES修改為MCRYPT_3DES,個人筆記
*/
class DesCrypt{
var $key = '';
var $deviceid = '';
var $user = '';
var $lsh = '';
var $cipherText = '';
var $HcipherText = '';
var $decrypted_data ='';

function DesCrypt(){
}
//加密
function en($str)
{
$cipher = mcrypt_module_open(MCRYPT_DES, '', MCRYPT_MODE_ECB, '');
$iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_DES,MCRYPT_MODE_ECB), MCRYPT_RAND);

if (mcrypt_generic_init($cipher, substr($this->key,0,8), $iv) != -1)
{
$this->cipherText = mcrypt_generic($cipher,$this->pad($str));
mcrypt_generic_deinit($cipher);
// 以十六進制字符顯示加密後的字符
$this->HcipherText=bin2hex($this->cipherText);
printf("<p>3DES encrypted:\n%s</p>",$this->cipherText);
printf("<p>3DES HexEncrypted:\n%s</p>",$this->HcipherText);
}
mcrypt_module_close($cipher);
return $this->cipherText;
}
//解密
function de($str)
{
$str = pack('H*', $str);
$cipher = mcrypt_module_open(MCRYPT_DES, '', MCRYPT_MODE_ECB, '');
$iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_DES,MCRYPT_MODE_ECB), MCRYPT_RAND);
if (mcrypt_generic_init($cipher, substr($this->key,0,8), $iv) != -1)
{
$this->decrypted_data = mdecrypt_generic($cipher,$str);
mcrypt_generic_deinit($cipher);
}
mcrypt_module_close($cipher);
return $this->unpad($this->decrypted_data);
}

private function pad ($data)
{
$data = str_replace("\n","",$data);
$data = str_replace("\t","",$data);
$data = str_replace("\r","",$data);
return $data;
}

private function unpad ($text)
{
$pad = ord($text{strlen($text) - 1});
if ($pad > strlen($text)) {
return false;
}
if (strspn($text, chr($pad), strlen($text) - $pad) != $pad) {
return false;
}
return substr($text, 0, - 1 * $pad);

}
};
?>

 
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved