Well i ran into the same bug bro and i tried to fix it so this is my solution that worked very well for me.
- Create a new key after setting your starter key @ App\Config\Encryption.php
- Create an encoded key:
Setting Your Encryption Key
Your encryption key must be as long as the encryption algorithm in use allows. For AES-256, that’s 256 bits or 32 bytes (characters) long.
The key should be as random as possible, and it must not be a regular text string, nor the output of a hashing function, etc. To create a proper key, you can use the Encryption library’s createKey() method.
// $key will be assigned a 32-byte (256-bit) random key
$key = Encryption::createKey(32);
The key can be stored in app/Config/Encryption.php, or you can design a storage mechanism of your own and pass the key dynamically when encrypting/decrypting.
To save your key to your app/Config/Encryption.php, open the file and set:
public $key = 'YOUR KEY';
Note: passing the generated key here by copy and past will damage the binary representation so continue reading blow
Encoding Keys or Results
You’ll notice that the createKey() method outputs binary data, which is hard to deal with (i.e. a copy-paste may damage it), so you may use bin2hex(), hex2bin() or Base64-encoding
to work with the key in a more friendly manner. For example:
// Get a hex-encoded representation of the key:
$encoded = bin2hex(Encryption::createKey(32));
echo $encoded
If you use base64_encode() make sure to use the base64_decode() in the constructor below and in encrypting your messages
e.g:
// if you use base64_encode do this
$message = 'some message to encode';
$encrypter = \Config\Services::encrypter();
$encodedMsg = base64_encode($encrypter->encrypt($message));
// decrypt the message
$txt = $encrypter->decrypt(base64_decode($encodedMsg))
echo $txt;
// and in your App\Config\Encryption.php Constructor that will
// dynamically decode the key to binary safe use
base64_decode($encodedKey)
// only if you encode the key using the
base64_encode($key)
//And if you use
bin2hex($key)
// during key creation in the constructor use:
hex2bin($encodedKey)
// and when transporting your message over url use:
bin2hex($encodedMessageFromEncrypter->encrypt($msg))
// and decode it using
hex2bin($transportedMessage)
#Passing the key dynamically
in your App\Config\Encryption.php
// create a constructor that will dynamically Put the same value in your config with hex2bin(),
// so that it is still passed as binary to the library:
like this:
public function __construct(){
//copy the encoded key and pass it in here
$this->key = hex2bin($encoded);
}
Then try to encrypt and decrypt the text again!