// Path: app/src/main/java/ca/alexland/renewpass/exceptions/DecryptionFailedException.java
// public class DecryptionFailedException extends Exception {
//     Exception originalException;
// 
//     public DecryptionFailedException(Exception originalException) {
//         this.originalException = originalException;
//     }
// 
//     public Exception getOriginalException() {
//         return originalException;
//     }
// }
// 
// Path: app/src/main/java/ca/alexland/renewpass/exceptions/EncryptionFailedException.java
// public class EncryptionFailedException extends Exception {
//     Exception originalException;
// 
//     public EncryptionFailedException(Exception originalException) {
//         this.originalException = originalException;
//     }
// 
//     public Exception getOriginalException() {
//         return originalException;
//     }
// }


import android.content.Context;
import android.security.KeyPairGeneratorSpec;
import android.util.Base64;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.security.KeyPairGenerator;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.util.ArrayList;
import java.util.Calendar;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.security.auth.x500.X500Principal;
import ca.alexland.renewpass.exceptions.DecryptionFailedException;
import ca.alexland.renewpass.exceptions.EncryptionFailedException;

            CipherInputStream cipherInputStream = new CipherInputStream(
                    new ByteArrayInputStream(Base64.decode(encryptedPassword, Base64.DEFAULT)), output);
            ArrayList<Byte> values = new ArrayList<>();
            int nextByte;
            while ((nextByte = cipherInputStream.read()) != -1) {
                values.add((byte)nextByte);
            }

            byte[] bytes = new byte[values.size()];
            for(int i = 0; i < bytes.length; i++) {
                bytes[i] = values.get(i);
            }

            return new String(bytes, 0, bytes.length, "UTF-8");

        } catch (Exception e) {
            throw new DecryptionFailedException(e);
        }
    }

    public void setAlias(String alias) {
        mAlias = alias;
    }

    public boolean keysExist() {
        KeyStore.Entry entry = null;
        try {
            KeyStore keyStore = KeyStore.getInstance(SecurityConstants.KEYSTORE_PROVIDER_ANDROID_KEYSTORE);
            keyStore.load(null);
// Path: app/src/main/java/ca/alexland/renewpass/exceptions/DecryptionFailedException.java
// public class DecryptionFailedException extends Exception {
//     Exception originalException;
// 
//     public DecryptionFailedException(Exception originalException) {
//         this.originalException = originalException;
//     }
// 
//     public Exception getOriginalException() {
//         return originalException;
//     }
// }
// 
// Path: app/src/main/java/ca/alexland/renewpass/exceptions/EncryptionFailedException.java
// public class EncryptionFailedException extends Exception {
//     Exception originalException;
// 
//     public EncryptionFailedException(Exception originalException) {
//         this.originalException = originalException;
//     }
// 
//     public Exception getOriginalException() {
//         return originalException;
//     }
// }


// Path: app/src/main/java/ca/alexland/renewpass/utils/KeyStoreUtil.java
import android.content.Context;
import android.security.KeyPairGeneratorSpec;
import android.util.Base64;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.security.KeyPairGenerator;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.util.ArrayList;
import java.util.Calendar;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.security.auth.x500.X500Principal;
import ca.alexland.renewpass.exceptions.DecryptionFailedException;
import ca.alexland.renewpass.exceptions.EncryptionFailedException;


            CipherInputStream cipherInputStream = new CipherInputStream(
                    new ByteArrayInputStream(Base64.decode(encryptedPassword, Base64.DEFAULT)), output);
            ArrayList<Byte> values = new ArrayList<>();
            int nextByte;
            while ((nextByte = cipherInputStream.read()) != -1) {
                values.add((byte)nextByte);
            }

            byte[] bytes = new byte[values.size()];
            for(int i = 0; i < bytes.length; i++) {
                bytes[i] = values.get(i);
            }

            return new String(bytes, 0, bytes.length, "UTF-8");

        } catch (Exception e) {
            throw new DecryptionFailedException(e);
        }
    }

    public void setAlias(String alias) {
        mAlias = alias;
    }

    public boolean keysExist() {
        KeyStore.Entry entry = null;
        try {
            KeyStore keyStore = KeyStore.getInstance(SecurityConstants.KEYSTORE_PROVIDER_ANDROID_KEYSTORE);
            keyStore.load(null);
            entry = keyStore.getEntry(mAlias, null);