Examples of encryption for database access
William Prothero
waprothero at gmail.com
Mon Jun 25 12:41:18 EDT 2018
Corrections to the posted code:
I changed the code to encrypt the returned text. I also note that using openSSL in php returns base64 data.
Bill
--------temp, testing iv for encryption
--To test this on your own server, upload the php script where you put cgi's
-- and modify the myURL setting.
//Be sure to change the encryption key and tiv value
on testEncryption
put "http://earthexplorer.earthlearningsolutions.org/scgi-bin/wpEncryptionTest.php" into myURL
put "AES-256-CTR" into tCipher
put "AFBDDFCFBDBBDDCCFFACGHDFFFFEEDCC" into tEncryptionKey //must be 43 chars
put "ABCDEEABCDEEAA%A" into tIV //must be 16 chars
put "The php should return this text." into tPostA["theQuery"]
put "query" into tPostA["type"]
put ArrayToJSON(tPostA,"string",pPretty) into tJson
encrypt tJson using tCipher with key tEncryptionKey and iV tIV
put base64encode(it) into tMyEncryptedData
post tMyEncryptedData to url myURL
put it into tRet
put tRet into fld "status"
—Note that openSSL in php returns base64 encoded data.
put base64decode(tRet) into tRetVal
decrypt tRetVal using tCipher with key tEncryptionKey and iV tIV
put it into theResult
put theResult after fld "status"
end testEncryption
----------php script, on server ---------------------------
--Note: you can run the above script on my server,
--to test the LC script.
<?php
//file: wpEncryptionTest.php
//external function
function debug($msg) {
$debug = false;
if ($debug) {
error_log("[DB LIB] $msg");
echo "$msg.\n";
}
}
//php code
$encryption_key = "AFBDDFCFBDBBDDCCFFACGHDFFFFEEDCC";
$cipher = "AES-256-CTR"; // do not change cipher unless you know what you're doing
$post = file_get_contents('php://input');
$iv = 'ABCDEEABCDEEAA%A';
$ivlen = 16;
/* set for debugging. To encrypt, set to TRUE */
$post = openssl_decrypt($post, $cipher, $encryption_key, $options=0, $iv);
$req = json_decode($post,true);
if (!$req) {
debug("error on decrypt");
debug(openssl_error_string());
}
$theOut = $req["theQuery"]; //This is just the text of the query
//$req is the array value of the tPostA array sent with the post comand.
//Access the elements of tPostA using $req[“name of element”]
//example: $req[“theQuery”] is tPoasA[“theQuery”]
$retVal = "Decrypted query: $theOut.\n";
$doEncryptOutput = TRUE;
if ($doEncryptOutput) {
$retVal = openssl_encrypt($retVal, $cipher, $encryption_key,0,$iv);
//openSSL in php returns base64 encoded data.`
}
echo $retVal;
?>
William A. Prothero
http://earthlearningsolutions.org
More information about the use-livecode
mailing list