Created
May 25, 2023 20:46
-
-
Save imacruz/af29a1ce5599f02126fc41d84855dbc0 to your computer and use it in GitHub Desktop.
Criptograr e descriptografar usando aes_encript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
private function getAppKey(): string | |
{ | |
return config("app.aes_key"); | |
} | |
function encrypt(string $data): string | |
{ | |
$key = $this->getAppKey(); | |
$method = 'aes-128-ecb'; | |
$ivSize = openssl_cipher_iv_length($method); | |
$iv = openssl_random_pseudo_bytes($ivSize); | |
$encrypted = openssl_encrypt($data, $method, $key, OPENSSL_RAW_DATA, $iv); | |
$encrypted = strtoupper(implode(null, unpack('H*', $encrypted))); | |
return $encrypted; | |
} | |
function decrypt(string $data): string | |
{ | |
$key = $this->getAppKey(); | |
$method = 'aes-128-ecb'; | |
$data = pack('H*', $data); | |
$ivSize = openssl_cipher_iv_length($method); | |
$iv = $iv = openssl_random_pseudo_bytes($ivSize); | |
$decrypted = openssl_decrypt($data, $method, $key, OPENSSL_RAW_DATA, $iv); | |
return trim($decrypted); | |
} | |
public function scopeNome($query, $value) | |
{ | |
return $query->whereRaw("LOWER(CONVERT(CAST(CONVERT(AES_DECRYPT(UNHEX(nome), ?) USING UTF8) AS BINARY) USING UTF8)) LIKE ?", [$this->getAppKey(),'%'.strtolower($value).'%']); | |
} |
Usando mysql e maria DB até o momento presente tudo ok
Atributo no banco devera ser varbinary(255) ou blob
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Exemplos de SQL utilizando a conversão: