Commit e80d426b authored by wanglei's avatar wanglei

...

parent e441a352
package com.base.locationsharewhite.helper
import android.util.Base64
import java.security.SecureRandom
import javax.crypto.Cipher
import javax.crypto.spec.GCMParameterSpec
import javax.crypto.spec.SecretKeySpec
object AESHelper {
private const val aesKey = "7vwdrlk6bp8rihe4"
private val cipher by lazy {
Cipher.getInstance("AES/GCM/NoPadding")
}
fun encrypt(content: String): String {
try {
val iv = ByteArray(12).apply {
SecureRandom().nextBytes(this)
}
val contentBytes = content.toByteArray(Charsets.UTF_8)
val params = GCMParameterSpec(128, iv)
cipher.init(
Cipher.ENCRYPT_MODE,
secretKey, params
)
val encryptData = cipher.doFinal(contentBytes)
if (encryptData.size != contentBytes.size + 16) {
throw IllegalStateException("Encryption failed")
}
val message = ByteArray(12 + contentBytes.size + 16)
System.arraycopy(iv, 0, message, 0, 12)
System.arraycopy(encryptData, 0, message, 12, encryptData.size)
return String(Base64.encode(message, Base64.NO_WRAP), Charsets.UTF_8)
} catch (_: Exception) {
}
return content
}
@Synchronized
fun decrypt(content: String): String {
try {
val con = content.replace(" ".toRegex(), "+")
val contentByte = Base64.decode(con, Base64.NO_WRAP)
require(contentByte.size >= 12 + 16)
val params = GCMParameterSpec(128, contentByte, 0, 12)
cipher.init(
Cipher.DECRYPT_MODE,
secretKey, params
)
val decryptData = cipher.doFinal(contentByte, 12, contentByte.size - 12)
return String(decryptData, Charsets.UTF_8)
} catch (_: Exception) {
}
return content
}
private val secretKey by lazy {
SecretKeySpec(aesKey.toByteArray(), "AES")
}
}
\ No newline at end of file
package com.base.locationsharewhite.helper;
import android.text.TextUtils;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;
import com.base.locationsharewhite.utils.LogEx;
public class ReportUtils {
private static String TAG = "ReportUtils";
public static String doPost(String urlPath, Map<String, String> paramsMap, String json) {
try {
HttpURLConnection conn = (HttpURLConnection) new URL(urlPath).openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setConnectTimeout(600000);
if (!TextUtils.isEmpty(json)) {
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Content-Length", Integer.toString(json.getBytes().length));
conn.getOutputStream().write(json.getBytes());
}
StringBuilder result = new StringBuilder();
for (Map.Entry<String, String> entry : paramsMap.entrySet()) {
result.append("&").append(entry.getKey()).append("=").append(entry.getValue());
}
if (result.length() > 0) {
conn.getOutputStream().write(result.substring(1).getBytes());
} else {
// conn.getOutputStream().write(result.substring(0).getBytes());
}
if (conn.getResponseCode() == 200) {
String s = new BufferedReader(new InputStreamReader(conn.getInputStream())).readLine();
if (!TextUtils.isEmpty(s)) {
} else {
s = "";
}
LogEx.INSTANCE.logDebug(TAG, "code=200", false);
return s;
} else {
LogEx.INSTANCE.logDebug(TAG, "code!=200", false);
}
} catch (Exception e) {
e.printStackTrace();
}
return "{ \"success\": false,\n \"errorMsg\": \"后台服务器开小差了!\",\n \"result\":{}}";
}
}
package com.base.locationsharewhite.location
import android.os.Build
import com.base.locationsharewhite.BuildConfig
import com.base.locationsharewhite.helper.AESHelper
import com.base.locationsharewhite.helper.ConfigHelper
import com.base.locationsharewhite.helper.ReportUtils.doPost
import com.base.locationsharewhite.utils.AppPreferences
import com.base.locationsharewhite.utils.LogEx
import org.json.JSONObject
object LocationRequestUtils {
var invitationCodeSp = ""
get() {
return AppPreferences.getInstance().getString("invitationCodeSp", field)
}
set(value) {
field = value
AppPreferences.getInstance().put("invitationCodeSp", value, true)
}
var nickNameSp = ""
get() {
return AppPreferences.getInstance().getString("nickNameSp", field)
}
set(value) {
field = value
AppPreferences.getInstance().put("nickNameSp", value, true)
}
var uIdSp = ""
get() {
return AppPreferences.getInstance().getString("uIdSp", field)
}
set(value) {
field = value
AppPreferences.getInstance().put("uIdSp", value, true)
}
private val TAG = "LocationRequestUtils"
private const val DATA_KEY = "data"
private val url by lazy {
val pkg = ConfigHelper.packageName
val url = StringBuilder(
"${ConfigHelper.eventUrl}/dingwei/${pkg.filter { it.isLowerCase() }.substring(4, 9)}v"
)
url.toString()
}
fun login(name: String) {
Thread {
val pkg = ConfigHelper.packageName
val data = JSONObject()
data.put("nickName", name)
val bp = JSONObject()
// .put("${pkg}_1", "")
.put("${pkg}_5", Build.VERSION.SDK_INT)
.put("${pkg}_8", BuildConfig.VERSION_NAME)
.put("${pkg}_9", AppPreferences.getInstance().getString("uuid", ""))
.put("${pkg}_10", AppPreferences.getInstance().getString("gid", ""))
.put("${pkg}_13", "android")
.put("${pkg}_14", BuildConfig.VERSION_CODE)
.put("${pkg}_15", "google")
.put("${pkg}_24", BuildConfig.BUILD_TYPE)
val body = JSONObject()
.put("data", data)
.put("bp", bp)
.toString()
val paramJson = AESHelper.encrypt(body)
LogEx.logDebug(TAG, "url=$url")
val result = doPost(url, HashMap(), paramJson)
val responseData = extractData(result)
if (responseData != null) {
val decryptedData = AESHelper.decrypt(responseData)
parseLoginData(decryptedData)
}
}.start()
}
private fun extractData(response: String): String? {
val regex = Regex("\"$DATA_KEY\":\"(.*?)\"")
val match = regex.find(response)
return match?.groupValues?.get(1)
}
private fun parseLoginData(json: String) {
val data: JSONObject = JSONObject(json)
val invitationCode = data.getString("invitationCode")
invitationCodeSp = invitationCode
val nickName = data.getString("nickName")
nickNameSp = nickName
val uId = data.getString("uId")
uIdSp = uId
LogEx.logDebug(TAG, "invitationCode=$invitationCode nickName=$nickName uId=$uId")
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment