Guest User

Untitled

a guest
Apr 12th, 2018
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.34 KB | None | 0 0
  1. function CasualPassword($lenght=527){
  2. $available_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
  3. $password = "";
  4. for($i = 0; $i<$lenght; $i++){
  5. $password .= substr($available_chars,rand(0,strlen($available_chars)-1),1);
  6. }
  7. return $password;
  8. }
  9. $key = CasualPassword();
  10. $string = nl2br($_POST['nota']);
  11. for ($i = 0; $i < strlen($string); $i++ ) {
  12. $temp = $string[$i] ^ $key[$i % strlen($key)];
  13. $crypt .= str_pad( dechex( ord( $temp ) ), 2, 0, STR_PAD_LEFT);
  14. }
  15. $encryptednote = $key.'__'.$crypt;
  16.  
  17. $separate = explode("__",$encryptednote);
  18. $key =$separate[0];
  19. $crypt = $separate[1];
  20. $cnt = 0;
  21. for ($i = 0; $i < strlen( $crypt ); $i+=2){
  22. $temp = chr( hexdec( substr( $crypt, $i, 2) ) );
  23. $string .= $temp ^ $key[$cnt % strlen($key)];
  24. $cnt++;
  25. }
  26.  
  27. <?php
  28. include("../co.php");
  29. include("config.php");
  30. if($_GET['del']!="" and is_numeric($_GET['del'])){
  31. $user = addslashes($_SESSION['user']);
  32. $drop = "DELETE FROM notes WHERE id='".$_GET['del']."' and username='".$user."'";
  33. mysqli_query($connect,$drop);
  34. $_SESSION['download']="0";
  35. $_SESSION['downloaded']="";
  36. echo '<script>
  37. $("#princ").html("Note deleted.");
  38. $("#princ").load("../includes/note.php", function (responseText, textStatus, req) {
  39. if (textStatus == "error") {
  40. $("#princ").html("An error occurred");
  41. }
  42. });
  43. setTimeout(function() {
  44. $("#note").load("../includes/note.php?load=true", function (responseText, textStatus, req) {
  45. if (textStatus == "error") {
  46. $("#princ").html("An error occurred");
  47. }
  48. });
  49. }, 10000);
  50. </script>';
  51. die();
  52. }
  53. if($_POST['nota']!=""){//critta e inserisci nel db
  54. function CasualPassword($lenght=527){
  55. $available_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
  56. $password = "";
  57. for($i = 0; $i<$lenght; $i++){
  58. $password .= substr($available_chars,rand(0,strlen($available_chars)-1),1);
  59. }
  60. return $password;
  61. }
  62. $key = CasualPassword();
  63. $string = nl2br($_POST['nota']);
  64. for ($i = 0; $i < strlen($string); $i++ ) {
  65. $temp = $string[$i] ^ $key[$i % strlen($key)];
  66. $crypt .= str_pad( dechex( ord( $temp ) ), 2, 0, STR_PAD_LEFT);
  67. }
  68. $encryptednote = $key.'__'.$crypt;
  69. $query = "INSERT INTO `notes` (
  70. `username` ,
  71. `nota`
  72. )
  73. VALUES (
  74. '$user', '$encryptednote'
  75. );";
  76. mysqli_query($connect,$query);
  77. $_SESSION['download']="0";
  78. $_SESSION['downloaded']="";
  79. echo '<script>
  80. $("#note").html("Loading...");
  81. $("#note").load("../includes/note.php?load=true", function (responseText, textStatus, req) {
  82. if (textStatus == "error") {
  83. $("#princ").html("An error occurred.");
  84. }
  85. });
  86. function update() {
  87. $.get("../includes/note.php?load=true", function(data) {
  88. $("#note").html(data);
  89. });
  90. }
  91. window.setTimeout(update, 3000);
  92. </script>';
  93. }
  94. else if($_POST['nota']=="" and $_GET['load']==""){echo '<script>
  95. $(document).ready(function() {
  96. $("#go").click(function(){
  97. $("#note").html("Please wait...");
  98. $.ajax({
  99. url:"../includes/note.php",
  100. type: "POST",
  101. data: $("#notes").serialize(),
  102. success: function(msg)
  103. {
  104. $("#note").html(msg);
  105. },
  106. error: function()
  107. {
  108. alert("Error!");
  109. }
  110. });
  111. });
  112. });
  113. $("#note").html("Loading...");
  114. $("#note").load("../includes/note.php?load=true", function (responseText, textStatus, req) {
  115. if (textStatus == "error") {
  116. $("#princ").html("An error occurred.");
  117. }
  118. });
  119. function update() {
  120. $.get("../includes/note.php?load=true", function(data) {
  121. $("#note").html(data);
  122. });
  123. }
  124. </script>
  125. <form method="post" action="../includes/note.php" id="notes">
  126. <textarea name="nota" id="nota" style="width:100%; height:20%;"></textarea>
  127. <input type="button" id="go" value="Save">
  128. </form>';}
  129. if($_GET['load']=="true"){echo '<h3>Your notes</h3>';
  130. //seleziona tutte le note
  131. $user = addslashes($_SESSION['user']);
  132. $download = addslashes($_SESSION['download']);
  133. $query = mysqli_query($connect,"SELECT * FROM notes WHERE username='".$user."' AND id>'".$download."' ORDER BY id DESC LIMIT 0,50");
  134. while($note = mysqli_fetch_assoc($query)){
  135. $separate = explode("__",$encryptednote);
  136. $key =$separate[0];
  137. $crypt = $separate[1];
  138. $cnt = 0;
  139. for ($i = 0; $i < strlen( $crypt ); $i+=2){
  140. $temp = chr( hexdec( substr( $crypt, $i, 2) ) );
  141. $string .= $temp ^ $key[$cnt % strlen($key)];
  142. $cnt++;
  143. }
  144. echo $string.'<br><button class="opzione" id="elimina'.$note['id'].'" onclick="$('#elimina'.$note['id'].'').hide(); $('#confermaz'.$note['id'].'').show();">Delete</button><button class="opzione" id="confermaz'.$note['id'].'" style="display:none;"><a href="javascript:apriLink('../includes/note.php?del='.$note['id'].'')">Confirm</a></button><hr>';
  145. if($note['id']>$_SESSION['download'] or $_SESSION['download']==""){$_SESSION['download']=$note['id'];}
  146. $_SESSION['downloaded'] .= $stringa.'<br><button class="opzione" id="elimina'.$note['id'].'" onclick="$('#elimina'.$note['id'].'').hide(); $('#confermaz'.$note['id'].'').show();">Delete</button><button class="opzione" id="confermaz'.$note['id'].'" style="display:none;"><a href="javascript:apriLink('../includes/note.php?del='.$note['id'].'')">Confirm</a></button><hr>';
  147. $stringa="";
  148. }
  149. echo $_SESSION['downloaded'];
  150. echo '<script>
  151. window.setTimeout(update, 3000); </script>';
  152. die();
  153. }
  154. ?>
  155. <div id="note"></div>
  156.  
  157. $drop = "DELETE FROM notes WHERE id='".$_GET['del']."' and username='".$user."'";
  158.  
  159. $query = "INSERT INTO `notes` (
  160. [...]
  161.  
  162. echo $string
  163.  
  164. "# === WARNING ===
  165.  
  166. # Resulting cipher text has no integrity or authenticity added
  167. # and is not protected against padding oracle attacks."
  168.  
  169. private function setupCipher(){
  170. $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
  171.  
  172. return array(
  173. pack('H*', "bcb04b7e103a0cd8b54763051cef08bc55abe029fdebae5e1d417e2ffb2a00a3"),
  174. $iv_size,
  175. mcrypt_create_iv($iv_size, MCRYPT_RAND)
  176. );
  177. }
  178.  
  179. public function decrypt($string){
  180. list($key, $iv_size) = $this->setupCipher();
  181.  
  182. $string = base64_decode($string);
  183. $iv_dec = substr($string, 0, $iv_size);
  184. $ciphertext_dec = substr($string, $iv_size);
  185. return trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $ciphertext_dec, MCRYPT_MODE_CBC, $iv_dec), "");
  186. }
  187.  
  188. public function encrypt($string) {
  189. list($key, $iv_size, $iv) = $this->setupCipher();
  190.  
  191. $string = $iv . mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $string, MCRYPT_MODE_CBC, $iv);
  192. return trim(base64_encode($string), "");
  193. }
Add Comment
Please, Sign In to add comment