package com.epson.mobilephone.common.keyenc;

import android.content.Context;
import android.util.Base64;

import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;

import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;

public class SecureKeyFactory {
    static SecureKeyFactory instance;
    private final String ENCRYPT_ALGORITHM = "AES";
    private final int ENCRYPT_KEY_BYTES = 16;
    private final String ENCRYPT_METHOD = "AES/ECB/PKCS5Padding";
    private final String ENCRYPT_PROVIDER = "AndroidOpenSSL";
    private final Charset charset = Charset.forName("UTF8");
    private byte[] key = null;
    protected Context mAppContext = null;
    String mSecurityProvider = "AndroidOpenSSL";

    private native byte[] navite_getAppKey(Context context);

    private native byte[] navite_getKey(Context context);

    protected SecureKeyFactory(Context context) {
        try {
            System.loadLibrary("keyenclib");
        } catch (Exception e) {
            e.printStackTrace();
        }
        mAppContext = context.getApplicationContext();
        key = navite_getKey(mAppContext);
    }

    public static SecureKeyFactory getInstance(Context context) {
        return getInstance(context, (String) null);
    }

    public static SecureKeyFactory getInstance(Context context, String str) {
        if (instance == null) {
            instance = new SecureKeyFactory(context);
        }
        if (str == null) {
            SecureKeyFactory secureKeyFactory = instance;
            secureKeyFactory.getClass();
            secureKeyFactory.mSecurityProvider = "AndroidOpenSSL";
        } else if (!instance.mSecurityProvider.equals(str)) {
            instance.mSecurityProvider = str;
        }
        return instance;
    }

    private Cipher getCipher() throws NoSuchPaddingException, NoSuchAlgorithmException {
        try {
            return Cipher.getInstance("AES/ECB/PKCS5Padding", mSecurityProvider);
        } catch (NoSuchAlgorithmException | NoSuchProviderException e) {
            e.printStackTrace();
            return Cipher.getInstance("AES/ECB/PKCS5Padding");
        }
    }

    /* JADX WARNING: Code restructure failed: missing block: B:6:0x0018, code lost:
        r2 = android.util.Base64.encodeToString((r2 = encodeAes(r2.getBytes(r1.charset), r1.key)), 10);
     */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public java.lang.String Encode(java.lang.String r2) {
        /*
            r1 = this;
            byte[] r0 = r1.key
            if (r0 != 0) goto L_0x0007
            java.lang.String r2 = ""
            return r2
        L_0x0007:
            java.nio.charset.Charset r0 = r1.charset
            byte[] r2 = r2.getBytes(r0)
            byte[] r0 = r1.key
            byte[] r2 = r1.encodeAes(r2, r0)
            if (r2 != 0) goto L_0x0018
            java.lang.String r2 = ""
            return r2
        L_0x0018:
            r0 = 10
            java.lang.String r2 = android.util.Base64.encodeToString(r2, r0)
            if (r2 != 0) goto L_0x0023
            java.lang.String r2 = ""
            return r2
        L_0x0023:
            return r2
        */
        throw new UnsupportedOperationException("Method not decompiled: com.epson.mobilephone.common.keyenc.SecureKeyFactory.Encode(java.lang.String):java.lang.String");
    }

    public String Decode(String str) {
        byte[] decode;
        byte[] decodeAes;
        if (key == null || (decode = Base64.decode(str, 10)) == null || (decodeAes = decodeAes(decode, key)) == null) {
            return "";
        }
        return new String(decodeAes, charset);
    }

    private byte[] encodeAes(byte[] bArr, byte[] bArr2) {
        if (bArr == null || bArr2 == null) {
            return null;
        }
        try {
            byte[] makeKeyBytes = makeKeyBytes(16, bArr2);
            Cipher cipher = getCipher();
            cipher.init(1, new SecretKeySpec(makeKeyBytes, "AES"));
            return cipher.doFinal(bArr);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    private byte[] makeKeyBytes(int i, byte[] bArr) {
        byte[] bArr2 = new byte[i];
        for (int i2 = 0; i2 < bArr2.length; i2++) {
            if (i2 < bArr.length) {
                bArr2[i2] = bArr[i2];
            } else {
                bArr2[i2] = 0;
            }
        }
        return bArr2;
    }

    private byte[] decodeAes(byte[] bArr, byte[] bArr2) {
        if (bArr == null || bArr2 == null) {
            return null;
        }
        try {
            byte[] makeKeyBytes = makeKeyBytes(16, bArr2);
            Cipher cipher = getCipher();
            cipher.init(2, new SecretKeySpec(makeKeyBytes, "AES"));
            return cipher.doFinal(bArr);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static byte[] getMd5(byte[] bArr) {
        byte[] bArr2 = {0};
        try {
            MessageDigest instance2 = MessageDigest.getInstance("MD5");
            instance2.update(bArr);
            return instance2.digest();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return bArr2;
        }
    }
}
