本文介紹了使用 php 的三重 DES 加密/解密的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我在 PHP 中有這個 TRIPLE DES 加密代碼
I have this TRIPLE DES ENCRYPTION CODE IN PHP
$encryption_key = "CE51E06875F7D964";
$data='tokenNo=test&securityCode=111' ;
echo $desEncryptedData = encryptText_3des($data, $encryption_key);//outputs 3des encrypted data
function encryptText_3des($plainText, $key) {
$key = hash("md5", $key, TRUE);
for ($x=0;$x<8;$x++) {
$key = $key.substr($key, $x, 1);
}
$padded = pkcs5_pad($plainText,
mcrypt_get_block_size(MCRYPT_3DES, MCRYPT_MODE_CBC));
$encrypted = base64_encode(mcrypt_encrypt(MCRYPT_3DES, $key, $padded, MCRYPT_MODE_CBC));
return $encrypted;
}
function pkcs5_pad ($text, $blocksize)
{
$pad = $blocksize - (strlen($text) % $blocksize);
return $text . str_repeat(chr($pad), $pad);
}
我可以將數據加密為 xcFEvIdLXc2fjhG1i4iPOQu5L6ahxwZVucDOPqeMM2E=
現在我有了密鑰,我可以將這些數據解密為純文本格式嗎?
Now I have the key,will I able to decrypt this data to plain text format?
我是這樣試的
$encryption_key = "CE51E06875F7D964";
$data='xcFEvIdLXc2fjhG1i4iPOQu5L6ahxwZVucDOPqeMM2E=' ; //encrypted data
echo $desEncryptedData = encryptText_3des($data, $encryption_key);//outputs 3des encrypted data
function encryptText_3des($plainText, $key) {
$key = hash("md5", $key, TRUE);
for ($x=0;$x<8;$x++) {
$key = $key.substr($key, $x, 1);
}
$padded = pkcs5_unpad($plainText,
mcrypt_get_block_size(MCRYPT_3DES, MCRYPT_MODE_CBC));
$encrypted = base64_encode(mcrypt_encrypt(MCRYPT_3DES, $key, $padded, MCRYPT_MODE_CBC));
return $encrypted;
}
function pkcs5_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);
}
但是我做不到.是我做的不對嗎?請給我建議一種解密方法?加密密鑰本身是否用于解密三重DES中的數據?請幫助
But I couldnt do it.IS what I am doing is wrong?Please suggest me a way to decrypt this?Is the encryption key itself is used to decrypt the data in triple DES?Please help
推薦答案
這是解決方案:
public function encrypt($data, $secret)
{
//Generate a key from a hash
$key = md5(utf8_encode($secret), true);
//Take first 8 bytes of $key and append them to the end of $key.
$key .= substr($key, 0, 8);
//Pad for PKCS7
$blockSize = mcrypt_get_block_size('tripledes', 'ecb');
$len = strlen($data);
$pad = $blockSize - ($len % $blockSize);
$data .= str_repeat(chr($pad), $pad);
//Encrypt data
$encData = mcrypt_encrypt('tripledes', $key, $data, 'ecb');
return base64_encode($encData);
}
public function decrypt($data, $secret)
{
//Generate a key from a hash
$key = md5(utf8_encode($secret), true);
//Take first 8 bytes of $key and append them to the end of $key.
$key .= substr($key, 0, 8);
$data = base64_decode($data);
$data = mcrypt_decrypt('tripledes', $key, $data, 'ecb');
$block = mcrypt_get_block_size('tripledes', 'ecb');
$len = strlen($data);
$pad = ord($data[$len-1]);
return substr($data, 0, strlen($data) - $pad);
}
問候.
這篇關于使用 php 的三重 DES 加密/解密的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!