PHP实现三重DES加密/解密

7
我有一个PHP中的三重DES加密代码。
    $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=

现在我有密钥,我能否解密数据以获得纯文本格式?

我尝试了这种方法:

            $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);
    }

但是我做不到。我所做的是错误的吗?请给我提供一种解密方法。在Triple DES中,加密密钥本身是否用于解密数据?请帮帮我。


加密此类数据(cookie或响应参数)是出了名的难以正确实现。CBC模式暴露了一个微不足道的填充预言机漏洞。重放攻击甚至更简单。在这种情况下,密钥派生也非常可疑。 - ntoskrnl
最好不要使用PHP mcrypt,它已经被废弃,多年没有更新,并且不支持标准的PKCS#7(原名PKCS#5)填充,只支持非标准的空填充,甚至不能用于二进制数据。mcrypt存在许多未解决的错误,这些错误可以追溯到2003年。mcrypt扩展已被弃用,将在PHP 7.2中删除。相反,考虑使用defuseRNCryptor,它们提供了完整的解决方案,并得到维护和修正。 - zaph
密钥派生不足,使用PBKDF2或类似的密钥派生方法。 - zaph
@ntoskrnl CBC填充预言攻击只有在填充错误返回给攻击者时才会生效。 - zaph
4个回答

10

对于PHP 7.1及以上版本,mcrypt函数已被弃用。下面是使用openssl_encrypt的替代方法。

$ciphertext = openssl_encrypt('string to be encrypted', 'DES-EDE3', 'key', OPENSSL_RAW_DATA);
$ciphertext = base64_encode($ciphertext);

你可以使用这个在线的Triple DES 加密 工具进行交叉检验。
对于其他可用的密码,你可以调用 PHP 方法 openssl_get_cipher_methods()

8
这是解决方案:
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);
}

致意。


2
你的解密函数返回了一个空字符串!请修复它。 - Black

6
我写了一个使用3DES-ECB加密算法的PHP代码,你可以在这里找到两个类文件——一个使用mcrypt库的Crypt_mcrypt,另一个使用openssl库的Crypt_openssl。虽然你可以使用其中任何一个,但强烈建议使用"Crypt_openssl"以获得更好的安全性、性能、可维护性和可移植性。如果你在代码中输入mcrypt这个词汇,那么你可能会犯错。尽管在mcrypt之上提供相对安全的加密库是可能的(早期版本的defuse/php-encryption就是基于此构建的),但切换到openssl将提供更好的安全性、性能、可维护性和可移植性。更好的选择是使用libsodium。

1
3des的最精确结果 ✨ - Rizki Noor Hidayat Wijaya

0

嘿,@黑色伙计,解密函数是完美的,问题在于你需要通过encrypt函数传递加密数据。

<?php 
  $encryption_key = "CE51E06875F7D964";
  $data='tokenNo=test&securityCode=111' ;
  // the below will return the encoded data you need to put the value in the variable $asl.
  echo encrypt($data,$encryption_key);
  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);
  }

  //the below is the encryption of $data = 'token.....' which there above 
  $asl = 'xcFEvIdLXc0LX+lk46iIFY09GF+FL+SWM0PTNw669VE=';
  // this echo will provide you the $data which is there in the begining
  echo decrypt($asl , $encryption_key);

  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);
  }

?>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接