用于编码功能的Php解码功能

时间:2016-12-01 21:03:38

标签: php

我在php中有一个编码功能,我试图为它做一个解码功能,但我没有发现..有人可以帮我这个吗?

function encode($data)
{

    $password = 'EBuLTKjdCf0dmX7MQ1SrquKtvs7Fn5EW13xouUNGWwpqLWisMqe8v574HWS1UT2bkAMXC163euCz5MDm0U2GpuY';
    $salt = substr(md5(mt_rand(), true), 8);
    $key = md5($password . $salt, true);
    $iv = md5($key . $password . $salt, true);
    $ct = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $data, MCRYPT_MODE_CBC, $iv);
    $unique = md5(mt_rand(), true);
    return str_replace(array('+', '/'), array('-', '_'), rtrim(base64_encode($unique . $salt . $ct), '='));
}

function decode($data) {


    $password = 'EBuLTKjdCf0dmX7MQ1SrquKtvs7Fn5EW13xouUNGWwpqLWisMqe8v574HWS1UT2bkAMXC163euCz5MDm0U2GpuY';
    $salt = $data = base64_decode( str_replace( array( '-', '_' ), array( '+', '/' ), $data ) );
    $ct = substr( $data, 18 );
    md5( $password . $salt, true );
    $key = substr( $data, 10, 8 );

    $iv = md5( $key . $password . $salt, true );
    $pt = mcrypt_decrypt( MCRYPT_RIJNDAEL_128, $key, $ct, MCRYPT_MODE_CBC, $iv );
    return trim( $pt);
}

解码函数返回一个空字符串。

1 个答案:

答案 0 :(得分:2)

这应该有效:

function decode($data) {
    $password = 'EBuLTKjdCf0dmX7MQ1SrquKtvs7Fn5EW13xouUNGWwpqLWisMqe8v574HWS1UT2bkAMXC163euCz5MDm0U2GpuY';
    $decoded = base64_decode( str_replace( array( '-', '_' ), array( '+', '/' ), $data ) );
    $unique = substr($decoded, 0, 36);
    $salt = substr($decoded, 16, 8);
    $ct = substr($decoded, 24);
    $key = md5($password . $salt, true);
    $iv = md5($key . $password . $salt, true);
    $pt = mcrypt_decrypt( MCRYPT_RIJNDAEL_128, $key, $ct, MCRYPT_MODE_CBC, $iv );

    return trim( $pt);
}

E.g:

php > echo decode(encode("Lorem ipsum dolor sit amet"));
Lorem ipsum dolor sit amet