<commit_before><?php

namespace VMelnik\DoctrineEncryptBundle\Encryptors;

/**
 * Class for AES256 encryption
 *
 * @author AlexanderC <<EMAIL>>
 */
class AES128MysqlCompatibleEncryptor implements EncryptorInterface
{

    /**
     * @var string
     */
    private $secretKey;

    /**
     * @var string
     */
    private $initializationVector;

    /**
     * {@inheritdoc}
     */
    public function __construct($key)
    {
        $this->secretKey = $this->mysqlAesKey($key);

        $this->initializationVector = mcrypt_create_iv(
            mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB),
            MCRYPT_DEV_URANDOM
        );
    }

    /**
     * {@inheritdoc}
     */
    public function encrypt($data)
    {
        // skip if not string
        if(!is_string($data) && !is_array($data)) {
            return $data;
        }

        if(is_array($data)) {
            $encodedArray = [];

            foreach($data as $key => $item) {
                $encodedArray[$key] = $this->doEncrypt($item);
            }

            return $encodedArray;
        }
        
        // check if data is already encrypted
        if(is_string($data)) {
            if($rawData = $this->doDecrypt($data)) {
                if($data === $this->doEncrypt($rawData)) {
                    return $data;
                }
            }
        }

        return $this->doEncrypt($data);
    }

    /**
     * @param string $data
     * @return string
     */
    protected function doEncrypt($data)
    {
        return base64_encode(
            mcrypt_encrypt(
                MCRYPT_RIJNDAEL_128,
                $this->secretKey,
                $data,
                MCRYPT_MODE_ECB,
                $this->initializationVector
            )
        );
    }

    /**
     * {@inheritdoc}
     */
    public function decrypt($data)
    {
        // skip if not string
        if(!is_string($data) && !is_array($data)) {
            return $data;
        }

        if(is_array($data)) {
            $decodedArray = [];

            foreach($data as $key => $item) {
                $decodedArray[$key] = $this->doDecrypt($item);
            }

            return $decodedArray;
        }

        return $this->doDecrypt($data);
    }

    /**
     * @param string $data
     * @return string
     */
    protected function doDecrypt($data)
    {
        $decodedData = base64_decode($data, true);

        // do not decode if broken or not base64
        if(false === $decodedData) {
            return $data;
        }

        $decryptedData = mcrypt_decrypt(
                MCRYPT_RIJNDAEL_128,
                $this->secretKey,
                $decodedData,
                MCRYPT_MODE_ECB,
                $this->initializationVector
            );

        $decryptedData = mb_convert_encoding($decryptedData, 'UTF-8', 'UTF-8');

        return trim(preg_replace(
            '/[\x00-\x08\x0B\x0C\x0E-\x1F\x80-\x9F]/u', 
            '', 
            $decryptedData
        ));
    }

    /**
     * @param string $key
     * @return string
     */
    protected function mysqlAesKey($key)
    {
        $newKey = str_repeat(chr(0), 16);

        for($i = 0, $len = strlen($key); $i < $len; $i++) {
            $newKey[$i % 16] = $newKey[$i % 16] ^ $key[$i];
        }

        return $newKey;
    }
}
<commit_msg>try fixing some issues<commit_after><?php

namespace VMelnik\DoctrineEncryptBundle\Encryptors;

/**
 * Class for AES256 encryption
 *
 * @author AlexanderC <<EMAIL>>
 */
class AES128MysqlCompatibleEncryptor implements EncryptorInterface
{

    /**
     * @var string
     */
    private $secretKey;

    /**
     * @var string
     */
    private $initializationVector;

    /**
     * {@inheritdoc}
     */
    public function __construct($key)
    {
        $this->secretKey = $this->mysqlAesKey($key);

        $this->initializationVector = mcrypt_create_iv(
            mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB),
            MCRYPT_DEV_URANDOM
        );
    }

    /**
     * {@inheritdoc}
     */
    public function encrypt($data)
    {
        // skip if not string
        if(!is_string($data) && !is_array($data)) {
            return $data;
        }

        if(is_array($data)) {
            $encodedArray = [];

            foreach($data as $key => $item) {
                $encodedArray[$key] = $this->doEncrypt($item);
            }

            return $encodedArray;
        }
        
        // check if data is already encrypted
        if(is_string($data)) {
            if($rawData = $this->doDecrypt($data) && $rawData !== $data) {
                if($data === $this->doEncrypt($rawData)) {
                    return $data;
                }
            }
        }

        return $this->doEncrypt($data);
    }

    /**
     * @param string $data
     * @return string
     */
    protected function doEncrypt($data)
    {
        return base64_encode(
            mcrypt_encrypt(
                MCRYPT_RIJNDAEL_128,
                $this->secretKey,
                $data,
                MCRYPT_MODE_ECB,
                $this->initializationVector
            )
        );
    }

    /**
     * {@inheritdoc}
     */
    public function decrypt($data)
    {
        // skip if not string
        if(!is_string($data) && !is_array($data)) {
            return $data;
        }

        if(is_array($data)) {
            $decodedArray = [];

            foreach($data as $key => $item) {
                $decodedArray[$key] = $this->doDecrypt($item);
            }

            return $decodedArray;
        }

        return $this->doDecrypt($data);
    }

    /**
     * @param string $data
     * @return string
     */
    protected function doDecrypt($data)
    {
        $decodedData = base64_decode($data, true);

        // do not decode if broken or not base64
        if(false === $decodedData) {
            return $data;
        }

        $decryptedData = mcrypt_decrypt(
                MCRYPT_RIJNDAEL_128,
                $this->secretKey,
                $decodedData,
                MCRYPT_MODE_ECB,
                $this->initializationVector
            );

        // WTF is this shi*t???
        //$decryptedData = mb_convert_encoding($decryptedData, 'UTF-8', 'UTF-8');

        return trim(preg_replace(
            '/[\x00-\x08\x0B\x0C\x0E-\x1F\x80-\x9F]/u', 
            '', 
            $decryptedData
        ));
    }

    /**
     * @param string $key
     * @return string
     */
    protected function mysqlAesKey($key)
    {
        $newKey = str_repeat(chr(0), 16);

        for($i = 0, $len = strlen($key); $i < $len; $i++) {
            $newKey[$i % 16] = $newKey[$i % 16] ^ $key[$i];
        }

        return $newKey;
    }
}

{'fraction_non_alphanumeric': 0.1343504665975411, 'fraction_numerical': 0.012442601096133906, 'mean_word_length': 1.4887578326575746, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6604299', 'n_tokens_mistral': 2214, 'n_tokens_neox': 2046, 'n_words': 355}