TinyWebDB with PHP and Extension to Encrypt data

In this old forum I put information about creating a TinyWebDB using PHP: https://groups.google.com/forum/#!category-topic/mitappinventortest/apps-tips--tricks/wi8w1x6ljPc

Now I will post the code more clearly and add an extension to encrypt the data.

1.- Create a TinyWebDB database with a PHP file.

We upload this file to our hosting:

tinywebdb.php

<?php
   // Juan Antonio Villalpando
  // http://kio4.com/appinventor/8A_MiniWebDB_TinyWebDB.htm
  
$receive_post = $_SERVER["REQUEST_URI"];
$tag = $_POST['tag'];
$file = 'tinywebdb.htm';

// If file not exist, create.
if (!file_exists($file)) {$create = fopen($file, 'w');}

if(strpos($receive_post,'storeavalue')){

			//////////////////////////////// If exist that tag, DELETE it
			//// to avoid repeating that tag.
			$arc = file($file);
			$auxi = fopen($file,"w");
			foreach($arc as $line)
			{ $fields = explode(":", $line);
			if ($fields[0] != $tag) { fputs($auxi, $line); }
			}
			fclose($auxi);
			/////////////////////////////////////////////////////////////////////
                                                                    
///////////////////////////////////////////////////////////////////////////////////////////////////
// STORE TAG AND VALUE
                                                                  
	$valueToStore = $_POST['value'];
	$line = $tag.":".$valueToStore.":\n";
	  
	$auxi = fopen($file, 'a'); // Add $line to file tinywebdb.htm 
	fwrite($auxi, $line);

} else {
///////////////////////////////////////////////////////////////////////////////////////////////////
// GET VALUE OF TAG.

// Search in file tinywebdb.htm value for that tag.
$auxi = fopen($file, 'r');
$exist = ""; 

while(!feof($auxi)){
    $line = fgets($auxi);
    $fields = explode(":", $line);

    if ($fields[0] == $tag) {
        $value =$fields[1];
        $exist = true;
    }
}

// Sends result.
	if ($exist){ $result = array("VALUE", $tag, $value);}    // Send value
	else { $result = array("VALUE", $tag, "Not_found");}  // Send "Not_found".
	 
	$result_in_JSON = json_encode($result);
	echo $result_in_JSON;
	 }
 
fclose($auxi);
?>
  • We place a TinyWebDB component and in Property ServiceURL we put the internet address of the tinywebdb.php file, example: https://mydomain/tinywebdb.php

  • We can use the TinyWebDB component as we normally do.

  • The data will be saved in the tinywebdb.htm file

3 Likes

2.- Now we are going to use an extension to encrypt the data, in the upload, in the storage and in the download.

KIO4_SecretKey.aix
(This extension works with API 26+ (Android 8+), but I can change it for other versions.)

This extension is based on:
https://developer.android.com/reference/javax/crypto/Cipher

https://developer.android.com/reference/javax/crypto/SecretKeyFactory

https://developer.android.com/reference/android/util/Base64

This extension cipher the KeySecret by adding Salt and with that key encodes the data in Base 64

We set SecretKey + Salt and encrypt the data.
They will be sent in Base 64 encrypted.
The data will be stored encrypted in the database.

In the data file we will obtain this type of information.
tinywebdb.htm

vnjIYkwtSAJMvI5YhWfnpg==:"77AFfhf7h25w5ZVp+St28vKuLj+qgOHo8ovBX5H+2Iw=":
1Fg5AVSB5CFqLJ/c71WqPg==:"Byimv8XYeqSNZ+c4gstAUkNzzW7l4wMNCTN6IEwkN+A=":
UjpIv4gqG34l61B0SZtCeg==:"DthkwsyLP5CEjVqYhRLchg==":
gsIQ+TWc0PA3HtV6eP4gng==:"Ip2gx\/lfzShn1jsxqdio9g==":

The tags and values ​​are encrypted.

Regards,
Juan A. Villalpando
http://kio4.com/appinventor/8A_MiniWebDB_TinyWebDB.htm (Tutorial in Spanish)

4 Likes

Nice work as always Juan :+1:

Now there are two!

https://ai2.metricrat.co.uk/guides/your-own-secure-php-tinywebdb

2 Likes

Three?

2 Likes

Hi,
I'm trying to create a TinywebDB online on my php web store.
I tried with the php script from post number 1 (without encryption), but reading values I got error "Value ... of type java.lang.String cannot be converted to JSON Array.
Maybe the PHP script needs to be updated ?

Thank you