Advertisement
BimoSora

Encrypt dan Decrypt

Nov 3rd, 2019
341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.78 KB | None | 0 0
  1. <li>bin2hex
  2. <br/>
  3. <?php
  4. $str = bin2hex('Ini siapa?');
  5. echo $str;
  6. echo '<br/>';
  7. echo hex2bin('496e692073696170613f');
  8. ?>
  9. </li>
  10. <br/>
  11.  
  12. <li>based64
  13. <br/>
  14. <?php
  15. $data = "Tutorial base64 di php www.malasngoding.com";
  16. echo base64_encode($data);
  17. echo '<br/>';
  18. $data2 = "VHV0b3JpYWwgYmFzZTY0IGRpIHBocCB3d3cubWFsYXNuZ29kaW5nLmNvbQ==";
  19. echo base64_decode($data2);
  20. ?>
  21. </li>
  22. <br/>
  23.  
  24. <li>sha1
  25. <br/>
  26. <?php
  27. $str3 = "Hello";
  28. echo sha1($str3);
  29.  
  30. if (sha1($str3) == "f7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0")
  31. {
  32. echo "<br>Hello";
  33. }
  34. ?>
  35. </li>
  36. <br/>
  37.  
  38. <li>md5
  39. <br/>
  40. <?php
  41. $str4 = "Hello";
  42. echo md5($str4);
  43.  
  44. if (md5($str4) == "8b1a9953c4611296a827abf8c47804d7")
  45. {
  46. echo "<br>Hello";
  47. }
  48. ?>
  49. </li>
  50. <br/>
  51.  
  52. <li>Dibutuhkan
  53. <br/>
  54. sudo apt install php-dev libmcrypt-dev gcc make autoconf libc-dev pkg-config
  55. <br/>
  56. sudo pecl install mcrypt-1.0.1
  57. <br/>
  58. echo 'extension=mcrypt.so' | sudo tee -a /etc/php/7.3/apache2/conf.d/mcrypt.ini
  59. </li>
  60.  
  61. <li>mcrypt_encrypt
  62.  
  63. <?php
  64. $key = 'aNdRgUkXp2s5v8y/B?E(H+MbQeShVmYq';
  65. $kunci = 'Ini siapa?';
  66. $encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $kunci, MCRYPT_MODE_ECB);
  67. echo '<br/>';
  68. echo $encrypted;
  69. ?>
  70. </li>
  71.  
  72. <li>mcrypt_decrypt
  73. <?php
  74. echo '<br/>';
  75. $decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $encrypted, MCRYPT_MODE_ECB);
  76. echo $decrypted;
  77. ?>
  78. </li>
  79. <li>mcrypt_encrypt dan mcrypt_decrypt
  80. <br/>
  81. <?php
  82. $Pass = "aNdRgUkXp2s5v8y/B?E(H+MbQeShVmYq";
  83. $Clear = "Klartext";
  84.  
  85. $crypted = fnEncrypt($Clear, $Pass);
  86. echo "Encrypted: ".$crypted."</br>";
  87.  
  88. $newClear = fnDecrypt($crypted, $Pass);
  89. echo "Decrypted: ".$newClear."</br>";
  90.  
  91. function fnEncrypt($sValue, $sSecretKey)
  92. {
  93. return rtrim(
  94. base64_encode(
  95. mcrypt_encrypt(
  96. MCRYPT_RIJNDAEL_256,
  97. $sSecretKey, $sValue,
  98. MCRYPT_MODE_ECB,
  99. mcrypt_create_iv(
  100. mcrypt_get_iv_size(
  101. MCRYPT_RIJNDAEL_256,
  102. MCRYPT_MODE_ECB
  103. ),
  104. MCRYPT_RAND)
  105. )
  106. ), "\0"
  107. );
  108. }
  109.  
  110. function fnDecrypt($sValue, $sSecretKey)
  111. {
  112. return rtrim(
  113. mcrypt_decrypt(
  114. MCRYPT_RIJNDAEL_256,
  115. $sSecretKey,
  116. base64_decode($sValue),
  117. MCRYPT_MODE_ECB,
  118. mcrypt_create_iv(
  119. mcrypt_get_iv_size(
  120. MCRYPT_RIJNDAEL_256,
  121. MCRYPT_MODE_ECB
  122. ),
  123. MCRYPT_RAND
  124. )
  125. ), "\0"
  126. );
  127. }
  128. ?>
  129. </li>
  130. <li>mcrypt_encrypt dan mcrypt_decrypt
  131. <br/>
  132. <?php
  133. define("ENCRYPTION_KEY", "!@#$%^&*");
  134. $string = "Ini siapa?";
  135.  
  136. echo $encrypted = encrypt($string, ENCRYPTION_KEY);
  137. echo "<br />";
  138. echo $decrypted = decrypt($encrypted, ENCRYPTION_KEY);
  139.  
  140. /**
  141. * Returns an encrypted & utf8-encoded
  142. */
  143. function encrypt($pure_string, $encryption_key) {
  144. $iv_size = mcrypt_get_iv_size(MCRYPT_BLOWFISH, MCRYPT_MODE_ECB);
  145. $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
  146. $encrypted_string = mcrypt_encrypt(MCRYPT_BLOWFISH, $encryption_key, utf8_encode($pure_string), MCRYPT_MODE_ECB, $iv);
  147. return $encrypted_string;
  148. }
  149.  
  150. /**
  151. * Returns decrypted original string
  152. */
  153. function decrypt($encrypted_string, $encryption_key) {
  154. $iv_size = mcrypt_get_iv_size(MCRYPT_BLOWFISH, MCRYPT_MODE_ECB);
  155. $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
  156. $decrypted_string = mcrypt_decrypt(MCRYPT_BLOWFISH, $encryption_key, $encrypted_string, MCRYPT_MODE_ECB, $iv);
  157. return $decrypted_string;
  158. }
  159. ?>
  160. </li>
  161. <br/>
  162.  
  163. <li> openssl encrypt
  164. <br/>
  165. <?php
  166. class Openssl_EncryptDecrypt {
  167. function encrypt ($pure_string, $encryption_key) {
  168. $cipher = 'AES-256-CBC';
  169. $options = OPENSSL_RAW_DATA;
  170. $hash_algo = 'sha256';
  171. $sha2len = 32;
  172. $ivlen = openssl_cipher_iv_length($cipher);
  173. $iv = openssl_random_pseudo_bytes($ivlen);
  174. $ciphertext_raw = openssl_encrypt($pure_string, $cipher, $encryption_key, $options, $iv);
  175. $hmac = hash_hmac($hash_algo, $ciphertext_raw, $encryption_key, true);
  176. return $iv.$hmac.$ciphertext_raw;
  177. }
  178. function decrypt ($encrypted_string, $encryption_key) {
  179. $cipher = 'AES-256-CBC';
  180. $options = OPENSSL_RAW_DATA;
  181. $hash_algo = 'sha256';
  182. $sha2len = 32;
  183. $ivlen = openssl_cipher_iv_length($cipher);
  184. $iv = substr($encrypted_string, 0, $ivlen);
  185. $hmac = substr($encrypted_string, $ivlen, $sha2len);
  186. $ciphertext_raw = substr($encrypted_string, $ivlen+$sha2len);
  187. $original_plaintext = openssl_decrypt($ciphertext_raw, $cipher, $encryption_key, $options, $iv);
  188. $calcmac = hash_hmac($hash_algo, $ciphertext_raw, $encryption_key, true);
  189. if(function_exists('hash_equals')) {
  190. if (hash_equals($hmac, $calcmac)) return $original_plaintext;
  191. } else {
  192. if ($this->hash_equals_custom($hmac, $calcmac)) return $original_plaintext;
  193. }
  194. }
  195. /**
  196. * (Optional)
  197. * hash_equals() function polyfilling.
  198. * PHP 5.6+ timing attack safe comparison
  199. */
  200. function hash_equals_custom($knownString, $userString) {
  201. if (function_exists('mb_strlen')) {
  202. $kLen = mb_strlen($knownString, '8bit');
  203. $uLen = mb_strlen($userString, '8bit');
  204. } else {
  205. $kLen = strlen($knownString);
  206. $uLen = strlen($userString);
  207. }
  208. if ($kLen !== $uLen) {
  209. return false;
  210. }
  211. $result = 0;
  212. for ($i = 0; $i < $kLen; $i++) {
  213. $result |= (ord($knownString[$i]) ^ ord($userString[$i]));
  214. }
  215. return 0 === $result;
  216. }
  217. }
  218.  
  219. define('ENCRYPTION_KEY', '__^%&Q@$&*!@#$%^&*^__');
  220. $string = "Ini siapa?";
  221.  
  222. $OpensslEncryption = new Openssl_EncryptDecrypt;
  223. $encrypted = $OpensslEncryption->encrypt($string, ENCRYPTION_KEY);
  224. $decrypted = $OpensslEncryption->decrypt($encrypted, ENCRYPTION_KEY);
  225. echo $encrypted;
  226. echo '<br/>';
  227. echo $decrypted;
  228. ?>
  229. </li>
  230. <br/>
  231.  
  232. <li> aes-128, sha256, openssl
  233. <br/>
  234. <?php
  235. $token = "Ini siapa?";
  236.  
  237. $cipher_method = 'aes-128-ctr';
  238. $enc_key = openssl_digest(php_uname(), 'SHA256', TRUE);
  239. $enc_iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length($cipher_method));
  240. $crypted_token = openssl_encrypt($token, $cipher_method, $enc_key, 0, $enc_iv) . "::" . bin2hex($enc_iv);
  241. echo $crypted_token;
  242. //unset($token, $cipher_method, $enc_key, $enc_iv);
  243. echo '<br/>';
  244. // Decrypt text --
  245.  
  246. list($crypted_token, $enc_iv) = explode("::", $crypted_token);
  247. $cipher_method = 'aes-128-ctr';
  248. $enc_key = openssl_digest(php_uname(), 'SHA256', TRUE);
  249. $token = openssl_decrypt($crypted_token, $cipher_method, $enc_key, 0, hex2bin($enc_iv));
  250. echo $token;
  251. ?>
  252. </li>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement