TRIPLE DES encryption/decryption using php

别说谁变了你拦得住时间么 提交于 2019-12-01 07:38:07

问题


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

Im able to encrypt the data as xcFEvIdLXc2fjhG1i4iPOQu5L6ahxwZVucDOPqeMM2E=

Now I have the key,will I able to decrypt this data to plain text format?

I tried it this way

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

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


回答1:


This is the solution:

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

Regards.




回答2:


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

here i wrote 3DES-ECB Cryptography in PHP. there you will get two class file "Crypt_mcrypt" using "mcrypt" and "Crypt_openssl" using "openssl". so you can use any of them but it is

highly recomended to use "Crypt_openssl"

If you're typing the word mcrypt into your code, you're probably making a mistake. Although it's possible to provide a relatively secure cryptography library that builds on top of mcrypt (the earlier version of defuse/php-encryption did), switching your code to openssl will provide better security, performance, maintainability, and portability. Even better: use libsodium instead.




回答3:


For PHP 7.1 and above, mcrypt function is deprecated. Here is the alternative using openssl_encrypt

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

You can use this online Triple DES encryption tool to cross check.

For other available cipher, you can invoke PHP method openssl_get_cipher_methods()




回答4:


Hey @black buddy the decrypt function is perfect the thing is that you need to pass the encyrpted data by the function 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);
  }

?>


来源:https://stackoverflow.com/questions/22592919/triple-des-encryption-decryption-using-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!