package org.snmp4j.security;

import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.ShortBufferException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.snmp4j.log.LogAdapter;
import org.snmp4j.log.LogFactory;

/* JADX INFO: loaded from: classes.dex */
public abstract class PrivacyGeneric implements PrivacyProtocol {
    private static final LogAdapter logger = LogFactory.getLogger(PrivacyGeneric.class);
    protected CipherPool cipherPool;
    protected int initVectorLength;
    protected int keyBytes;
    protected String protocolClass;
    protected String protocolId;
    protected Salt salt;

    protected byte[] doDecrypt(byte[] bArr, int i2, int i3, byte[] bArr2, byte[] bArr3) {
        byte[] bArrDoFinal = null;
        try {
            Cipher cipherReuseCipher = this.cipherPool.reuseCipher();
            if (cipherReuseCipher == null) {
                cipherReuseCipher = Cipher.getInstance(this.protocolId);
            }
            cipherReuseCipher.init(2, new SecretKeySpec(bArr2, 0, this.keyBytes, this.protocolClass), new IvParameterSpec(bArr3));
            bArrDoFinal = cipherReuseCipher.doFinal(bArr, i2, i3);
            this.cipherPool.offerCipher(cipherReuseCipher);
            return bArrDoFinal;
        } catch (Exception e2) {
            logger.error(e2);
            if (!logger.isDebugEnabled()) {
                return bArrDoFinal;
            }
            e2.printStackTrace();
            return bArrDoFinal;
        }
    }

    protected byte[] doFinal(byte[] bArr, int i2, int i3, Cipher cipher) throws BadPaddingException, IllegalBlockSizeException, ShortBufferException {
        return cipher.doFinal(bArr, i2, i3);
    }

    protected byte[] doFinalWithPadding(byte[] bArr, int i2, int i3, Cipher cipher) throws BadPaddingException, IllegalBlockSizeException, ShortBufferException {
        int i4 = i3 % 8;
        if (i4 == 0) {
            return cipher.doFinal(bArr, i2, i3);
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Using padding.");
        }
        byte[] bArr2 = new byte[((i3 / 8) + 1) * 8];
        cipher.doFinal(new byte[8], 0, 8 - i4, bArr2, cipher.update(bArr, i2, i3, bArr2));
        return bArr2;
    }

    protected Cipher doInit(byte[] bArr, byte[] bArr2) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, InvalidAlgorithmParameterException {
        Cipher cipherReuseCipher = this.cipherPool.reuseCipher();
        if (cipherReuseCipher == null) {
            cipherReuseCipher = Cipher.getInstance(this.protocolId);
        }
        cipherReuseCipher.init(1, new SecretKeySpec(bArr, 0, this.keyBytes, this.protocolClass), new IvParameterSpec(bArr2));
        return cipherReuseCipher;
    }

    @Override // org.snmp4j.security.SecurityProtocol
    public boolean isSupported() {
        try {
            Cipher cipherReuseCipher = this.cipherPool.reuseCipher();
            if (cipherReuseCipher == null) {
                cipherReuseCipher = Cipher.getInstance(this.protocolId);
            }
            cipherReuseCipher.init(1, new SecretKeySpec(new byte[this.keyBytes], 0, this.keyBytes, this.protocolClass), new IvParameterSpec(new byte[this.initVectorLength]));
            return true;
        } catch (InvalidAlgorithmParameterException e2) {
            if (logger.isDebugEnabled()) {
                logger.debug(this.protocolClass + " privacy not available due to invalid parameter: " + e2.getMessage());
            }
            return false;
        } catch (InvalidKeyException unused) {
            if (logger.isDebugEnabled()) {
                logger.debug(this.protocolClass + " privacy with key length " + this.keyBytes + " not supported");
            }
            return false;
        } catch (NoSuchAlgorithmException unused2) {
            if (logger.isDebugEnabled()) {
                logger.debug(this.protocolClass + " privacy not available");
            }
            return false;
        } catch (NoSuchPaddingException unused3) {
            if (logger.isDebugEnabled()) {
                logger.debug(this.protocolClass + " privacy not available without padding");
            }
            return false;
        }
    }
}
