<?php

namespace app\models\mikrotik;

class MikrotikCrypt {

    public function Decrypt($input, $key) {
        $td = mcrypt_module_open('des', '', 'ecb', '');
        $key = substr($key, 0, mcrypt_enc_get_key_size($td));
        $iv_size = mcrypt_enc_get_iv_size($td);
        $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
        if (mcrypt_generic_init($td, $key, $iv) != -1) {
            mcrypt_generic_init($td, $key, $iv);
            $p_t = mdecrypt_generic($td, $input);
            return $p_t;
            mcrypt_generic_deinit($td);
            mcrypt_module_close($td);
        }
    }

    public function Crypt($input, $key) {
        $td = mcrypt_module_open('des', '', 'ecb', '');
        $key = substr($key, 0, mcrypt_enc_get_key_size($td));
        $iv_size = mcrypt_enc_get_iv_size($td);
        $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
        if (mcrypt_generic_init($td, $key, $iv) != -1) {
            $c_t = mcrypt_generic($td, $input);
            mcrypt_generic_deinit($td);
            return $c_t;
            mcrypt_generic_deinit($td);
            mcrypt_module_close($td);
        }
    }

}
