Using http json with encryption

tonygil

New Member
Licensed User
Longtime User
Hi Guys

I have project which is using a php script to talk to a database via json.

My basic4android module uses http and json parser and that works perfectly without encryption.

I have modified the phpto use mcrypt and that works as it returns a encrypted json data.

Had a look at the encryption basic encryption and i am stuck I think with the key that is to be same on the php and basic module

In my php i have $key = hash("md5", "All Systems Go");

What needs to change in the basic code sample

Also i have enclosed the PHP as well

Basic
------------------------------------------------------
Dim key(0) As Byte
Dim data(0) As Byte
Dim iv(0) As Byte
Dim bytecon As ByteConverter
iv = Array As Byte(11, 22, 33, 44, 55, 66, 77, 88,66,78,64,66,55,34,56,15) ' 16 bytes for AES

' use DES for variable length data using padding
Dim kg As KeyGenerator
Dim c As Cipher
c.Initialize("AES/CBC/PKCS5Padding") ' replace "DES/" with "AES/" for Rijndael or "DESEDE/" for triple DES

' CBC needs an initialisation vector
c.InitialisationVector = iv
kg.Initialize("AES") ' replace "DES" with "AES" for Rijndael or "DESEDE" for triple DES
kg.GenerateKey
key = kg.KeyToBytes
Msgbox(Bconv.HexFromBytes(key), "Key " & key.Length & " bytes")

clear = "A clear string of data to encrypt"
data = Bconv.StringToBytes(clear, "UTF8")
data = c.Encrypt(data, kg.Key, True)
Msgbox(Bconv.HexFromBytes(data), "Encrypted is " & data.Length & " bytes")
data = c.Decrypt(data, kg.Key, True)
Msgbox(Bconv.StringFromBytes(data, "UTF8"), "Decrypted")

------------------------------------------
PHP

// Connect to the database server
$link = mysql_connect(DBHOST, DBUSER, DBPW, false, 65536) or die("Could not connect");
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
print $iv_size;
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
$key = hash("md5", "All Systems Go");
$text = "CALL spGetContactList('le')";

$crypttext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $text, MCRYPT_MODE_CBC, $iv);

$decrypttext = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $crypttext,
MCRYPT_MODE_CBC, $iv);

$qry1 = $decrypttext;
$query = rtrim($qry1, "\0");
echo $query;
//$query = (file_get_contents("php://input"));

//select the json database
mysql_select_db(DBNAME) or die("Could not select database");


// Create an array to hold our results
$arr = array();
//Execute the query
$rs = mysql_query($query);

// Add the rows to the array
while ($obj = mysql_fetch_object($rs)) {
$arr[] = $obj;
}

// return the json result. The string users is just a name for the container object. Can be set anything.
print json_encode($arr);
print mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, json_encode($arr),
MCRYPT_MODE_CBC, $iv);
//echo '{"users":'.json_encode($arr).'}';



Thanks
Tonygil:sign0085:
 
Top