package com.it_nomads.fluttersecurestorage.ciphers;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Base64;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.KeyStore;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.cert.Certificate;
import java.security.spec.AlgorithmParameterSpec;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

/* JADX INFO: loaded from: classes.dex */
public class d {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final Cipher f24552a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final SecureRandom f24553b = new SecureRandom();

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final Key f24554c;

    public d(Context context, b bVar) throws Exception {
        String strB = b();
        SharedPreferences sharedPreferences = context.getSharedPreferences("FlutterSecureKeyStorage", 0);
        SharedPreferences.Editor editorEdit = sharedPreferences.edit();
        String string = sharedPreferences.getString(strB, null);
        this.f24552a = c();
        if (string != null) {
            try {
                this.f24554c = bVar.f(Base64.decode(string, 0));
                return;
            } catch (Exception unused) {
            }
        }
        byte[] bArr = new byte[16];
        this.f24553b.nextBytes(bArr);
        SecretKeySpec secretKeySpec = new SecretKeySpec(bArr, "AES");
        this.f24554c = secretKeySpec;
        KeyStore keyStore = KeyStore.getInstance("AndroidKeyStore");
        keyStore.load(null);
        String str = bVar.f24550a;
        Certificate certificate = keyStore.getCertificate(str);
        if (certificate == null) {
            throw new Exception(defpackage.e.i("No certificate found under alias: ", str));
        }
        PublicKey publicKey = certificate.getPublicKey();
        if (publicKey == null) {
            throw new Exception(defpackage.e.i("No key found under alias: ", str));
        }
        Cipher cipherC = bVar.c();
        cipherC.init(3, publicKey, bVar.b());
        editorEdit.putString(strB, Base64.encodeToString(cipherC.wrap(secretKeySpec), 0));
        editorEdit.apply();
    }

    public final byte[] a(byte[] bArr) throws BadPaddingException, IllegalBlockSizeException, InvalidKeyException, InvalidAlgorithmParameterException {
        int iD = d();
        byte[] bArr2 = new byte[iD];
        this.f24553b.nextBytes(bArr2);
        AlgorithmParameterSpec algorithmParameterSpecE = e(bArr2);
        Key key = this.f24554c;
        Cipher cipher = this.f24552a;
        cipher.init(1, key, algorithmParameterSpecE);
        byte[] bArrDoFinal = cipher.doFinal(bArr);
        byte[] bArr3 = new byte[bArrDoFinal.length + iD];
        System.arraycopy(bArr2, 0, bArr3, 0, iD);
        System.arraycopy(bArrDoFinal, 0, bArr3, iD, bArrDoFinal.length);
        return bArr3;
    }

    public String b() {
        return "VGhpcyBpcyB0aGUga2V5IGZvciBhIHNlY3VyZSBzdG9yYWdlIEFFUyBLZXkK";
    }

    public Cipher c() {
        return Cipher.getInstance("AES/CBC/PKCS7Padding");
    }

    public int d() {
        return 16;
    }

    public AlgorithmParameterSpec e(byte[] bArr) {
        return new IvParameterSpec(bArr);
    }
}
