package tv.ouya.console.api;

import android.util.Base64;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.security.InvalidParameterException;
import java.security.KeyFactory;
import java.security.PublicKey;
import java.security.spec.AlgorithmParameterSpec;
import java.security.spec.PKCS8EncodedKeySpec;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import tv.ouya.console.internal.util.OuyaDateParser;

public class OuyaEncryptionHelper
{
  private Receipt createReceipt(OuyaDateParser paramOuyaDateParser, JSONObject paramJSONObject)
    throws JSONException, ParseException
  {
    Date localDate;
    if (paramJSONObject.has("purchaseDate"))
    {
      paramOuyaDateParser = new Date(paramJSONObject.getLong("purchaseDate"));
      if (!paramJSONObject.has("generateDate")) {
        break label142;
      }
      localDate = new Date(paramJSONObject.getLong("generateDate"));
    }
    for (;;)
    {
      String str2 = paramJSONObject.optString("identifier");
      String str1 = str2;
      if (str2.isEmpty()) {
        str1 = paramJSONObject.getString("sku");
      }
      str2 = paramJSONObject.getString("gamer");
      String str3 = paramJSONObject.getString("uuid");
      return new Receipt(str1, paramJSONObject.optInt("priceInCents", 0), paramOuyaDateParser, localDate, str2, str3, paramJSONObject.optDouble("localPrice", 0.0D), paramJSONObject.optString("currency", "USD"));
      paramOuyaDateParser = paramOuyaDateParser.parse(paramJSONObject.getString("date"));
      break;
      label142:
      localDate = new Date();
      localDate.setTime(0L);
    }
  }
  
  public static Product decryptProductResponse(String paramString, byte[] paramArrayOfByte)
    throws GeneralSecurityException, IOException, JSONException
  {
    paramString = Base64.decode(paramString, 0);
    if (paramString[0] != 1) {
      throw new InvalidParameterException("The receipt is encrypted using a system which is unknown");
    }
    paramArrayOfByte = KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(paramArrayOfByte));
    int i = paramString[1];
    Cipher localCipher = Cipher.getInstance("RSA");
    localCipher.init(2, paramArrayOfByte);
    paramArrayOfByte = new SecretKeySpec(localCipher.doFinal(paramString, 1, i), "AES");
    localCipher = Cipher.getInstance("AES");
    localCipher.init(2, paramArrayOfByte);
    return new Product(new JSONObject(new String(localCipher.doFinal(paramString, i + 2, paramString.length), "UTF-8")));
  }
  
  public String decryptPurchaseResponse(JSONObject paramJSONObject, PublicKey paramPublicKey)
    throws GeneralSecurityException, IOException, JSONException, ParseException
  {
    if ((!paramJSONObject.has("key")) || (!paramJSONObject.has("iv")) || (!paramJSONObject.has("blob"))) {
      throw new RuntimeException("Invalid response from server");
    }
    Object localObject = Base64.decode(paramJSONObject.getString("key"), 0);
    Cipher localCipher = Cipher.getInstance("RSA/ECB/PKCS1Padding", "BC");
    localCipher.init(2, paramPublicKey);
    paramPublicKey = new SecretKeySpec(localCipher.doFinal((byte[])localObject), "AES");
    localObject = new IvParameterSpec(Base64.decode(paramJSONObject.getString("iv"), 0));
    paramJSONObject = Base64.decode(paramJSONObject.getString("blob"), 0);
    localCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    localCipher.init(2, paramPublicKey, (AlgorithmParameterSpec)localObject);
    paramJSONObject = new JSONObject(new String(localCipher.doFinal(paramJSONObject), "UTF-8"));
    if (paramJSONObject.has("uuid")) {
      return paramJSONObject.getString("uuid");
    }
    return null;
  }
  
  public List<Receipt> decryptReceiptResponse(JSONObject paramJSONObject, PublicKey paramPublicKey)
    throws GeneralSecurityException, IOException, JSONException, ParseException
  {
    if ((!paramJSONObject.has("key")) || (!paramJSONObject.has("iv")) || (!paramJSONObject.has("blob"))) {
      throw new RuntimeException("Invalid response from server");
    }
    Object localObject = Base64.decode(paramJSONObject.getString("key").trim(), 0);
    Cipher localCipher = Cipher.getInstance("RSA/ECB/PKCS1Padding", "BC");
    localCipher.init(2, paramPublicKey);
    paramPublicKey = new SecretKeySpec(localCipher.doFinal((byte[])localObject), "AES");
    localObject = new IvParameterSpec(Base64.decode(paramJSONObject.getString("iv").trim(), 0));
    paramJSONObject = Base64.decode(paramJSONObject.getString("blob").trim(), 0);
    localCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    localCipher.init(2, paramPublicKey, (AlgorithmParameterSpec)localObject);
    return parseJSONReceiptResponse(new JSONObject(new String(localCipher.doFinal(paramJSONObject), "UTF-8")).getJSONArray("purchases"));
  }
  
  public List<Receipt> parseJSONReceiptResponse(String paramString)
    throws IOException
  {
    try
    {
      paramString = parseJSONReceiptResponse(new JSONArray(paramString));
      return paramString;
    }
    catch (JSONException paramString)
    {
      throw new IOException(paramString);
    }
    catch (ParseException paramString)
    {
      throw new IOException(paramString);
    }
  }
  
  public List<Receipt> parseJSONReceiptResponse(JSONArray paramJSONArray)
    throws JSONException, ParseException
  {
    ArrayList localArrayList = new ArrayList(paramJSONArray.length());
    OuyaDateParser localOuyaDateParser = new OuyaDateParser();
    int i = 0;
    while (i < paramJSONArray.length())
    {
      localArrayList.add(createReceipt(localOuyaDateParser, paramJSONArray.getJSONObject(i)));
      i += 1;
    }
    return localArrayList;
  }
}


/* Location:              /home/enzo/Documents/hacking/malwares/viking-horde/classes.jar!/tv/ouya/console/api/OuyaEncryptionHelper.class
 * Java compiler version: 6 (50.0)
 * JD-Core Version:       0.7.1
 */