package com.baidu.techain.i; import android.accounts.NetworkErrorException; import android.content.Context; import android.text.TextUtils; import android.util.Base64; import com.baidu.techain.ac.F; import com.baidu.techain.core.h; import com.baidu.techain.h.a; import com.meizu.cloud.pushsdk.constants.PushConstants; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.net.URLEncoder; import java.util.Date; import java.util.HashMap; import java.util.Map; import org.json.JSONObject; /* compiled from: EncryptConnUtil */ public final class f { public static String a(Context context, String str, String str2, boolean z, boolean z2) throws Throwable { return b(context, str, str2, z, z2); } private static String b(Context context, String str, String str2, boolean z, boolean z2) throws Throwable { byte[] bArr; String[] g = c.g(context); String str3 = g[0]; String str4 = g[1]; if (!"com.baidu.input_huawei".equals(context.getPackageName()) || a.a(context).w()) { String valueOf = String.valueOf(new Date().getTime() / 1000); String a = n.a(str3 + valueOf + str4); byte[] a2 = h.a(); String str5 = ""; if (!TextUtils.isEmpty(str2)) { ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(str2.getBytes()); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); i.a(byteArrayInputStream, byteArrayOutputStream); byte[] byteArray = byteArrayOutputStream.toByteArray(); byteArrayOutputStream.flush(); byteArrayOutputStream.close(); byteArrayInputStream.close(); bArr = F.getInstance().ae(byteArray, a2); } else { bArr = str5.getBytes(); } byte[] bytes = n.a(e.b(context)).getBytes(); String encodeToString = Base64.encodeToString(F.getInstance().re(a2, bytes), 0); StringBuilder sb = new StringBuilder(); sb.append(str); sb.append("/120/"); sb.append(str3); sb.append("/"); sb.append(valueOf); sb.append("/"); sb.append(a); if (!TextUtils.isEmpty(encodeToString)) { sb.append("?skey="); sb.append(URLEncoder.encode(encodeToString, "utf-8")); } try { if (q.a(context)) { str5 = new q(context).a(sb.toString(), bArr); } else { str5 = new l(context).a(sb.toString(), bArr); } } catch (Throwable unused) { c.a(); } if (z && TextUtils.isEmpty(str5)) { try { a a3 = a.a(context); long currentTimeMillis = System.currentTimeMillis(); long j = a3.a.getLong("pu_cl_fd", 0); if (j == 0) { j = System.currentTimeMillis(); a3.f(); } if (currentTimeMillis - j > 86400000) { HashMap hashMap = new HashMap(); if (c.e(context)) { hashMap.put(PushConstants.PUSH_TYPE_NOTIFY, Integer.valueOf(a3.h() + 1)); hashMap.put("1", Integer.valueOf(a3.i())); } else { hashMap.put(PushConstants.PUSH_TYPE_NOTIFY, Integer.valueOf(a3.h())); hashMap.put("1", Integer.valueOf(a3.i() + 1)); } a3.b.putInt("mo_fa_pu_cl", 0); a3.b.commit(); a3.b.putInt("wi_fa_pu_cl", 0); a3.b.commit(); a3.f(); c.a(context, "1003112", (Map<String, Object>) hashMap, false); } else if (c.e(context)) { a3.a(a3.h() + 1); } else { a3.b(a3.i() + 1); } } catch (Throwable unused2) { c.a(); } throw new NetworkErrorException("response is empty"); } else if (!z2) { return str5; } else { JSONObject jSONObject = new JSONObject(str5); byte[] rd = F.getInstance().rd(Base64.decode(jSONObject.optString("skey"), 0), bytes); String optString = jSONObject.optString("response"); jSONObject.optString("request_id"); byte[] decode = Base64.decode(optString, 0); byte[] ad = F.getInstance().ad(decode, rd); if (decode == null || decode.length <= 0 || (ad != null && ad.length != 0)) { return new String(ad); } c.h(context); throw new NetworkErrorException("aes is fail"); } } else { throw new NetworkErrorException("conn is blocked"); } } }