My encryption algorithm
I'm using this algorithm in order to encrypt notes users save on my site:
function CasualPassword($lenght=527){
$available_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
$password = "";
for($i = 0; $i<$lenght; $i++){
$password .= substr($available_chars,rand(0,strlen($available_chars)-1),1);
}
return $password;
}
$key = CasualPassword();
$string = nl2br($_POST['nota']);
for ($i = 0; $i < strlen($string); $i++ ) {
$temp = $string[$i] ^ $key[$i % strlen($key)];
$crypt .= str_pad( dechex( ord( $temp ) ), 2, 0, STR_PAD_LEFT);
}
$encryptednote = $key.'__'.$crypt;
So if I encrypted "Code review is awesome" I'd get something like this:
CB9A25T2g2kgSmeUAtFu6h2vOeVPMLOONMOFGh8nYhJrKoCKXH6TpwF4QaJcmYrzk66rd3U3ZekjJmuhuq1Zc6TtWdL923ybytIRldKB9ulHWWoGCfgfa1OxTavKTklHUuZ7Oj8MTjTq69x9dUd5KsrdIdnbDPiY09UOhRa22uixtVvMqPH18zTGS7hWMGuLEEZmW1eNkCBTYwgECo6AdOGvLoIXu4jz90cUz7ia0juYWRdZ6YZYmgKyKTM6MvvKuNfkKDAKnuiABBZ8ZzVCVg2fzDbBzMMjarKkvKuZ6SZ8Uz5uoQwAwofmr8ngJ1GjhQKTb6sWOIUi9PSmfFmStI5bFi8PYEz7V0Qw59JIHdrFsAsOFUhzdbQj0OyYSnVnnlZikjrrCYxvYfAe1hTT0j39xrXFUst9UJg5sNmvmZgA5hevFyEEX8DLoaR0JA8dJeX2r0mTZJUTqzFvnJ1BH4MwTdvcet7nNTe6THOsRjC8ZHtDRkEZdRLwv7PPpL44fYyzfDiwnkuCseF__002d5d24124731440e571c473a1e45343611351a5b0d
Or this:
6sa9JEQHpMmvwapMLUPlVKbVSSXgbbPWJpVSulZZyCKkc1xoKCPfMr1fAOBbpQyz7JHqvhGjjQKCH9qRB6xOnotn3vEiC3X9CfpxCvXmChpJfVbhROv53eivPmTRfrQhM6UPrs3uQi4w4VTLjEGcioNyRhGxPWUbSpr0XktoInaC0tOixUBW0OKRwQEBnZd6EL6cvpG5Tg7SQK2Df3afHlw4216fPYbUjWxVC4PVaMorXfv3XLXewKYoBTJQrllR8YnAS4WtGAadgLW4XuYJ5wy6G9MxjyFhMthfm4Os5pMa2iUOSsYMfNIlLUZvIfSVO112uFkovwEmUZ2nHPznd9OoSoZbJsxYjoQTt19fnCH9b9ljObMrZbWsFwKPf8oEl5XVvV1YY9X180aLam41EPjKCtahrElT99puUgIsegjbf0n7mgwrwV2yFRVwW7GV6vFPRO9wuiNPh1LKX92JUs8ZASMWPSrKExaWBYIv7VBeMNojLfsfOp6O9HlNaTXeGx2IM04JV6nhISH__751c055c6a37343e19281a561e12502c3b3023033b2e
My decryption algorithm
$separate = explode("__",$encryptednote);
$key =$separate[0];
$crypt = $separate[1];
$cnt = 0;
for ($i = 0; $i < strlen( $crypt ); $i+=2){
$temp = chr( hexdec( substr( $crypt, $i, 2) ) );
$string .= $temp ^ $key[$cnt % strlen($key)];
$cnt++;
}
Complete code
<?php
include("../co.php");
include("config.php");
if($_GET['del']!="" and is_numeric($_GET['del'])){
$user = addslashes($_SESSION['user']);
$drop = "DELETE FROM notes WHERE id='".$_GET['del']."' and username='".$user."'";
mysqli_query($connect,$drop);
$_SESSION['download']="0";
$_SESSION['downloaded']="";
echo '<script>
$("#princ").html("Note deleted.");
$("#princ").load("../includes/note.php", function (responseText, textStatus, req) {
if (textStatus == "error") {
$("#princ").html("An error occurred");
}
});
setTimeout(function() {
$("#note").load("../includes/note.php?load=true", function (responseText, textStatus, req) {
if (textStatus == "error") {
$("#princ").html("An error occurred");
}
});
}, 10000);
</script>';
die();
}
if($_POST['nota']!=""){//critta e inserisci nel db
function CasualPassword($lenght=527){
$available_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
$password = "";
for($i = 0; $i<$lenght; $i++){
$password .= substr($available_chars,rand(0,strlen($available_chars)-1),1);
}
return $password;
}
$key = CasualPassword();
$string = nl2br($_POST['nota']);
for ($i = 0; $i < strlen($string); $i++ ) {
$temp = $string[$i] ^ $key[$i % strlen($key)];
$crypt .= str_pad( dechex( ord( $temp ) ), 2, 0, STR_PAD_LEFT);
}
$encryptednote = $key.'__'.$crypt;
$query = "INSERT INTO `notes` (
`username` ,
`nota`
)
VALUES (
'$user', '$encryptednote'
);";
mysqli_query($connect,$query);
$_SESSION['download']="0";
$_SESSION['downloaded']="";
echo '<script>
$("#note").html("Loading...");
$("#note").load("../includes/note.php?load=true", function (responseText, textStatus, req) {
if (textStatus == "error") {
$("#princ").html("An error occurred.");
}
});
function update() {
$.get("../includes/note.php?load=true", function(data) {
$("#note").html(data);
});
}
window.setTimeout(update, 3000);
</script>';
}
else if($_POST['nota']=="" and $_GET['load']==""){echo '<script>
$(document).ready(function() {
$("#go").click(function(){
$("#note").html("Please wait...");
$.ajax({
url:"../includes/note.php",
type: "POST",
data: $("#notes").serialize(),
success: function(msg)
{
$("#note").html(msg);
},
error: function()
{
alert("Error!");
}
});
});
});
$("#note").html("Loading...");
$("#note").load("../includes/note.php?load=true", function (responseText, textStatus, req) {
if (textStatus == "error") {
$("#princ").html("An error occurred.");
}
});
function update() {
$.get("../includes/note.php?load=true", function(data) {
$("#note").html(data);
});
}
</script>
<form method="post" action="../includes/note.php" id="notes">
<textarea name="nota" id="nota" style="width:100%; height:20%;"></textarea>
<input type="button" id="go" value="Save">
</form>';}
if($_GET['load']=="true"){echo '<h3>Your notes</h3>';
//seleziona tutte le note
$user = addslashes($_SESSION['user']);
$download = addslashes($_SESSION['download']);
$query = mysqli_query($connect,"SELECT * FROM notes WHERE username='".$user."' AND id>'".$download."' ORDER BY id DESC LIMIT 0,50");
while($note = mysqli_fetch_assoc($query)){
$separate = explode("__",$encryptednote);
$key =$separate[0];
$crypt = $separate[1];
$cnt = 0;
for ($i = 0; $i < strlen( $crypt ); $i+=2){
$temp = chr( hexdec( substr( $crypt, $i, 2) ) );
$string .= $temp ^ $key[$cnt % strlen($key)];
$cnt++;
}
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>';
if($note['id']>$_SESSION['download'] or $_SESSION['download']==""){$_SESSION['download']=$note['id'];}
$_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>';
$stringa="";
}
echo $_SESSION['downloaded'];
echo '<script>
window.setTimeout(update, 3000); </script>';
die();
}
?>
<div id="note"></div>
My question
Is this method to encrypt/decrypt a string safe?