My little contribution to reducing app fraud
Verifying the signature on an external server on your Android code:
verifySignatureOnServer ()
private boolean verifySignatureOnServer(String data, String signature) {
String retFromServer = "";
URL url;
HttpsURLConnection urlConnection = null;
try {
String urlStr = "https://www.example.com/verify.php?data=" + URLEncoder.encode(data, "UTF-8") + "&signature=" + URLEncoder.encode(signature, "UTF-8");
url = new URL(urlStr);
urlConnection = (HttpsURLConnection) url.openConnection();
InputStream in = urlConnection.getInputStream();
InputStreamReader inRead = new InputStreamReader(in);
retFromServer = convertStreamToString(inRead);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
}
return retFromServer.equals("good");
}
convertStreamToString ()
private static String convertStreamToString(java.io.InputStreamReader is) {
java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
return s.hasNext() ? s.next() : "";
}
verify.php in the web hosting root directory
<?php
$data = $_GET['data'];
$signature = $_GET['signature'];
$key_64 = ".... put here the base64 encoded pub key from google play console , all in one row !! ....";
$key = "-----BEGIN PUBLIC KEY-----\n".
chunk_split($key_64, 64,"\n").
'-----END PUBLIC KEY-----';
$key = openssl_get_publickey($key);
$ok = openssl_verify($data, base64_decode($signature), $key, OPENSSL_ALGO_SHA1);
if ($ok == 1) {
echo "good";
} elseif ($ok == 0) {
echo "bad";
} else {
die ("fault, error checking signature");
}
openssl_free_key($key);
?>
NOTES:
You should encrypt the url in your java code if the url cannot be easily found with a simple text search in your unpacked apk application
It is also better to change the php file name, url arguments, good / bad answers to something without meaning.
verifySignatureOnServer () should be run in a separate thread if a network is not selected if the main thread is excluded.
Hope this helps ...
source
share