im trying to establish secure RSA connection between PHP server and Unity 3D game (in Web Player). At the end of process $rsa->decrypt() return "false" :-(
Server generate RSA keys and send public key to Unity:
$rsa = new Crypt_RSA();
$rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);
$rsa->setPrivateKeyFormat(CRYPT_RSA_PRIVATE_FORMAT_PKCS1);
$rsa->setPublicKeyFormat(CRYPT_RSA_PUBLIC_FORMAT_PKCS1);
$keys = $rsa->createKey(512);
extract($keys);
$rsa->loadKey($publickey);
$_SESSION["privatekey"] = $privatekey;
$this->payload->Modulus = base64_encode($rsa->modulus);
$this->payload->Exponent = base64_encode($rsa->publicExponent);
This generate JSON:
{"Modulus":"MTE5MjcyOTYyNjQzMTIzODQ1MTI4MjE2ODA3OTY2MDE5MDQwODQ1NTc0MDIzMDI0NDQ5MjAzMDY4NDgxNTkyNzk5MTc0MzYxMzI4MDA3Njk0MjI4NjAyMzAwODA4MDI5MzkwOTk2MjUyMTg5OTkwNDgwNzg3MDcwMjk4MjkxMjcxNjQ1NzMzNDg0MTcxNTc0MDM3ODM0NjE3ODE=","Exponent":"NjU1Mzc="}
Unity ENCODER:
var N = JSON.Parse (generatedJSON); //im using SimpleJSON library
var publicKey = new RSAParameters ();
publicKey.Modulus = Convert.FromBase64String(N ["Modulus"].Value);
publicKey.Exponent = Convert.FromBase64String(N ["Exponent"].Value);
var csp = new RSACryptoServiceProvider(512);
csp.ImportParameters(publicKey);
var plainTextData = "Hello Wordl"; //here come AES key generator (not implemented yet)
var bytesPlainTextData = System.Text.Encoding.Unicode.GetBytes(plainTextData);
var bytesCypherText = csp.Encrypt(bytesPlainTextData, false);
var cypherText = Convert.ToBase64String(bytesCypherText);
Unity response:
Nqsno9tTXWr4UfwoXQcHwzwnusvdKWpVGoakSsVECh3lH/3bNaPKY9LzZ9iZIs8RI9e5EI+GvegnxrW5xoqnyrDHbF8AuWh9Hndnn0OS5SV/kiYeBT6Wn9pxwjq5MoixM3geushHpvGTDQV0NOLcsXTdv8tG0CvFZip31GpMp9C/OalxolpaUvk65YBJ0dJcyNiuD08PQJAupJXKnVgfLZ0i1GrjQ7guHO6OmEUKDyQcZ5Sf/6yJry3Mhv2R4ioR/jU+mL4tLKuix5+/XKmBjg==
And server DECODER:
$rsa = new Crypt_RSA();
$rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);
$rsa->setPrivateKeyFormat(CRYPT_RSA_PRIVATE_FORMAT_PKCS1);
$rsa->setPublicKeyFormat(CRYPT_RSA_PUBLIC_FORMAT_PKCS1);
$rsa->loadKey($_SESSION["privatekey"]);
$message = $_POST["unityResponse"];
$ciphertext = base64_decode($message);
$this->payload->message = $rsa->decrypt($ciphertext);
Return "Decryption error in..." :(
I think general problem is in C#... Can you help me?
SOLVED!
By another way - XML key format
Server code:
$rsa = new Crypt_RSA();
$rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);
$rsa->setPrivateKeyFormat(CRYPT_RSA_PRIVATE_FORMAT_XML);
$rsa->setPublicKeyFormat(CRYPT_RSA_PUBLIC_FORMAT_XML);
$keys = $rsa->createKey(1024);
extract($keys);
$rsa->loadKey($publickey);
$_SESSION["privatekey"] = $privatekey;
$this->payload->publickey = $publickey;
Generate JSON:
{"publickey":"<RSAKeyValue>\r\n <Modulus>sCL/O7uUl4T32nUxZhKj1svDG88k4jkGsh/7IiFX/kTbj3mXmtYPLn5xTyLxEt9FxA/aoVxBmh1k7wA7lvcu5z4Avw6+AA/j793iDEhGdfvmXmHxm05xkjYO+LZ449YGDst9DUDO8SDA948Rld+eA187d/nhVPPAmun7RbLXBrs=</Modulus>\r\n <Exponent>AQAB</Exponent>\r\n</RSAKeyValue>"}
Unity Web Player code:
var N = JSON.Parse (generatedJSON);
var csp = new RSACryptoServiceProvider(1024);
csp.FromXmlString (N ["publickey"]);
var plainTextData = "Hello from Web Player";
var bytesPlainTextData = System.Text.Encoding.Unicode.GetBytes(plainTextData);
var bytesCypherText = csp.Encrypt(bytesPlainTextData, false);
var cypherText = Convert.ToBase64String(bytesCypherText);
And finally Server Decryption:
$rsa = new Crypt_RSA();
$rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);
$rsa->setPrivateKeyFormat(CRYPT_RSA_PRIVATE_FORMAT_XML);
$rsa->setPublicKeyFormat(CRYPT_RSA_PUBLIC_FORMAT_XML);
$rsa->loadKey($_SESSION["privatekey"]);
$cypherText= $_POST["cypherText"];
$bytesCipherText = base64_decode($message);
$this->payload->encrypted = $rsa->decrypt($bytesCipherText);
And you get JSON:
{"message":"Hello from Web Player"}
Thanks for perfect phpsec library! Its epic!
I share my example Unity project here. Hoping that it is useful to some people ;)
GitHub: unity-php-secure-communication
<?php
<?php
/**
* test with curl:
*
* // Generate keypair : return XML public key
* curl -c cookies.txt -d "keygen=1" http://exemple.com//encrypt.php
*
* // Test encrypt/decrypt : return encrypted and decrypted `my text to encode`
* curl -b cookies.txt -d "test=my text to encode" http://exemple.com//encrypt.php
*
* // Test encrypt : return encrypted
* curl -b cookie.txt -d "encrypt=my text to encode" http://exemple.com//encrypt.php > encrypted.txt; cat encrypted.txt
*
* // Test decrypt : return decrypted `my text to encode`
* curl -b cookie.txt -d "decrypt=`cat encrypted.txt`" http://exemple.com//encrypt.php
*/
include 'vendor/autoload.php';
use phpseclib\Crypt\RSA;
function generateKeyPair(){
if (!isset($_SESSION['publickey'])){
$rsa = new RSA();
$rsa->setPrivateKeyFormat(RSA::PRIVATE_FORMAT_XML);
$rsa->setPublicKeyFormat(RSA::PUBLIC_FORMAT_XML);
$keys = $rsa->createKey();
$_SESSION['privatekey'] = $keys['privatekey'];
$_SESSION['publickey'] = $keys['publickey'];
}
return $_SESSION['publickey'];
}
function encrypt($cleartext){
$rsa = new RSA();
$rsa->setEncryptionMode(RSA::ENCRYPTION_PKCS1);
$rsa->setPublicKeyFormat(RSA::PUBLIC_FORMAT_XML);
$rsa->loadKey($_SESSION['publickey']);
$bytesCipherText = $rsa->encrypt($cleartext);
return rawurlencode(base64_encode($bytesCipherText));
}
function decrypt($encrypted){
$rsa = new RSA();
$rsa->setEncryptionMode(RSA::ENCRYPTION_PKCS1);
$rsa->setPrivateKeyFormat(RSA::PRIVATE_FORMAT_XML);
$rsa->loadKey($_SESSION['privatekey']);
$bytesCipherText = base64_decode(rawurldecode($encrypted));
return $rsa->decrypt($bytesCipherText);
}
if (isset($_POST['session_id'])) {
session_id($_POST['session_id']);
}
session_start();
if (isset($_POST['keygen'])) {
echo generateKeyPair();
exit();
}
if (isset($_POST['encrypt'])) {
echo encrypt($_POST['encrypt']);
exit();
}
if (isset($_POST['decrypt'])) {
echo decrypt($_POST['decrypt']);
exit();
}
if (isset($_POST['test'])) {
generateKeyPair();
$ciphertext = encrypt($_POST['test']);
echo "encrypted: $ciphertext\n\n";
$clearText = decrypt($ciphertext);
echo "decrypted: $clearText\n";
exit();
}
using System;
using System.Collections;
using System.Security.Cryptography;
using System.Text;
using UnityEngine;
using UnityEngine.Analytics;
using UnityEngine.Networking;
using UnityEngine.UI;
public class EncryptController : MonoBehaviour
{
public string ScriptUrl = "http://example.com/encrypt.php";
public InputField ClearTextInputField;
public InputField EncryptedTextInputField;
public InputField ClearResponseInputField;
public Text SessionIdText;
public Text PublicKeyText;
public void OnKeygenButtonClick()
{
StartCoroutine(GenerateRsaKeyPair());
}
public void OnEncryptButtonClick()
{
if (PublicKeyText.text.Length < 100)
{
Debug.LogError("You must generate RSA key pair before");
return;
}
var csp = new RSACryptoServiceProvider(1024);
csp.FromXmlString(PublicKeyText.text);
var plainTextData = ClearTextInputField.text;
var bytesPlainTextData = Encoding.Unicode.GetBytes(plainTextData);
var bytesCypherText = csp.Encrypt(bytesPlainTextData, false);
var cypherText = Convert.ToBase64String(bytesCypherText);
EncryptedTextInputField.text = cypherText;
}
public void OnDecryptButtonClick()
{
if (PublicKeyText.text.Length < 100)
{
Debug.LogError("You must generate RSA key pair before");
return;
}
StartCoroutine(SendEncryptedText(EncryptedTextInputField.text));
}
IEnumerator GenerateRsaKeyPair()
{
var form = new WWWForm();
form.AddField("keygen", 1);
form.AddField("session_id", AnalyticsSessionInfo.sessionId.ToString());
SessionIdText.text = "sessionId=" + AnalyticsSessionInfo.sessionId.ToString();
using (var www = UnityWebRequest.Post(ScriptUrl, form))
{
yield return www.SendWebRequest();
if (www.isNetworkError)
{
Debug.LogError(www.error);
}
else
{
LogHeaders(www);
Debug.Log(www.downloadHandler.text);
PublicKeyText.text = www.downloadHandler.text;
}
}
}
IEnumerator SendEncryptedText(string text)
{
var form = new WWWForm();
form.AddField("decrypt", text);
form.AddField("session_id", AnalyticsSessionInfo.sessionId.ToString());
SessionIdText.text = "sessionId=" + AnalyticsSessionInfo.sessionId.ToString();
using (var www = UnityWebRequest.Post(ScriptUrl, form))
{
//www.SetRequestHeader("cookie", SessionIdText.text);
yield return www.SendWebRequest();
if (www.isNetworkError)
{
Debug.LogError(www.error);
}
else
{
LogHeaders(www);
// Print Body
Debug.Log(www.downloadHandler.text);
ClearResponseInputField.text = www.downloadHandler.text;
}
}
}
private void LogHeaders(UnityWebRequest www)
{
var sb = new StringBuilder();
foreach (var dict in www.GetResponseHeaders())
{
sb.Append(dict.Key).Append(": \t[").Append(dict.Value).Append("]\n");
if (dict.Key == "Set-Cookie")
{
SessionIdText.text = dict.Value + " (from response headers)";
}
}
Debug.Log(sb.ToString());
}
}
Related
(Please note that the function I reference from the provided answer didn't work so I want to modify it to make it work or please provide a suitable solution to AES encryption in VueJS and decryption in PHP (or Laravel))
I have a password will be sent to Laravel API so I managed to encrypt and base64 it then send it to Laravel API. Here's the original encryption
And here's the encryption and encoding
var encryptedPassword = CryptoJS.AES.encrypt("123456", "Secret Passphrase");
const replacerFunc = () => {
const visited = new WeakSet();
return (key, value) => {
if (typeof value === "object" && value !== null) {
if (visited.has(value)) {
return;
}
visited.add(value);
}
return value;
};
};
let jsonString = JSON.stringify(encryptedPassword, replacerFunc());
let bs46encoded = btoa(unescape(encodeURIComponent(JSON.stringify(jsonString, replacerFunc()))))
I get this result
IntcIiRzdXBlclwiOntcIiRzdXBlclwiOnt9fSxcImNpcGhlcnRleHRcIjp7XCJ3b3Jkc1wiOlsxODQ4NTYxMTAzLDE1MjkyNDgyMTUsLTEwODg3MTk1MDEsLTEyNjAyOTYzNjJdLFwic2lnQnl0ZXNcIjoxNn0sXCJrZXlcIjp7XCIkc3VwZXJcIjp7fSxcIndvcmRzXCI6Wy0yODA5NjkxMjcsNzAzNjYxOTM0LDE1Mjg5OTYxNDEsLTEwOTU5OTQ2MDYsLTY4OTMyOTM1NiwtNDQ1MDc3MjE3LC0yMDM4MDY1MTUxLC0xNzI0MjIzMzE4LC0xNTk1Nzk5MDI5LDgxOTY1NjMwMSwtMTQzNTIxMDg2NCwxODkwMDcyNTc5XSxcInNpZ0J5dGVzXCI6MzJ9LFwiaXZcIjp7XCJ3b3Jkc1wiOlstMTU5NTc5OTAyOSw4MTk2NTYzMDEsLTE0MzUyMTA4NjQsMTg5MDA3MjU3OV0sXCJzaWdCeXRlc1wiOjE2fSxcImFsZ29yaXRobVwiOntcImtleVNpemVcIjo4LFwiJHN1cGVyXCI6e1wiY2ZnXCI6e1wibW9kZVwiOntcIiRzdXBlclwiOnt9LFwiRW5jcnlwdG9yXCI6e30sXCJEZWNyeXB0b3JcIjp7fX0sXCJwYWRkaW5nXCI6e30sXCIkc3VwZXJcIjp7fX0sXCJibG9ja1NpemVcIjo0LFwiJHN1cGVyXCI6e1wia2V5U2l6ZVwiOjQsXCJpdlNpemVcIjo0LFwiX0VOQ19YRk9STV9NT0RFXCI6MSxcIl9ERUNfWEZPUk1fTU9ERVwiOjIsXCIkc3VwZXJcIjp7XCJfbWluQnVmZmVyU2l6ZVwiOjB9fX19LFwiYmxvY2tTaXplXCI6NCxcImZvcm1hdHRlclwiOnt9LFwic2FsdFwiOntcIndvcmRzXCI6WzE5NDk1NjcxNjIsMjExODc0MjAyOF0sXCJzaWdCeXRlc1wiOjh9fSI=
In laravel
$objString = base64_decode($ecodedString);
And I get
"{\"$super\":{\"$super\":{}},\"ciphertext\":{\"words\":[-1208966431,415513002,-380428285,-1848403568],\"sigBytes\":16},\
"key\":{\"$super\":{},\"words\":[1997420112,-1528658502,-601329304,170123410,85401029,1271916135,-1899682466,856024313,-1926693888,505524620,1922976396,79922502],\"sigBytes\":32},
\"iv\":{\"words\":[-1926693888,505524620,1922976396,79922502],\"sigBytes\":16},\"algorithm\":{\"keySize\":8,\"$super\":{\"cfg\":{\"mode\":{\"$super\":{},\"Encryptor\":{},\"Decryptor\":{}},\"padding\":{},\"$super\":{}},\"blockSize\":4,\"$super\":{\"keySize\":4,\"ivSize\":4,\"_ENC_XFORM_MODE\":1,\"_DEC_XFORM_MODE\":2,\"$super\":{\"_minBufferSize\":0}}}},\"blockSize\":4,\"formatter\":{},
\"salt\":{\"words\":[3335652791,3193595722],\"sigBytes\":8}}"
Then
$this->cryptoJsAesDecrypt("Secret Passphrase", $objString);
public function cryptoJsAesDecrypt($passphrase, $jsonString){
$jsondata = json_decode($jsonString, true);
Log::alert("jsondata");
Log::alert($jsondata);
$salt = hex2bin($jsondata["salt"]);
$ct = base64_decode($jsondata["ct"]);
$iv = hex2bin($jsondata["iv"]);
$concatedPassphrase = $passphrase.$salt;
$md5 = array();
$md5[0] = md5($concatedPassphrase, true);
$result = $md5[0];
for ($i = 1; $i < 3; $i++) {
$md5[$i] = md5($md5[$i - 1].$concatedPassphrase, true);
$result .= $md5[$i];
}
$key = substr($result, 0, 32);
$data = openssl_decrypt($ct, 'aes-256-cbc', $key, true, $iv);
Log::alert("data");
Log::alert($data);
return $data;
}
$jsondata returns
{"$super":{"$super":{}},"ciphertext":{"words":[-1208966431,415513002,-380428285,-1848403568],"sigBytes":16},
"key":{"$super":{},"words":[1997420112,-1528658502,-601329304,170123410,85401029,1271916135,-1899682466,856024313,-1926693888,505524620,1922976396,79922502],"sigBytes":32},
"iv":{"words":[-1926693888,505524620,1922976396,79922502],"sigBytes":16},"algorithm":{"keySize":8,"$super":{"cfg":{"mode":{"$super":{},"Encryptor":{},"Decryptor":{}},"padding":{},"$super":{}},"blockSize":4,"$super":{"keySize":4,"ivSize":4,"_ENC_XFORM_MODE":1,"_DEC_XFORM_MODE":2,"$super":{"_minBufferSize":0}}}},"blockSize":4,"formatter":{},
"salt":{"words":[3335652791,3193595722],"sigBytes":8}}
But can't get other variables correctly to decrypt this encryption and find the original plain text. How can I edit this function to make it work?
I try to clone this answer Encryption in JavaScript and decryption with PHP
I have a nested JSON object and i want to save the values into a txt file.
This is the nested Json object
"objectJSON":{
"alt":"136.22",
"lat":"46.7484",
"lng":"33.0685"
}
I use the following PHP code to save the values into a txt.file
<?php
header("Content-type: application/json");
$json = file_get_contents("php://input");
$obj = json_decode($json);
$decoded = base64_decode(json_encode($obj->data));
$encoded = json_encode($obj->objectJSON);
$fp = #fopen("TMP.txt", "a"); //Open and write into TMP.txt
fwrite($fp, $encoded);
fwrite($fp,"\r\n");
fclose($fp);
?>
And here are the values into the TMP.txt file
"{\"alt\":136.22,\"lat\":46.7484,\"lng\":33.0685}"
And here is the problem:
What do I have to change/add to save only the "alt" value in the TMP.txt file?
HERE ARE MORE INFORMATION ABOUT MY PROJECT.
I receive the data via a lora node to a lora gateway which send them to my lora server. Here is a part from the lora node code that has been uploaded on it:
void GPSWrite()
{
/*Convert GPS data to format*/
datastring1 +=dtostrf(flat, 0, 6, gps_lat);
datastring2 +=dtostrf(flon, 0, 6, gps_lon);
//datastring3 +=dtostrf(falt, 0, 2, gps_alt);
if(flon!=1000.000000)
{
strcat(gps_lon,",");
strcat(gps_lon,gps_lat);
//strcat(gps_lon,",");
//strcat(gps_lon,gps_alt);
int i = 0;
for(i = 0; i < 2; i++)
{
//datasend.toFloat();
atof(gps_lon);
//Serial.println((char*)datasend);
Serial.println("Testing converted data:");
Serial.println(gps_lon);
// atof(gps_alt);
// Serial.print(gps_alt);
}
strcpy(datasend,gps_lon); //the format of datasend is longtitude,latitude,altitude
Serial.print("########### ");
Serial.print("NO.");
Serial.print(count);
Serial.println(" ###########");
Serial.println("The longtitude and latitude are:");
Serial.print("[");
Serial.print((char*)datasend);
Serial.print("]");
Serial.print("");
/*
for(int k = 0; k < 20;k++)
{
Serial.print("[");
Serial.print(datasend[k], HEX);
Serial.print("]");
}
Serial.println("");
Serial.println("");*/
count++;
}
int32_t lat = flat * 10000;
int32_t lng = flon * 10000;
datasend[0] = lng;
datasend[1] = lng >> 8;
datasend[2] = lng >> 16;
datasend[3] = lat;
datasend[4] = lat >> 8;
datasend[5] = lat >> 16;
smartdelay(1000);
}
static void smartdelay(unsigned long ms)
{
unsigned long start = millis();
do
{
while (ss.available())
{
gps.encode(ss.read());
}
} while (millis() - start < ms);
}
void loop() {
os_runloop_once();
}
The application server decodes the data with the following function:
function Decode(fPort, bytes) {
var decoded = {};
var hexString=bin2HexStr(bytes);
return rakSensorDataDecode(hexString);
}
// convert array of bytes to hex string.
// e.g: 0188053797109D5900DC140802017A0768580673256D0267011D040214AF0371FFFFFFDDFC2E
function bin2HexStr(bytesArr) {
var str = "";
for(var i=0; i<bytesArr.length; i++) {
var tmp = (bytesArr[i] & 0xff).toString(16);
if(tmp.length == 1) {
tmp = "0" + tmp;
}
str += tmp;
}
return str;
}
// convert string to short integer
function parseShort(str, base) {
var n = parseInt(str, base);
return (n << 16) >> 16;
}
// convert string to triple bytes integer
function parseTriple(str, base) {
var n = parseInt(str, base);
return (n << 8) >> 8;
}
// decode Hex sensor string data to object
function rakSensorDataDecode(hexStr) {
var str = hexStr;
var myObj = {};
while (str.length > 4) {
var flag = parseInt(str.substring(0, 4), 16);
{
myObj.lat = (bytes[3] | bytes[4]<<8 | bytes[5]<<16 | (bytes[5] & 0x80 ? 0xFF<<24 : 0)) / 10000;
myObj.lng = (bytes[0] | bytes[1]<<8 | bytes[2]<<16 | (bytes[2] & 0x80 ? 0xFF<<24 : 0)) / 10000;
myObj.alt = ((bytes[6] << 8) + bytes[7])/100;
str = str.substring(22);
}
}
return myObj;
}
so I get through my application server the following results:
received data
Then I use the php script to save data on txt, file. As I have told you, the problem is that I can save all data but can't save for example only the altitude.
The only thing you have to do is decode and then encode and write the proper one:
$json = file_get_contents("php://input");
$obj = json_decode($json);
file_put_contents("TMP.txt", json_encode($obj->objectJSON->alt));
If you want to append then:
file_put_contents("TMP.txt",
file_get_contents("TMP.txt") . json_encode($obj->objectJSON->alt));
But that won't be valid JSON in the file.
You are using the encoded data, instead use decoded data if you want to access the 'alt' attribute, here is an example :
<?php
$obj = (object) [
"objectJSON" => [
'alt' => '136.22',
'lat' => '46.7484',
'lng' => '33.0685'
]
];
$encoded = json_encode($obj->objectJSON);
$decoded = json_decode($encoded);
$fp = #fopen("TMP.txt", "a"); //Open and write into TMP.txt
fwrite($fp, $decoded->alt);
fwrite($fp,"\r\n");
fclose($fp);
?>
You just need to extract the value from the object:
$fp = #fopen("TMP.txt", "a"); //Open and write into TMP.txt
fwrite($fp, $decoded->alt);
// ^^^^^^^
fwrite($fp,"\r\n");
fclose($fp);
Finally, I can save my data in a txt file and I can insert my data into a MySQL database at the same time. Thank you all for your help. Here is the PHP script
<?php
header("Content-type: application/json");
$json = file_get_contents("php://input");
$obj = json_decode($json);
$decoded = base64_decode(json_encode($obj->data));
$fp = #fopen("TMP.txt", "a"); //Open and write into TMP.txt
fwrite($fp,$decoded);
fwrite($fp,"\r\n");
fclose($fp);
$server = "#######";
$username = "#######";
$password = "#######";
$DB = "#######";
$conn = new mysqli($server, $username, $password, $DB);
if ($conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}
$insert = "INSERT INTO gpssensor(longitude, latitude) VALUES ($decoded)";
$conn->query($insert);
$conn->close();
?>
I been working on this for days.
Our backend have a signature checking which is done using PHP:
private $HMAC_ALGO = 'md5';
public function decodeAndValidateMessage($data,$signature,$secretkey) {
if (!is_string($data)) {
throw new InvalidRequestException($data);
}
$decodedData = base64_decode($data);
// if not json returned the throw exception...
$jsonDecoded = json_decode($decodedData,true);
if (!$jsonDecoded) {
throw new InvalidRequestException($decodedData);
}
// validate
$signatureRef = base64_encode(hash_hmac($this->HMAC_ALGO,$decodedData,$secretkey,true));
if ($signature === $signatureRef) {
return $jsonDecoded;
} else {
throw new InvalidSignatureException();
}
}
I made it work on iOS:
func hmac(_ algorithm: HMACAlgorithm, key: String) -> String {
let cKey = key.cString(using: String.Encoding.utf8)
let cData = self.cString(using: String.Encoding.utf8)
var result = [CUnsignedChar](repeating: 0, count: Int(algorithm.digestLength()))
CCHmac(algorithm.toCCHmacAlgorithm(), cKey!, Int(strlen(cKey!)), cData!, Int(strlen(cData!)), &result)
let hmacData:Data = Data(bytes: UnsafePointer<UInt8>(result), count: (Int(algorithm.digestLength())))
let hmacBase64 = hmacData.base64EncodedString(options: NSData.Base64EncodingOptions(rawValue: 0))
print(String(hmacBase64))
return String(hmacBase64)
}
Any idea/help on doing this on Kotlin/Android? I'm being stuck on InvalidSignatureException error.
fun generateSignature(data : HashMap<String, Any>) : String {
val hmac = Mac.getInstance("HmacMD5")
hmac.init(SecretKeySpec(Constant.PRIVATEKEY.toByteArray(Charsets.UTF_8), hmac.algorithm))
return Base64.encodeToString(data.toString().toByteArray(),Base64.URL_SAFE + Base64.NO_PADDING + Base64.NO_CLOSE + Base64.NO_WRAP)
}
Thanks :D I really appreciate for any help :D
Update:
Just to make my question simpler?
Is it possible to make translate the iOS line of code to Kotlin?
enum HMACAlgorithm {
case md5, sha1, sha224, sha256, sha384, sha512
func toCCHmacAlgorithm() -> CCHmacAlgorithm {
var result: Int = 0
switch self {
case .md5:
result = kCCHmacAlgMD5
case .sha1:
result = kCCHmacAlgSHA1
case .sha224:
result = kCCHmacAlgSHA224
case .sha256:
result = kCCHmacAlgSHA256
case .sha384:
result = kCCHmacAlgSHA384
case .sha512:
result = kCCHmacAlgSHA512
}
return CCHmacAlgorithm(result)
}
func digestLength() -> Int {
var result: CInt = 0
switch self {
case .md5:
result = CC_MD5_DIGEST_LENGTH
case .sha1:
result = CC_SHA1_DIGEST_LENGTH
case .sha224:
result = CC_SHA224_DIGEST_LENGTH
case .sha256:
result = CC_SHA256_DIGEST_LENGTH
case .sha384:
result = CC_SHA384_DIGEST_LENGTH
case .sha512:
result = CC_SHA512_DIGEST_LENGTH
}
return Int(result)
}
}
this is how I call the function
var params : Dictionary
params.generateSignature()
fun generateSignature(data : HashMap) : String {
val hmac = Mac.getInstance("HmacMD5")
hmac.init(SecretKeySpec(Constant.PRIVATEKEY.toByteArray(Charsets.UTF_8), hmac.algorithm))
return Base64.encodeToString(data.toString().toByteArray(),Base64.URL_SAFE + Base64.NO_PADDING + Base64.NO_CLOSE + Base64.NO_WRAP)
}
Someone finally found out the answer.
My mistake is hashmap should be run under JSONObject
var obj = JsonObject(data)
and use obj.toString() :D
I currently handle MongoDB with PHP.
I am trying to process data on Android using that value.
PHP:
public function Find($collection, $query = null, $fields = null) {
/* init */
$this->mClient = new MongoClient(...);
$this->mDBName = $dbName;
$this->mDB = $this->mClient->{$this->mDBName};
if(!isset($this->mDB)) {
// TODO
return;
}
/* find query */
$coll = $this->mDB->{$collection};
if(isset($query)) {
if(isset($fields)) $cursor = $coll->find($query, $fields);
else $cursor = $coll->find($query);
} else {
$cursor = $coll->find();
}
return json_encode(iterator_to_array($cursor, false));
}
ANDROID:
// Get String From PHP
// ex) [{"_id":{"$id":"59ad4d2425b572b7124be684"},"name":"\uacf5\ud3ec"},{"_id":{"$id":"59ad4d3625b572b7124be69a"},"name":"SF"}]
String result = getHttpData(getInstance().mStrUrl, data);
// In this part, data processing is done
List<DBObject> arr = new ArrayList<DBObject>();
//JSONObject json = new JSONObject(result);
JSONArray jsonArray = new JSONArray(result);
int len = jsonArray.length();
for (int i=0;i<len;i++){
String json = jsonArray.get(i).toString();
//Object o = com.mongodb.util.JSON.parse(result);
Object o = com.mongodb.util.JSON.parse(json);
DBObject dbObj = (DBObject) o;
arr.add(dbObj);
}
In the above case, referring to "_ id" will return BasicDBObject. It is necessary to receive an ObjectID.
Likewise for child document "_id" should be ObjectID.
HOW?
In our in house system, we have csv & xls files generated for users (phpexcel) from our MySQL database, but my boss is asking if is's possible to create a mdb file.
I've never come across any sample on creating a new MDB file on the fly, and I wonder if it's possible. I'm not an expert programmer by any chance, but I can manage if anyone could guide me on how I should proceed.
We're trying to reduce user load/mistakes by providing them with mdb instead of CSV that they have to import into Access. Currently they have to import over 100 csv files into Access.
Thank you.
You can use COM for accessing the MDB driver, There is a simple class located at phpclasses that acts as a wrapper for this, you can either use it for inspiration or port it directly, either way it's perfectly possible:
Here is the class, the source link is provided below:
<?php
class mdb
{
var $RS = 0;
var $ADODB = 0;
var $RecordsAffected;
var $strProvider = 'Provider=Microsoft.Jet.OLEDB.4.0';
var $strMode = 'Mode=ReadWrite';
var $strPSI = 'Persist Security Info=False';
var $strDataSource = '';
var $strConn = '';
var $strRealPath = '';
var $recordcount = 0;
var $ok = false;
/**
* Constructor needs path to .mdb file
*
* #param string $dsn = path to *.mdb file
* #return boolean success
*/
function mdb( $dsn='Please enter DataSource!' )
{
$this->strRealPath = realpath( $dsn );
if( strlen( $this->strRealPath ) > 0 )
{
$this->strDataSource = 'Data Source='.$this->strRealPath;
$result = true;
}
else
{
echo "<br>mdb::mdb() File not found $dsn<br>";
$result = false;
}
$this->RecordsAffected = new VARIANT();
$this->open();
} // eof constructor mdb()
function open( )
{
if( strlen( $this->strRealPath ) > 0 )
{
$this->strConn =
$this->strProvider.';'.
$this->strDataSource.';'.
$this->strMode.';'.
$this->strPSI;
$this->ADODB = new COM( 'ADODB.Connection' );
if( $this->ADODB )
{
$this->ADODB->open( $this->strConn );
$result = true;
}
else
{
echo '<br>mdb::open() ERROR with ADODB.Connection<br>'.$this->strConn;
$result = false;
}
}
$this->ok = $result;
return $result;
} // eof open()
/**
* Execute SQL-Statement
* #param string $strSQL = sql statement
* #param boolean $getrecordcount = true when a record count is wanted
*/
function execute( $strSQL, $getrecordcount = false )
{
$this->RS = $this->ADODB->execute( $strSQL, &$this->RecordsAffected );
if( $getrecordcount == true )
{
$this->RS->MoveFirst();
$this->recordcount = 0;
# brute force loop
while( $this->RS->EOF == false )
{
$this->recordcount++;
$this->RS->MoveNext();
}
$this->RS->MoveFirst();
}
} // eof execute()
function eof()
{
return $this->RS->EOF;
} // eof eof()
function movenext( )
{
$this->RS->MoveNext();
} // eof movenext()
function movefirst()
{
$this->RS->MoveFirst();
} // eof movefirst()
function close()
{
#$this->RS->Close(); // Generates a warning when without "#"
$this->RS=null;
#$this->ADODB->Close();
$this->ADODB=null;
} // eof close()
function fieldvalue( $fieldname )
{
return $this->RS->Fields[$fieldname]->value;
} // eof fieldvalue()
function fieldname( $fieldnumber )
{
return $this->RS->Fields[$fieldnumber]->name;
} // eof fieldname()
function fieldcount( )
{
return $this->RS->Fields->Count;
} // eof fieldcount()
} // eoc mdb
?>
And an example:
include 'class_mdb.php';
$mdb = new mdb('mymdbfile.mdb'); // your own mdb filename required
$mdb->execute('select * from table'); // your own table in the mdb file
#
# first example: using fieldnames
#
while( !$mdb->eof() )
{
echo $mdb->fieldvalue('description'); // using your own fields name
echo ' = ';
echo $mdb->fieldvalue( 1 ); // using the fields fieldnumber
echo '<br>';
$mdb->movenext();
}
echo '<br><hr><br>';
#
# Going back to the first recordset for the second example
#
$mdb->movefirst();
#
# This works, too: Make each Field an object. The values change
# when the data pointer advances with movenext().
#
$url = $mdb->RS->Fields(1);
$bez = $mdb->RS->Fields(2);
$kat = $mdb->RS->Fields(3);
while( !$mdb->eof() )
{
# works!
echo $bez->value;
echo ' = ';
echo $url->value;
echo '<br>';
$mdb->movenext();
}
$mdb->close();
Source: http://www.phpclasses.org/package/1700-PHP-Access-Microsoft-Access-mdb-database-files.html
To create a new empty Microsoft Access Database we can use ADOX.
The code below creates an empty Test.mdb file in c:\
$adox_catalog = new COM("ADOX.Catalog");
$adox_catalog->create('Provider = Microsoft.Jet.OLEDB.4.0; Data Source=c:\Test.mdb');
$adodb_conection = $adox_catalog->activeconnection();