<?php


namespace App\Common\Library;

class DES
{
    private $key;
    private $encryptor;

    public function __construct($key)
    {
        $this->key = $this->complement($key);
        $this->encryptor = new Encryptor($key);
    }

    public function complement($key)
    {
        if (strlen($key) < 8) {
            $len = strlen($key) % 8;
            if ($len == 0) {
                return $key;
            } else {
                $j = 8 - $len;
                for ($i = 0; $i < $j; $i++) {
                    $key .= "\0";
                }
                return $key;
            }
        } else {
            return $key;
        }
    }

    /**
     * @param $encrypt
     * @return string
     */
    public function encrypt($encrypt)
    {
        return $this->encryptor->encrypt($encrypt);
    }


    /**
     * @param $decrypt
     * @return string
     */
    public function decrypt($decrypt)
    {
        return $this->encryptor->decrypt($decrypt);
    }
}
