Advertisement
Guest User

Untitled

a guest
Jun 10th, 2016
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.93 KB | None | 0 0
  1. <?php
  2. function encrypt($decrypted, $password, $salt='!kQm*fF3pXe1Kbm%9') {
  3. // Build a 256-bit $key which is a SHA256 hash of $salt and $password.
  4. $key = hash('SHA256', $salt . $password, true);
  5. // Build $iv and $iv_base64. We use a block size of 128 bits (AES compliant) and CBC mode. (Note: ECB mode is inadequate as IV is not used.)
  6. srand(); $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC), MCRYPT_RAND);
  7. if (strlen($iv_base64 = rtrim(base64_encode($iv), '=')) != 22) return false;
  8. // Encrypt $decrypted and an MD5 of $decrypted using $key. MD5 is fine to use here because it's just to verify successful decryption.
  9. $encrypted = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $decrypted . md5($decrypted), MCRYPT_MODE_CBC, $iv));
  10. // We're done!
  11. return $iv_base64 . $encrypted;
  12. }
  13.  
  14. function decrypt($encrypted, $password, $salt='!kQm*fF3pXe1Kbm%9') {
  15. // Build a 256-bit $key which is a SHA256 hash of $salt and $password.
  16. $key = hash('SHA256', $salt . $password, true);
  17. // Retrieve $iv which is the first 22 characters plus ==, base64_decoded.
  18. $iv = base64_decode(substr($encrypted, 0, 22) . '==');
  19. // Remove $iv from $encrypted.
  20. $encrypted = substr($encrypted, 22);
  21. // Decrypt the data. rtrim won't corrupt the data because the last 32 characters are the md5 hash; thus any \0 character has to be padding.
  22. $decrypted = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, base64_decode($encrypted), MCRYPT_MODE_CBC, $iv), "\0\4");
  23. // Retrieve $hash which is the last 32 characters of $decrypted.
  24. $hash = substr($decrypted, -32);
  25. // Remove the last 32 characters from $decrypted.
  26. $decrypted = substr($decrypted, 0, -32);
  27. // Integrity check. If this fails, either the data is corrupted, or the password/salt was incorrect.
  28. if (md5($decrypted) != $hash) return false;
  29. // Yay!
  30. return $decrypted;
  31. }
  32.  
  33. //Dont change the salt, the salt will be same
  34.  
  35. // lets say you let your user sign up, he will enter password = "mybroeddie", and year of birth = "1991"
  36.  
  37. //when he submits the form, you get those values in your php and then do this
  38.  
  39. $password = sha1(md5("mybroeddie")); //assume we have someting like this
  40. $year = "1991";
  41.  
  42. //NOW,
  43.  
  44. $userskey = base64_encode($password . $year);
  45.  
  46. //so now you have the users key.
  47. // lets say you want to encrypt their firstname
  48.  
  49. $firstname = "Eddie Brewer";
  50.  
  51. $value_in_db = encrypt($firstname, $userskey); //HtPigqENKqg/aLtv5vIB0gsuFKHr8yS12hDK+SWWEFWUhCnKK+aznYrLxTU8cRF2DZpkCdGXVF9hWXX6J3ZuSG <-- Save this to your database
  52.  
  53. //so now when ever user wants to see their data, you give them 2 text boxes and ask them to enter their password and year of birth.
  54.  
  55. //CASE 1 : They enter wrongly
  56.  
  57. $inputpasswordbyuser = "mypass";
  58.  
  59. // then you would do the exact same stuff which you did before
  60.  
  61. $password = sha1(md5($inputpasswordbyuser));
  62. $year = "1991"; //assuming he gets the year right somehow
  63.  
  64.  
  65. $userskey = base64_encode($password . $year); //so now your system made the key with the wrongly provided stuff, lets see if this can decrypt your data;
  66.  
  67. $yourdatafromdb = "HtPigqENKqg/aLtv5vIB0gsuFKHr8yS12hDK+SWWEFWUhCnKK+aznYrLxTU8cRF2DZpkCdGXVF9hWXX6J3ZuSG";
  68.  
  69. //echo decrypt($yourdatafromdb, $userskey);
  70.  
  71.  
  72.  
  73. //CASE 2 : They enter CORRECTLY
  74.  
  75. $inputpasswordbyuser = "mybroeddie";
  76.  
  77. // then you would do the exact same stuff which you did before
  78.  
  79. $password = sha1(md5($inputpasswordbyuser));
  80. $year = "1991"; //assuming he gets the year right somehow
  81.  
  82.  
  83. $userskey = base64_encode($password . $year); //so now your system made the key with the wrongly provided stuff, lets see if this can decrypt your data;
  84.  
  85. $yourdatafromdb = "HtPigqENKqg/aLtv5vIB0gsuFKHr8yS12hDK+SWWEFWUhCnKK+aznYrLxTU8cRF2DZpkCdGXVF9hWXX6J3ZuSG";
  86.  
  87. echo decrypt($yourdatafromdb, $userskey);
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97. //$res = encrypt("myssn123", "mysecretkey");
  98. //echo "ENCRYTPTED STUFF IS : " . $res;
  99.  
  100. //echo "<br/><br/>DECRYPTED STUFF IS : " . decrypt($res, "mysecretkey");
  101.  
  102.  
  103. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement