Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <li>bin2hex
- <br/>
- <?php
- $str = bin2hex('Ini siapa?');
- echo $str;
- echo '<br/>';
- echo hex2bin('496e692073696170613f');
- ?>
- </li>
- <br/>
- <li>based64
- <br/>
- <?php
- $data = "Tutorial base64 di php www.malasngoding.com";
- echo base64_encode($data);
- echo '<br/>';
- $data2 = "VHV0b3JpYWwgYmFzZTY0IGRpIHBocCB3d3cubWFsYXNuZ29kaW5nLmNvbQ==";
- echo base64_decode($data2);
- ?>
- </li>
- <br/>
- <li>sha1
- <br/>
- <?php
- $str3 = "Hello";
- echo sha1($str3);
- if (sha1($str3) == "f7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0")
- {
- echo "<br>Hello";
- }
- ?>
- </li>
- <br/>
- <li>md5
- <br/>
- <?php
- $str4 = "Hello";
- echo md5($str4);
- if (md5($str4) == "8b1a9953c4611296a827abf8c47804d7")
- {
- echo "<br>Hello";
- }
- ?>
- </li>
- <br/>
- <li>Dibutuhkan
- <br/>
- sudo apt install php-dev libmcrypt-dev gcc make autoconf libc-dev pkg-config
- <br/>
- sudo pecl install mcrypt-1.0.1
- <br/>
- echo 'extension=mcrypt.so' | sudo tee -a /etc/php/7.3/apache2/conf.d/mcrypt.ini
- </li>
- <li>mcrypt_encrypt
- <?php
- $key = 'aNdRgUkXp2s5v8y/B?E(H+MbQeShVmYq';
- $kunci = 'Ini siapa?';
- $encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $kunci, MCRYPT_MODE_ECB);
- echo '<br/>';
- echo $encrypted;
- ?>
- </li>
- <li>mcrypt_decrypt
- <?php
- echo '<br/>';
- $decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $encrypted, MCRYPT_MODE_ECB);
- echo $decrypted;
- ?>
- </li>
- <li>mcrypt_encrypt dan mcrypt_decrypt
- <br/>
- <?php
- $Pass = "aNdRgUkXp2s5v8y/B?E(H+MbQeShVmYq";
- $Clear = "Klartext";
- $crypted = fnEncrypt($Clear, $Pass);
- echo "Encrypted: ".$crypted."</br>";
- $newClear = fnDecrypt($crypted, $Pass);
- echo "Decrypted: ".$newClear."</br>";
- function fnEncrypt($sValue, $sSecretKey)
- {
- return rtrim(
- base64_encode(
- mcrypt_encrypt(
- MCRYPT_RIJNDAEL_256,
- $sSecretKey, $sValue,
- MCRYPT_MODE_ECB,
- mcrypt_create_iv(
- mcrypt_get_iv_size(
- MCRYPT_RIJNDAEL_256,
- MCRYPT_MODE_ECB
- ),
- MCRYPT_RAND)
- )
- ), "\0"
- );
- }
- function fnDecrypt($sValue, $sSecretKey)
- {
- return rtrim(
- mcrypt_decrypt(
- MCRYPT_RIJNDAEL_256,
- $sSecretKey,
- base64_decode($sValue),
- MCRYPT_MODE_ECB,
- mcrypt_create_iv(
- mcrypt_get_iv_size(
- MCRYPT_RIJNDAEL_256,
- MCRYPT_MODE_ECB
- ),
- MCRYPT_RAND
- )
- ), "\0"
- );
- }
- ?>
- </li>
- <li>mcrypt_encrypt dan mcrypt_decrypt
- <br/>
- <?php
- define("ENCRYPTION_KEY", "!@#$%^&*");
- $string = "Ini siapa?";
- echo $encrypted = encrypt($string, ENCRYPTION_KEY);
- echo "<br />";
- echo $decrypted = decrypt($encrypted, ENCRYPTION_KEY);
- /**
- * Returns an encrypted & utf8-encoded
- */
- function encrypt($pure_string, $encryption_key) {
- $iv_size = mcrypt_get_iv_size(MCRYPT_BLOWFISH, MCRYPT_MODE_ECB);
- $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
- $encrypted_string = mcrypt_encrypt(MCRYPT_BLOWFISH, $encryption_key, utf8_encode($pure_string), MCRYPT_MODE_ECB, $iv);
- return $encrypted_string;
- }
- /**
- * Returns decrypted original string
- */
- function decrypt($encrypted_string, $encryption_key) {
- $iv_size = mcrypt_get_iv_size(MCRYPT_BLOWFISH, MCRYPT_MODE_ECB);
- $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
- $decrypted_string = mcrypt_decrypt(MCRYPT_BLOWFISH, $encryption_key, $encrypted_string, MCRYPT_MODE_ECB, $iv);
- return $decrypted_string;
- }
- ?>
- </li>
- <br/>
- <li> openssl encrypt
- <br/>
- <?php
- class Openssl_EncryptDecrypt {
- function encrypt ($pure_string, $encryption_key) {
- $cipher = 'AES-256-CBC';
- $options = OPENSSL_RAW_DATA;
- $hash_algo = 'sha256';
- $sha2len = 32;
- $ivlen = openssl_cipher_iv_length($cipher);
- $iv = openssl_random_pseudo_bytes($ivlen);
- $ciphertext_raw = openssl_encrypt($pure_string, $cipher, $encryption_key, $options, $iv);
- $hmac = hash_hmac($hash_algo, $ciphertext_raw, $encryption_key, true);
- return $iv.$hmac.$ciphertext_raw;
- }
- function decrypt ($encrypted_string, $encryption_key) {
- $cipher = 'AES-256-CBC';
- $options = OPENSSL_RAW_DATA;
- $hash_algo = 'sha256';
- $sha2len = 32;
- $ivlen = openssl_cipher_iv_length($cipher);
- $iv = substr($encrypted_string, 0, $ivlen);
- $hmac = substr($encrypted_string, $ivlen, $sha2len);
- $ciphertext_raw = substr($encrypted_string, $ivlen+$sha2len);
- $original_plaintext = openssl_decrypt($ciphertext_raw, $cipher, $encryption_key, $options, $iv);
- $calcmac = hash_hmac($hash_algo, $ciphertext_raw, $encryption_key, true);
- if(function_exists('hash_equals')) {
- if (hash_equals($hmac, $calcmac)) return $original_plaintext;
- } else {
- if ($this->hash_equals_custom($hmac, $calcmac)) return $original_plaintext;
- }
- }
- /**
- * (Optional)
- * hash_equals() function polyfilling.
- * PHP 5.6+ timing attack safe comparison
- */
- function hash_equals_custom($knownString, $userString) {
- if (function_exists('mb_strlen')) {
- $kLen = mb_strlen($knownString, '8bit');
- $uLen = mb_strlen($userString, '8bit');
- } else {
- $kLen = strlen($knownString);
- $uLen = strlen($userString);
- }
- if ($kLen !== $uLen) {
- return false;
- }
- $result = 0;
- for ($i = 0; $i < $kLen; $i++) {
- $result |= (ord($knownString[$i]) ^ ord($userString[$i]));
- }
- return 0 === $result;
- }
- }
- define('ENCRYPTION_KEY', '__^%&Q@$&*!@#$%^&*^__');
- $string = "Ini siapa?";
- $OpensslEncryption = new Openssl_EncryptDecrypt;
- $encrypted = $OpensslEncryption->encrypt($string, ENCRYPTION_KEY);
- $decrypted = $OpensslEncryption->decrypt($encrypted, ENCRYPTION_KEY);
- echo $encrypted;
- echo '<br/>';
- echo $decrypted;
- ?>
- </li>
- <br/>
- <li> aes-128, sha256, openssl
- <br/>
- <?php
- $token = "Ini siapa?";
- $cipher_method = 'aes-128-ctr';
- $enc_key = openssl_digest(php_uname(), 'SHA256', TRUE);
- $enc_iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length($cipher_method));
- $crypted_token = openssl_encrypt($token, $cipher_method, $enc_key, 0, $enc_iv) . "::" . bin2hex($enc_iv);
- echo $crypted_token;
- //unset($token, $cipher_method, $enc_key, $enc_iv);
- echo '<br/>';
- // Decrypt text --
- list($crypted_token, $enc_iv) = explode("::", $crypted_token);
- $cipher_method = 'aes-128-ctr';
- $enc_key = openssl_digest(php_uname(), 'SHA256', TRUE);
- $token = openssl_decrypt($crypted_token, $cipher_method, $enc_key, 0, hex2bin($enc_iv));
- echo $token;
- ?>
- </li>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement