Skip to content

Commit

Permalink
Adding Encryption
Browse files Browse the repository at this point in the history
  • Loading branch information
pablojotaguardiola committed Jul 12, 2016
1 parent aa0f43a commit 8a26d5d
Show file tree
Hide file tree
Showing 5 changed files with 352 additions and 12 deletions.
198 changes: 191 additions & 7 deletions PHP/parseql/application/controllers/ParseQLController.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php
define("TOKEN", "`p+23049diqowedqhd++ç!ª!·", TRUE);
define("PRIVATEKEY", "dnuehjbdq834+ç´`", TRUE);

class ParseQLController extends CI_Controller {

Expand Down Expand Up @@ -38,43 +40,167 @@ public function configDatabase() {
public function saveObject() {
$objectData = json_decode(file_get_contents('php://input'), true);

echo $this->parseQLModel->saveObject($objectData);
//DESENCRYPTION
$encryptedData = $objectData["encryptedData"];
$desencryptedJson = json_decode($this->encryptedByteArrayToString($encryptedData), true);

if ($desencryptedJson["token"] == TOKEN) {
unset($desencryptedJson["token"]);
//MODEL
$encryptedResponse = $this->parseQLModel->saveObject($desencryptedJson);
echo json_encode(array(
"encryptedData" => $this->stringToEncryptedByteArray($encryptedResponse)
));
}
else {
echo json_encode(array(
"encryptedData" => array("Resp" => "INVALID TOKEN")
));
}
}

public function getObject() {
$objectData = json_decode(file_get_contents('php://input'), true);

echo $this->parseQLModel->getObject($objectData);
$objectData = json_decode(file_get_contents('php://input'), true);

//DESENCRYPTION
$encryptedData = $objectData["encryptedData"];
$desencryptedJson = json_decode($this->encryptedByteArrayToString($encryptedData), true);

if ($desencryptedJson["token"] == TOKEN) {
unset($desencryptedJson["token"]);
//MODEL
$encryptedResponse = $this->parseQLModel->getObject($objectData);
echo json_encode(array(
"encryptedData" => $this->stringToEncryptedByteArray($encryptedResponse)
));
}
else {
echo json_encode(array(
"encryptedData" => array("Resp" => "INVALID TOKEN")
));
}
}

public function getCreateObject() {
$objectData = json_decode(file_get_contents('php://input'), true);

echo $this->parseQLModel->getCreateObject($objectData);
$objectData = json_decode(file_get_contents('php://input'), true);

//DESENCRYPTION
$encryptedData = $objectData["encryptedData"];
$desencryptedJson = json_decode($this->encryptedByteArrayToString($encryptedData), true);

if ($desencryptedJson["token"] == TOKEN) {
unset($desencryptedJson["token"]);
//MODEL
$encryptedResponse = $this->parseQLModel->getCreateObject($objectData);
echo json_encode(array(
"encryptedData" => $this->stringToEncryptedByteArray($encryptedResponse)
));
}
else {
echo json_encode(array(
"encryptedData" => array("Resp" => "INVALID TOKEN")
));
}
}

public function updateCreateObject() {
$objectData = json_decode(file_get_contents('php://input'), true);

echo $this->parseQLModel->updateCreateObject($objectData);
$objectData = json_decode(file_get_contents('php://input'), true);

//DESENCRYPTION
$encryptedData = $objectData["encryptedData"];
$desencryptedJson = json_decode($this->encryptedByteArrayToString($encryptedData), true);

if ($desencryptedJson["token"] == TOKEN) {
unset($desencryptedJson["token"]);
//MODEL
$encryptedResponse = $this->parseQLModel->updateCreateObject($objectData);
echo json_encode(array(
"encryptedData" => $this->stringToEncryptedByteArray($encryptedResponse)
));
}
else {
echo json_encode(array(
"encryptedData" => array("Resp" => "INVALID TOKEN")
));
}
}

public function updateObject() {
$objectData = json_decode(file_get_contents('php://input'), true);

echo $this->parseQLModel->updateObject($objectData);
$objectData = json_decode(file_get_contents('php://input'), true);

//DESENCRYPTION
$encryptedData = $objectData["encryptedData"];
$desencryptedJson = json_decode($this->encryptedByteArrayToString($encryptedData), true);

if ($desencryptedJson["token"] == TOKEN) {
unset($desencryptedJson["token"]);
//MODEL
$encryptedResponse = $this->parseQLModel->updateObject($objectData);
echo json_encode(array(
"encryptedData" => $this->stringToEncryptedByteArray($encryptedResponse)
));
}
else {
echo json_encode(array(
"encryptedData" => array("Resp" => "INVALID TOKEN")
));
}
}

public function deleteObject() {
$objectData = json_decode(file_get_contents('php://input'), true);

echo $this->parseQLModel->deleteObject($objectData);
$objectData = json_decode(file_get_contents('php://input'), true);

//DESENCRYPTION
$encryptedData = $objectData["encryptedData"];
$desencryptedJson = json_decode($this->encryptedByteArrayToString($encryptedData), true);

if ($desencryptedJson["token"] == TOKEN) {
unset($desencryptedJson["token"]);
//MODEL
$encryptedResponse = $this->parseQLModel->deleteObject($objectData);
echo json_encode(array(
"encryptedData" => $this->stringToEncryptedByteArray($encryptedResponse)
));
}
else {
echo json_encode(array(
"encryptedData" => array("Resp" => "INVALID TOKEN")
));
}
}

public function count() {
$objectData = json_decode(file_get_contents('php://input'), true);

echo $this->parseQLModel->count($objectData);
$objectData = json_decode(file_get_contents('php://input'), true);

//DESENCRYPTION
$encryptedData = $objectData["encryptedData"];
$desencryptedJson = json_decode($this->encryptedByteArrayToString($encryptedData), true);

if ($desencryptedJson["token"] == TOKEN) {
unset($desencryptedJson["token"]);
//MODEL
$encryptedResponse = $this->parseQLModel->count($objectData);
echo json_encode(array(
"encryptedData" => $this->stringToEncryptedByteArray($encryptedResponse)
));
}
else {
echo json_encode(array(
"encryptedData" => array("Resp" => "INVALID TOKEN")
));
}
}

public function prueba() {
Expand All @@ -84,5 +210,63 @@ public function prueba() {

echo 'ok';
}

public function saveObjectTest() {
$objectData = json_decode(file_get_contents('php://input'), true);

//DESENCRYPTION
$encryptedData = $objectData["encryptedData"];
$desencryptedJson = json_decode($this->encryptedByteArrayToString($encryptedData), true);

//ENCRYPTION
$tableName = $desencryptedJson["tableName"];
echo json_encode(array(
"encryptedData" => $this->stringToEncryptedByteArray($tableName)
));
}

public function encryptedByteArrayToString($byteArray) { //DESENCRYPT
$keyArray = unpack("C*", PRIVATEKEY);

$resultString = "";

$i = 1;
foreach ($byteArray as $char) {

$xor = $char ^ $keyArray[$i];

$resultString .= chr($xor);

$i++;
if ($i >= count($keyArray)+1) {
$i = 1;
}
}

return $resultString;
}

public function stringToEncryptedByteArray($string) { //ENCRYPT
$stringArray = unpack("C*", $string);
$keyArray = unpack("C*", PRIVATEKEY);

$resultArray = array();


$i = 1;
foreach ($stringArray as $char) {

$xor = $char ^ $keyArray[$i];

$resultArray[] = $xor;

$i++;
if ($i >= count($keyArray)) {
$i = 1;
}
}

return implode($resultArray, ',');
}
}
?>
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<Bucket
type = "1"
version = "2.0">
</Bucket>
5 changes: 5 additions & 0 deletions ParseQL/ParseQL/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleExecutable</key>
Expand Down
Loading

0 comments on commit 8a26d5d

Please sign in to comment.