使用php进行TRIPLE DES加密/解密

时间:2014-03-23 15:30:36

标签: php encryption cryptography tripledes

我在PHP中有这个TRIPLE DES ENCRYPTION CODE

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

但我无法做到。我正在做的事情是错的?请建议我解密这个问题?加密密钥本身是用来解密三重DES中的数据吗?请帮忙

4 个答案:

答案 0 :(得分: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);
}

问候。

答案 1 :(得分:2)

  

https://github.com/iam-raihan96/3DES-ECB-Cryptography-in-PHP

我在 PHP 中写了 3DES-ECB 加密。使用“openssl”,您将使用“ mcrypt ”和“ Crypt_openssl ”获得两个类文件“ Crypt_mcrypt ” 。所以你可以使用它们中的任何一个,但它是

  

高度推荐使用“Crypt_openssl”

如果您在代码中输入单词mcrypt,那么您可能犯了一个错误。虽然可以提供构建在mcrypt之上的相对安全的加密库(早期版本的defuse/php-encryption),但将代码切换为openssl将提供更好的安全性,性能,可维护性和可移植性。更好的是:改用libsodium。

答案 2 :(得分:2)

对于 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 encryption工具进行交叉检查。

对于其他可用密码,您可以调用PHP方法 openssl_get_cipher_methods()

答案 3 :(得分:0)

嘿@black伙伴解密功能完善的事情是你需要通过功能加密传递encyrpted数据。

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

?>
相关问题