Skip to content

Instantly share code, notes, and snippets.

@appwebd
Last active July 22, 2021 17:45
Show Gist options
  • Save appwebd/5a13e4e9091abf476aca84a29aff445e to your computer and use it in GitHub Desktop.
Save appwebd/5a13e4e9091abf476aca84a29aff445e to your computer and use it in GitHub Desktop.
[Encriptar / desencriptar AES-256-CBC en kumbiaPHP] #Encrypt & #Decrypt #php
// En archivo de configuración kumbiaPHP default/app/config
//
// crear la variable secret_iv bajo authentication, por ejemplo:
//
// [authentication]
// secret_key = "z?&vbX$4\N`}@vo=~)\?eBFyc&;J.Nx-58/dU8\)"
// copy/paste este código para un controlador de acceso global
//
const ENCRYPT_METHOD = 'AES-256-CBC';
const SECRET_IV = 'MAPt=&L3>*m@=K;UyU`2fGcT\e[#{LiUha@_/yF%';
const SHA256 = 'sha256';
public static function stringEncode($string)
{
$encrypt_method = self::ENCRYPT_METHOD;
$secret_iv = self::SECRET_IV;
$secret_key = config::get("config.authentication.secret_key");
// hash
$key = hash(self::SHA256, $secret_key);
// iv - encrypt method AES-256-CBC expects 16 bytes - else you will get a warning
$iv = substr(hash(self::SHA256, $secret_iv), 0, 16);
$output = openssl_encrypt($string, $encrypt_method, $key, 0, $iv);
return base64_encode($output);
}
public static function stringDecode($string)
{
$encrypt_method = self::ENCRYPT_METHOD;
$secret_iv = self::SECRET_IV;
$secret_key = config::get("config.authentication.secret_key");
// hash
$key = hash(self::SHA256, $secret_key);
// iv - encrypt method AES-256-CBC expects 16 bytes - else you will get a warning
$iv = substr(hash(self::SHA256, $secret_iv), 0, 16);
return openssl_decrypt(base64_decode($string), $encrypt_method, $key, 0, $iv);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment