package com.worklight.wlclient.api;

import android.content.Context;
import com.worklight.common.WLConfig;
import com.worklight.nativeandroid.common.WLUtils;
import com.worklight.wlclient.WLRequestListener;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.json.JSONException;
import org.json.JSONObject;

public class SecurityUtils
{
  private static final int CURRENT_VERSION = 1;
  public static final String CYPHER_TEXT_LABEL = "ct";
  private static final String ENCRYPTION_SOURCE = "java";
  public static final String ENCRYPTION_SOURCE_LABEL = "src";
  private static final int IV_BYTE_SIZE = 16;
  public static final String IV_LABEL = "iv";
  private static final int PBKDF2_LENGTH_BYTE_SIZE = 32;
  private static final String UTF_8 = "UTF-8";
  public static final String VERSION_LABEL = "v";

  static URL buildURL(int paramInt, WLConfig paramWLConfig)
    throws MalformedURLException
  {
    return new URL(paramWLConfig.getRootURL() + "/" + "apps/services/random" + "?bytes=" + paramInt);
  }

  public static String decrypt(String paramString, JSONObject paramJSONObject)
    throws SecurityUtilsException
  {
    if ((paramString == null) || (paramString.length() <= 0))
      throw new SecurityUtilsException("Key cannot be null or empty.");
    if ((paramJSONObject == null) || (paramJSONObject.length() <= 0) || (!paramJSONObject.has("ct")) || (!paramJSONObject.has("iv")) || (!paramJSONObject.has("src")) || (!paramJSONObject.has("v")))
      throw new SecurityUtilsException("The given encrypted object is invalid or null.");
    try
    {
      if (!paramJSONObject.getString("src").equals("java"))
        throw new SecurityUtilsException("The encrypted object was generated in another environment. Cannot decrypt in this environment.");
    }
    catch (JSONException localJSONException)
    {
      throw new SecurityUtilsException("There was a problem while decrypting. Make sure the given encryptedObject is valid.", localJSONException);
      String str1 = paramJSONObject.getString("ct");
      String str2 = paramJSONObject.getString("iv");
      byte[] arrayOfByte = WLUtils.hexStringToByteArray(str1);
      String str3 = new String(initCipher(2, WLUtils.hexStringToByteArray(paramString), WLUtils.hexStringToByteArray(str2)).doFinal(arrayOfByte), "UTF-8");
      return str3;
    }
    catch (Exception localException)
    {
      throw new SecurityUtilsException("There was a problem while adding properties to the returned JSONObject.", localException);
    }
  }

  // ERROR //
  public static JSONObject encrypt(String paramString1, String paramString2)
    throws SecurityUtilsException
  {
    // Byte code:
    //   0: aload_0
    //   1: ifnull +10 -> 11
    //   4: aload_0
    //   5: invokevirtual 87	java/lang/String:length	()I
    //   8: ifgt +13 -> 21
    //   11: new 77	com/worklight/wlclient/api/SecurityUtilsException
    //   14: dup
    //   15: ldc 89
    //   17: invokespecial 90	com/worklight/wlclient/api/SecurityUtilsException:<init>	(Ljava/lang/String;)V
    //   20: athrow
    //   21: aload_1
    //   22: ifnull +10 -> 32
    //   25: aload_1
    //   26: invokevirtual 87	java/lang/String:length	()I
    //   29: ifgt +13 -> 42
    //   32: new 77	com/worklight/wlclient/api/SecurityUtilsException
    //   35: dup
    //   36: ldc 151
    //   38: invokespecial 90	com/worklight/wlclient/api/SecurityUtilsException:<init>	(Ljava/lang/String;)V
    //   41: athrow
    //   42: bipush 16
    //   44: invokestatic 155	com/worklight/wlclient/api/SecurityUtils:getRandomString	(I)Ljava/lang/String;
    //   47: astore_2
    //   48: iconst_1
    //   49: aload_0
    //   50: invokestatic 120	com/worklight/nativeandroid/common/WLUtils:hexStringToByteArray	(Ljava/lang/String;)[B
    //   53: aload_2
    //   54: invokestatic 120	com/worklight/nativeandroid/common/WLUtils:hexStringToByteArray	(Ljava/lang/String;)[B
    //   57: invokestatic 124	com/worklight/wlclient/api/SecurityUtils:initCipher	(I[B[B)Ljavax/crypto/Cipher;
    //   60: astore 7
    //   62: aload 7
    //   64: aload_1
    //   65: invokevirtual 159	java/lang/String:getBytes	()[B
    //   68: invokevirtual 130	javax/crypto/Cipher:doFinal	([B)[B
    //   71: astore 10
    //   73: aload 10
    //   75: invokestatic 163	com/worklight/nativeandroid/common/WLUtils:byteArrayToHexString	([B)Ljava/lang/String;
    //   78: astore 11
    //   80: new 92	org/json/JSONObject
    //   83: dup
    //   84: invokespecial 164	org/json/JSONObject:<init>	()V
    //   87: astore 12
    //   89: aload 12
    //   91: ldc 11
    //   93: aload 11
    //   95: invokevirtual 168	org/json/JSONObject:put	(Ljava/lang/String;Ljava/lang/Object;)Lorg/json/JSONObject;
    //   98: pop
    //   99: aload 12
    //   101: ldc 22
    //   103: aload_2
    //   104: invokevirtual 168	org/json/JSONObject:put	(Ljava/lang/String;Ljava/lang/Object;)Lorg/json/JSONObject;
    //   107: pop
    //   108: aload 12
    //   110: ldc 17
    //   112: ldc 14
    //   114: invokevirtual 168	org/json/JSONObject:put	(Ljava/lang/String;Ljava/lang/Object;)Lorg/json/JSONObject;
    //   117: pop
    //   118: aload 12
    //   120: ldc 30
    //   122: iconst_1
    //   123: invokevirtual 171	org/json/JSONObject:put	(Ljava/lang/String;I)Lorg/json/JSONObject;
    //   126: pop
    //   127: aload 12
    //   129: areturn
    //   130: astore 6
    //   132: new 77	com/worklight/wlclient/api/SecurityUtilsException
    //   135: dup
    //   136: ldc 173
    //   138: aload 6
    //   140: invokespecial 114	com/worklight/wlclient/api/SecurityUtilsException:<init>	(Ljava/lang/String;Ljava/lang/Throwable;)V
    //   143: athrow
    //   144: astore 5
    //   146: new 77	com/worklight/wlclient/api/SecurityUtilsException
    //   149: dup
    //   150: ldc 173
    //   152: aload 5
    //   154: invokespecial 114	com/worklight/wlclient/api/SecurityUtilsException:<init>	(Ljava/lang/String;Ljava/lang/Throwable;)V
    //   157: athrow
    //   158: astore 4
    //   160: new 77	com/worklight/wlclient/api/SecurityUtilsException
    //   163: dup
    //   164: ldc 173
    //   166: aload 4
    //   168: invokespecial 114	com/worklight/wlclient/api/SecurityUtilsException:<init>	(Ljava/lang/String;Ljava/lang/Throwable;)V
    //   171: athrow
    //   172: astore_3
    //   173: new 77	com/worklight/wlclient/api/SecurityUtilsException
    //   176: dup
    //   177: ldc 173
    //   179: aload_3
    //   180: invokespecial 114	com/worklight/wlclient/api/SecurityUtilsException:<init>	(Ljava/lang/String;Ljava/lang/Throwable;)V
    //   183: athrow
    //   184: astore 9
    //   186: new 77	com/worklight/wlclient/api/SecurityUtilsException
    //   189: dup
    //   190: ldc 173
    //   192: aload 9
    //   194: invokespecial 114	com/worklight/wlclient/api/SecurityUtilsException:<init>	(Ljava/lang/String;Ljava/lang/Throwable;)V
    //   197: athrow
    //   198: astore 8
    //   200: new 77	com/worklight/wlclient/api/SecurityUtilsException
    //   203: dup
    //   204: ldc 173
    //   206: aload 8
    //   208: invokespecial 114	com/worklight/wlclient/api/SecurityUtilsException:<init>	(Ljava/lang/String;Ljava/lang/Throwable;)V
    //   211: athrow
    //   212: astore 13
    //   214: new 77	com/worklight/wlclient/api/SecurityUtilsException
    //   217: dup
    //   218: ldc 135
    //   220: aload 13
    //   222: invokespecial 114	com/worklight/wlclient/api/SecurityUtilsException:<init>	(Ljava/lang/String;Ljava/lang/Throwable;)V
    //   225: athrow
    //
    // Exception table:
    //   from	to	target	type
    //   48	62	130	java/security/InvalidKeyException
    //   48	62	144	java/security/NoSuchAlgorithmException
    //   48	62	158	javax/crypto/NoSuchPaddingException
    //   48	62	172	java/security/InvalidAlgorithmParameterException
    //   62	73	184	javax/crypto/IllegalBlockSizeException
    //   62	73	198	javax/crypto/BadPaddingException
    //   89	127	212	org/json/JSONException
  }

  public static String generateKey(String paramString1, String paramString2, int paramInt)
    throws SecurityUtilsException
  {
    return generateKey(paramString1, paramString2, paramInt, 32);
  }

  public static String generateKey(String paramString1, String paramString2, int paramInt1, int paramInt2)
    throws SecurityUtilsException
  {
    if ((paramString1 == null) || (paramString1.length() <= 0))
      throw new SecurityUtilsException("Password cannot be null or empty.");
    if ((paramString2 == null) || (paramString2.length() <= 0))
      throw new SecurityUtilsException("Salt cannot be null or empty.");
    if (paramInt1 <= 0)
      throw new SecurityUtilsException("Iterations must be greater than zero.");
    if (paramInt2 <= 0)
      throw new SecurityUtilsException("Key length must be greater than zero.");
    try
    {
      String str = WLUtils.byteArrayToHexString(com.worklight.androidgap.jsonstore.security.SecurityUtils.generateKey(paramString1, paramString2, paramInt1, paramInt2).getEncoded());
      return str;
    }
    catch (Exception localException)
    {
    }
    throw new SecurityUtilsException("Problem occured while encrypting. Make sure the given key is valid.");
  }

  private static WLConfig getConfig(Context paramContext)
  {
    try
    {
      WLConfig localWLConfig = WLConfig.getInstance();
      return localWLConfig;
    }
    catch (IllegalStateException localIllegalStateException)
    {
      WLConfig.createInstance(paramContext);
    }
    return WLConfig.getInstance();
  }

  public static String getRandomString(int paramInt)
  {
    return com.worklight.androidgap.jsonstore.security.SecurityUtils.encodeBytesAsHexString(com.worklight.androidgap.jsonstore.security.SecurityUtils.generateLocalKey(paramInt));
  }

  public static void getRandomStringFromServer(final int paramInt, Context paramContext, final WLRequestListener paramWLRequestListener)
  {
    new Runnable()
    {
      public void run()
      {
        try
        {
          WLConfig localWLConfig = SecurityUtils.getConfig(this.val$context);
          HttpURLConnection localHttpURLConnection = (HttpURLConnection)SecurityUtils.buildURL(paramInt, localWLConfig).openConnection();
          try
          {
            WLResponse localWLResponse = new WLResponse(200, SecurityUtils.readRandomStringFromInputStream(localHttpURLConnection), null);
            paramWLRequestListener.onSuccess(localWLResponse);
            return;
          }
          finally
          {
            localHttpURLConnection.disconnect();
          }
        }
        catch (Throwable localThrowable)
        {
          WLFailResponse localWLFailResponse = new WLFailResponse(WLErrorCode.UNEXPECTED_ERROR, localThrowable.getMessage(), null);
          paramWLRequestListener.onFailure(localWLFailResponse);
        }
      }
    }
    .run();
  }

  static Cipher initCipher(int paramInt, byte[] paramArrayOfByte1, byte[] paramArrayOfByte2)
    throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException
  {
    IvParameterSpec localIvParameterSpec = new IvParameterSpec(paramArrayOfByte2);
    SecretKeySpec localSecretKeySpec = new SecretKeySpec(paramArrayOfByte1, "AES");
    Cipher localCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    localCipher.init(paramInt, localSecretKeySpec, localIvParameterSpec);
    return localCipher;
  }

  static String readRandomStringFromInputStream(HttpURLConnection paramHttpURLConnection)
    throws IOException
  {
    BufferedReader localBufferedReader = new BufferedReader(new InputStreamReader(new BufferedInputStream(paramHttpURLConnection.getInputStream())));
    Object localObject1 = "";
    try
    {
      while (true)
      {
        String str1 = localBufferedReader.readLine();
        if (str1 == null)
          break;
        String str2 = (String)localObject1 + str1;
        localObject1 = str2;
      }
      return localObject1;
    }
    finally
    {
      localBufferedReader.close();
    }
  }
}

/* Location:           C:\Documents and Settings\Cesar Cabello Cea\Mis documentos\Downloads\apk-downloader\com.ikea.kompis-6_dex2jar.jar
 * Qualified Name:     com.worklight.wlclient.api.SecurityUtils
 * JD-Core Version:    0.6.2
 */