Agreed way:

aes-128-ecb mode, pkcs7 padding, base64

Problems encountered:

When key is less than or equal to 16 bits, calling openssl_encrypt($text, 'aes-128-ecb', $key) has the same result, and when key is greater than 16 bits, the result is inconsistent.

Solve the problem:

When key is greater than 16 bits, use 256-bit encryption instead.

 <?php

class Encrypter
{
    public static function encrypt($key, $text)
    {
        if (strlen($key) <= 16) {
            $algo = 'AES-128-ECB';
        } else {
            $algo = 'AES-256-ECB';
        }

        $data = openssl_encrypt($text, $algo, $key);

        return $data;
    }
}

church
3.6k 声望67 粉丝