Find this useful? Enter your email to receive occasional updates for securing PHP code.

Signing you up...

Thank you for signing up!

PHP Decode

<?php namespace Swoole; /** * * 64/128/256 bit8/16/32 * DES/AESDES:64bit AES:128/256b..

Decoded Output download

<?php
namespace Swoole;

/**
 * 
 * 64/128/256 bit8/16/32
 * DES/AESDES:64bit AES:128/256bit
 * CBC/ECB/OFB/CFB
 * base64//
 * : PKCS5PaddingDES
 */
class DES
{
    public $base64Encoding = true;

    protected $key;
    protected $mode;
    protected $cipher;
    protected $iv = null;
    protected $blockSize;
    protected $padding = false;

    const PADDING_NONE = 0;
    const PADDING_PKCS5 = 1;
    const PADDING_PKCS7 = 2;

    /**
     * @param $key
     * @param null $cipher
     * @param string $mode
     * @param bool $base64
     * @throws \Exception
     */
    public function __construct($key, $cipher = null, $mode = 'cbc', $base64 = true, $padding = self::PADDING_NONE)
    {
        if (!function_exists('mcrypt_create_iv'))
        {
            throw new \Exception(__CLASS__ . " require mcrypt extension.");
        }

        $this->key = $key;
        if (!$cipher)
        {
            $this->cipher = self::getCipher($key);
        }
        else
        {
            $this->cipher = $cipher;
        }
        $this->base64Encoding = $base64;
        $this->padding = $padding;
        switch (strtolower($mode))
        {
            case 'ofb':
                $this->mode = MCRYPT_MODE_OFB;
                break;
            case 'cfb':
                $this->mode = MCRYPT_MODE_CFB;
                break;
            case 'ecb':
                $this->mode = MCRYPT_MODE_ECB;
                break;
            case 'cbc':
            default:
                $this->mode = MCRYPT_MODE_CBC;
        }
    }

    static function getCipher($key)
    {
        switch (strlen($key))
        {
            case 8:
                $mcrypt = MCRYPT_DES;
                break;
            case 16:
                $mcrypt = MCRYPT_RIJNDAEL_128;
                break;
            case 32:
                $mcrypt = MCRYPT_RIJNDAEL_256;
                break;
            default:
                throw new \Exception("des key size must be 8/16/32");
        }
        return $mcrypt;
    }

    function createIV()
    {
        $source = PHP_OS == 'WINNT' ? MCRYPT_RAND : MCRYPT_DEV_RANDOM;
        $this->iv = mcrypt_create_iv($this->blockSize, $source);
    }

    function getIV()
    {
        return $this->iv;
    }

    function setIV($iv)
    {
        $this->iv = $iv;
        $this->blockSize = mcrypt_get_block_size($this->cipher, $this->mode);
    }

    /**
     * 
     * @param string $str 
     * @return string 
     */
    public function encode($str)
    {
        //
        if ($this->padding)
        {
            $str = self::PADDING_PKCS5 == $this->padding ? $this->addPKCS5Padding($str) : $this->addPKCS7Padding($str);
        }
        //BASE64
        if ($this->base64Encoding)
        {
            $_str = base64_encode($str);
        }
        else
        {
            $_str = $str;
        }
        return mcrypt_encrypt($this->cipher, $this->key, $_str, $this->mode, $this->iv);
    }

    /**
     * 
     * @param string $str 
     * @return string 
     */
    public function decode($str)
    {
        $ret = mcrypt_decrypt($this->cipher, $this->key, $str, $this->mode, $this->iv);
        //
        if ($this->padding)
        {
            $ret =  self::PADDING_PKCS5 == $this->padding ? $this->stripPKCS5Padding($ret) : $this->stripPKCS7Padding($ret);
        }
        //BASE64
        if ($this->base64Encoding)
        {
            return base64_decode($ret);
        }
        else
        {
            return $ret;
        }
    }

    public function addPKCS5Padding($souce)
    {
        $pad = $this->blockSize - (strlen($souce) % $this->blockSize);
        return $souce . str_repeat(chr($pad), $pad);
    }

    public function stripPKCS5Padding($source)
    {
        $pad = ord($source{strlen($source) - 1});
        if ($pad > strlen($source))
        {
            return false;
        }
        if (strspn($source, chr($pad), strlen($source) - $pad) != $pad)
        {
            return false;
        }
        $ret = substr($source, 0, -1 * $pad);
        return $ret;
    }

    public function addPKCS7Padding($source)
    {
        $pad = $this->blockSize - (strlen($source) % $this->blockSize);
        if ($pad <= $this->blockSize)
        {
            $char = chr($pad);
            $source .= str_repeat($char, $pad);
        }
        return $source;
    }

    public function stripPKCS7Padding($source)
    {
        $char = substr($source, -1, 1);
        $num = ord($char);
        if ($num > 8)
        {
            return $source;
        }
        $len = strlen($source);
        for ($i = $len - 1; $i >= $len - $num; $i--)
        {
            if (ord(substr($source, $i, 1)) != $num)
            {
                return $source;
            }
        }
        $source = substr($source, 0, -$num);
        return $source;
    }
}
 ?>

Did this file decode correctly?

Original Code

<?php
namespace Swoole;

/**
 * 
 * 64/128/256 bit8/16/32
 * DES/AESDES:64bit AES:128/256bit
 * CBC/ECB/OFB/CFB
 * base64//
 * : PKCS5PaddingDES
 */
class DES
{
    public $base64Encoding = true;

    protected $key;
    protected $mode;
    protected $cipher;
    protected $iv = null;
    protected $blockSize;
    protected $padding = false;

    const PADDING_NONE = 0;
    const PADDING_PKCS5 = 1;
    const PADDING_PKCS7 = 2;

    /**
     * @param $key
     * @param null $cipher
     * @param string $mode
     * @param bool $base64
     * @throws \Exception
     */
    public function __construct($key, $cipher = null, $mode = 'cbc', $base64 = true, $padding = self::PADDING_NONE)
    {
        if (!function_exists('mcrypt_create_iv'))
        {
            throw new \Exception(__CLASS__ . " require mcrypt extension.");
        }

        $this->key = $key;
        if (!$cipher)
        {
            $this->cipher = self::getCipher($key);
        }
        else
        {
            $this->cipher = $cipher;
        }
        $this->base64Encoding = $base64;
        $this->padding = $padding;
        switch (strtolower($mode))
        {
            case 'ofb':
                $this->mode = MCRYPT_MODE_OFB;
                break;
            case 'cfb':
                $this->mode = MCRYPT_MODE_CFB;
                break;
            case 'ecb':
                $this->mode = MCRYPT_MODE_ECB;
                break;
            case 'cbc':
            default:
                $this->mode = MCRYPT_MODE_CBC;
        }
    }

    static function getCipher($key)
    {
        switch (strlen($key))
        {
            case 8:
                $mcrypt = MCRYPT_DES;
                break;
            case 16:
                $mcrypt = MCRYPT_RIJNDAEL_128;
                break;
            case 32:
                $mcrypt = MCRYPT_RIJNDAEL_256;
                break;
            default:
                throw new \Exception("des key size must be 8/16/32");
        }
        return $mcrypt;
    }

    function createIV()
    {
        $source = PHP_OS == 'WINNT' ? MCRYPT_RAND : MCRYPT_DEV_RANDOM;
        $this->iv = mcrypt_create_iv($this->blockSize, $source);
    }

    function getIV()
    {
        return $this->iv;
    }

    function setIV($iv)
    {
        $this->iv = $iv;
        $this->blockSize = mcrypt_get_block_size($this->cipher, $this->mode);
    }

    /**
     * 
     * @param string $str 
     * @return string 
     */
    public function encode($str)
    {
        //
        if ($this->padding)
        {
            $str = self::PADDING_PKCS5 == $this->padding ? $this->addPKCS5Padding($str) : $this->addPKCS7Padding($str);
        }
        //BASE64
        if ($this->base64Encoding)
        {
            $_str = base64_encode($str);
        }
        else
        {
            $_str = $str;
        }
        return mcrypt_encrypt($this->cipher, $this->key, $_str, $this->mode, $this->iv);
    }

    /**
     * 
     * @param string $str 
     * @return string 
     */
    public function decode($str)
    {
        $ret = mcrypt_decrypt($this->cipher, $this->key, $str, $this->mode, $this->iv);
        //
        if ($this->padding)
        {
            $ret =  self::PADDING_PKCS5 == $this->padding ? $this->stripPKCS5Padding($ret) : $this->stripPKCS7Padding($ret);
        }
        //BASE64
        if ($this->base64Encoding)
        {
            return base64_decode($ret);
        }
        else
        {
            return $ret;
        }
    }

    public function addPKCS5Padding($souce)
    {
        $pad = $this->blockSize - (strlen($souce) % $this->blockSize);
        return $souce . str_repeat(chr($pad), $pad);
    }

    public function stripPKCS5Padding($source)
    {
        $pad = ord($source{strlen($source) - 1});
        if ($pad > strlen($source))
        {
            return false;
        }
        if (strspn($source, chr($pad), strlen($source) - $pad) != $pad)
        {
            return false;
        }
        $ret = substr($source, 0, -1 * $pad);
        return $ret;
    }

    public function addPKCS7Padding($source)
    {
        $pad = $this->blockSize - (strlen($source) % $this->blockSize);
        if ($pad <= $this->blockSize)
        {
            $char = chr($pad);
            $source .= str_repeat($char, $pad);
        }
        return $source;
    }

    public function stripPKCS7Padding($source)
    {
        $char = substr($source, -1, 1);
        $num = ord($char);
        if ($num > 8)
        {
            return $source;
        }
        $len = strlen($source);
        for ($i = $len - 1; $i >= $len - $num; $i--)
        {
            if (ord(substr($source, $i, 1)) != $num)
            {
                return $source;
            }
        }
        $source = substr($source, 0, -$num);
        return $source;
    }
}

Function Calls

None

Variables

None

Stats

MD5 cf504e052a574a79a995eb006ff41c2c
Eval Count 0
Decode Time 88 ms