Skip to content

Instantly share code, notes, and snippets.

@aditagusta
Created July 24, 2021 06:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aditagusta/2227d4e83d1118210353f1c013ca5f6d to your computer and use it in GitHub Desktop.
Save aditagusta/2227d4e83d1118210353f1c013ca5f6d to your computer and use it in GitHub Desktop.
PHP_AES_CHIPER
class PHP_AES_Cipher {
private static $OPENSSL_CIPHER_NAME = "aes-128-cbc"; //Name of OpenSSL Cipher
private static $CIPHER_KEY_LEN = 16; //128 bits
/**
* Encrypt data using AES Cipher (CBC) with 128 bit key
*
* @param type $key - key to use should be 16 bytes long (128 bits)
* @param type $iv - initialization vector
* @param type $data - data to encrypt
* @return encrypted data in base64 encoding with iv attached at end after a :
*/
static function encryp_my($key, $iv, $data) {
if (strlen($key) < PHP_AES_Cipher::$CIPHER_KEY_LEN) {
$key = str_pad("$key", PHP_AES_Cipher::$CIPHER_KEY_LEN, "0"); //0 pad to len 16
} else if (strlen($key) > PHP_AES_Cipher::$CIPHER_KEY_LEN) {
$key = substr($key, 0, PHP_AES_Cipher::$CIPHER_KEY_LEN); //truncate to 16 bytes
}
$encodedEncryptedData = base64_encode(openssl_encrypt($data, PHP_AES_Cipher::$OPENSSL_CIPHER_NAME, $key, OPENSSL_RAW_DATA, $iv));
$encodedIV = base64_encode($iv);
$encryptedPayload = $encodedEncryptedData.":".$encodedIV;
return $encryptedPayload;
}
/**
* Decrypt data using AES Cipher (CBC) with 128 bit key
*
* @param type $key - key to use should be 16 bytes long (128 bits)
* @param type $data - data to be decrypted in base64 encoding with iv attached at the end after a :
* @return decrypted data
*/
static function decrypt_my($key, $data) {
if (strlen($key) < PHP_AES_Cipher::$CIPHER_KEY_LEN) {
$key = str_pad("$key", PHP_AES_Cipher::$CIPHER_KEY_LEN, "0"); //0 pad to len 16
} else if (strlen($key) > PHP_AES_Cipher::$CIPHER_KEY_LEN) {
$key = substr($key, 0, PHP_AES_Cipher::$CIPHER_KEY_LEN); //truncate to 16 bytes
}
$parts = explode(':', $data); //Separate Encrypted data from iv.
$decryptedData = openssl_decrypt(base64_decode($parts[0]), PHP_AES_Cipher::$OPENSSL_CIPHER_NAME, $key, OPENSSL_RAW_DATA, base64_decode($parts[1]));
return $decryptedData;
}
}
function generateRandomString($length = 24) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}
//Cara Penggunaannya Encrypt
~===========================~
$iv = generateRandomString(16); #Same as in JAVA
$key = generateRandomString(24).'=='; #Same as in JAVA
$data = "data yang di encrypt";
$data_encrypt = \PHP_AES_Cipher::encryp_my($key, $iv, $data).':'.$key;
//Cara Penggunaannya Decrypt
~===========================~
$data_encrypt ="MWCkYOtUSlc5Be/enZAIZ0mGf6hex/gefyqB/HFNXEo=:ODlrY055MDRieHBteDNmUg==:T5MaTCSPq40TdgxiGQkjuA1r==";
$data = substr($data_encrypt, 0, strlen($data_encrypt) -27);
$key = substr($data_encrypt, -26);
$data_decrypt = \PHP_AES_Cipher::decrypt_my($key, $data);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment