package com.seewo.libnfc.cipher;

import com.seewo.libnfc.tools.HexTools;
import java.security.GeneralSecurityException;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.SecretKeySpec;

/* JADX INFO: loaded from: classes.dex */
public class AESTool {
    private static final String ALGORITHM = "AES";
    public static final byte[] PROWISE_KEY_HEAD = {-14, -1, -1, -1, -1, -1};
    private static byte[] COMPLEMENT_VALUS = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

    private AESTool() {
    }

    public static String encryptWithRandomKey(String str, String str2) throws GeneralSecurityException {
        return HexTools.toHex(encrypt(getComplementKey(str.getBytes()), str2.getBytes()));
    }

    public static byte[] encryptWithRandomKey(byte[] bArr, String str) throws GeneralSecurityException {
        return encrypt(getComplementKey(bArr), str.getBytes());
    }

    public static String decryptWithRandomKey(String str, String str2) throws GeneralSecurityException {
        return new String(decrypt(getComplementKey(str.getBytes()), HexTools.toByte(str2)));
    }

    public static String decryptWithRandomKey(byte[] bArr, byte[] bArr2) throws GeneralSecurityException {
        return new String(decrypt(getComplementKey(bArr), bArr2));
    }

    private static byte[] encrypt(byte[] bArr, byte[] bArr2) throws GeneralSecurityException {
        SecretKeySpec secretKeySpec = new SecretKeySpec(bArr, ALGORITHM);
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(1, secretKeySpec);
        return cipher.doFinal(bArr2);
    }

    private static byte[] decrypt(byte[] bArr, byte[] bArr2) throws GeneralSecurityException {
        SecretKeySpec secretKeySpec = new SecretKeySpec(bArr, ALGORITHM);
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(2, secretKeySpec);
        return cipher.doFinal(bArr2);
    }

    private static byte[] getRawKey(byte[] bArr) throws GeneralSecurityException {
        KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM);
        SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG", "Crypto");
        secureRandom.setSeed(bArr);
        keyGenerator.init(256, secureRandom);
        return keyGenerator.generateKey().getEncoded();
    }

    private static byte[] getComplementKey(byte[] bArr) {
        byte[] bArr2 = COMPLEMENT_VALUS;
        if (bArr.length > bArr2.length) {
            System.arraycopy(bArr, 0, bArr2, 0, bArr2.length);
        } else {
            System.arraycopy(bArr, 0, bArr2, 0, bArr.length);
        }
        return bArr2;
    }
}
