package com.ss.android.ugc.effectmanager.common.encrypt;

import X.7xU;
import X.9lJ;
import android.util.Base64;
import com.bytedance.covode.number.Covode;
import com.bytedance.frameworks.apm.trace.MethodCollector;
import com.ss.android.ugc.effectmanager.common.logger.EPLog;
import com.ss.android.ugc.effectmanager.common.utils.CollectionUtil;
import com.ss.android.ugc.effectmanager.common.utils.EPUtils;
import com.ss.android.ugc.effectmanager.common.utils.MD5Utils;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import kotlin.h.b.n;
import kotlin.o.y;

public final class AES {
    public static final AES INSTANCE = new AES();
    public static boolean hasInitialized;
    public static IvParameterSpec ivParameterSpec;
    public static SecretKeySpec secretKeySpec;

    static {
        Covode.recordClassIndex(125272);
    }

    private final synchronized boolean checkIfInitialized() {
        boolean z;
        byte[] generateSecretKey;
        MethodCollector.i(10469);
        if (!hasInitialized && (generateSecretKey = generateSecretKey()) != null) {
            AES aes = INSTANCE;
            secretKeySpec = aes.initSecretKeySpec(generateSecretKey);
            ivParameterSpec = aes.initIvParameterSpec(generateSecretKey);
            hasInitialized = true;
        }
        z = hasInitialized;
        MethodCollector.o(10469);
        return z;
    }

    private final byte[] generateSecretKey() {
        String platformSDKVersion = EPUtils.getPlatformSDKVersion();
        if (platformSDKVersion != null) {
            try {
                String mD5String16Bit = MD5Utils.getMD5String16Bit(platformSDKVersion + ":android");
                n.LIZ(mD5String16Bit, "");
                Charset forName = Charset.forName("UTF-8");
                n.LIZ(forName, "");
                if (mD5String16Bit != null) {
                    byte[] bytes = mD5String16Bit.getBytes(forName);
                    n.LIZ(bytes, "");
                    return bytes;
                }
                throw new 7xU("null cannot be cast to non-null type java.lang.String");
            } catch (Exception e) {
                EPLog.e("AES_TAG", "generateSecretKey error, " + e.getMessage());
            }
        }
        return null;
    }

    private final IvParameterSpec initIvParameterSpec(byte[] bArr) {
        return new IvParameterSpec(bArr);
    }

    private final SecretKeySpec initSecretKeySpec(byte[] bArr) {
        return new SecretKeySpec(bArr, "AES");
    }

    public static final String decrypt(String str) {
        AES aes = INSTANCE;
        if (!aes.checkIfInitialized() || str == null || y.LIZ(str)) {
            return "";
        }
        return aes.decryptInternal(str);
    }

    public static final List<String> decryptArray(List<String> list) {
        if (!INSTANCE.checkIfInitialized() || CollectionUtil.isListEmpty(list)) {
            return new ArrayList();
        }
        ArrayList arrayList = new ArrayList();
        if (list == null) {
            n.LIZ();
        }
        for (String decrypt : list) {
            String decrypt2 = decrypt(decrypt);
            if (decrypt2 != null) {
                arrayList.add(decrypt2);
            }
        }
        return arrayList;
    }

    private final String decryptInternal(String str) {
        try {
            Cipher instance = Cipher.getInstance("AES/CBC/PKCS5Padding");
            instance.init(2, secretKeySpec, ivParameterSpec);
            byte[] doFinal = instance.doFinal(Base64.decode(str, 0));
            n.LIZ(doFinal, "");
            return new String(doFinal, 9lJ.LIZ);
        } catch (Exception e) {
            EPLog.d("AES_TAG", "Error while decrypting " + str + ", exception: " + e);
            return "";
        }
    }

    public static final String encrypt(String str) {
        boolean z;
        if (INSTANCE.checkIfInitialized()) {
            if (str == null || y.LIZ(str)) {
                z = true;
            } else {
                z = false;
            }
            if (!z) {
                try {
                    Cipher instance = Cipher.getInstance("AES/CBC/PKCS5Padding");
                    instance.init(1, secretKeySpec, ivParameterSpec);
                    Charset forName = Charset.forName("UTF-8");
                    n.LIZ(forName, "");
                    if (str != null) {
                        byte[] bytes = str.getBytes(forName);
                        n.LIZ(bytes, "");
                        return Base64.encodeToString(instance.doFinal(bytes), 0);
                    }
                    throw new 7xU("null cannot be cast to non-null type java.lang.String");
                } catch (Exception e) {
                    System.out.println("Error while encrypting: ".concat(String.valueOf(e)));
                }
            }
        }
        return "";
    }
}
