private String storeKeysIV(String monkeyId, String pubKey) { // Encrypt workers RSAUtil rsa = new RSAUtil(Base64.decode(pubKey.getBytes(), 0)); String usk = rsa.encrypt(aesUtil.strKey + ":" + aesUtil.strIV); // Guardo mis key & Iv KeyStoreCriptext.putString(ctxRef.get(), monkeyId, aesUtil.strKey + ":" + aesUtil.strIV); return usk; }
private ServerResponse userSync(final String monkeyId) throws Exception { // Create a new HttpClient and Post Header RSAUtil rsaUtil = new RSAUtil(); rsaUtil.generateKeys(); HttpClient httpclient = MonkeyHttpClient.newClient(); HttpPost httppost = MonkeyHttpClient.newPost( MonkeyKitSocketService.Companion.getHttpsURL() + "/user/key/sync", urlUser, urlPass); JSONObject localJSONObject1 = new JSONObject(); localJSONObject1.put("monkey_id", monkeyId); localJSONObject1.put( "public_key", "-----BEGIN PUBLIC KEY-----\n" + rsaUtil.getPublicKey() + "\n-----END PUBLIC KEY-----"); // System.out.println("-----BEGIN PUBLIC KEY-----\n" + rsaUtil.pubKeyStr + "\n-----END PUBLIC // KEY-----"); JSONObject params = new JSONObject(); params.put("data", localJSONObject1.toString()); Log.d("userSyncMS", "Req: " + params.toString()); JSONObject finalResult = MonkeyHttpClient.getResponse(httpclient, httppost, params.toString()); Log.d("userSyncMS", finalResult.toString()); finalResult = finalResult.getJSONObject("data"); final String keys = finalResult.getString("keys"); final long lastSync = finalResult.getLong("last_time_synced"); String decriptedKey = rsaUtil.desencrypt(keys); KeyStoreCriptext.putString(ctxRef.get(), monkeyId, decriptedKey); KeyStoreCriptext.setLastSync(ctxRef.get(), lastSync); try { aesUtil = new AESUtil(ctxRef.get(), monkeyId); } catch (Exception ex) { ex.printStackTrace(); // Como fallo algo con esas keys las encero y creo unas nuevas KeyStoreCriptext.putString(ctxRef.get(), monkeyId, ""); aesUtil = new AESUtil(ctxRef.get(), monkeyId); return getSessionHTTP(this.urlUser, this.urlPass, this.userInfo, this.ignore_params); } final String domain = finalResult.getString("sdomain"); final int port = finalResult.getInt("sport"); return new ServerResponse(monkeyId, domain, port); }