// 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;
        else {
            return false;
        }

    }

    public String encryptPassword(String plainTextPassword) throws EncryptionFailedException {
        try {
            KeyStore keyStore = KeyStore.getInstance(SecurityConstants.KEYSTORE_PROVIDER_ANDROID_KEYSTORE);
            keyStore.load(null);

            KeyStore.Entry entry = keyStore.getEntry(mAlias, null);

            KeyStore.PrivateKeyEntry privateKeyEntry = (KeyStore.PrivateKeyEntry)entry;
            RSAPublicKey publicKey = (RSAPublicKey) privateKeyEntry.getCertificate().getPublicKey();

            Cipher input;
            input = Cipher.getInstance("RSA/ECB/PKCS1Padding");
            input.init(Cipher.ENCRYPT_MODE, publicKey);

            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            CipherOutputStream cipherOutputStream = new CipherOutputStream(
                    outputStream, input);
            cipherOutputStream.write(plainTextPassword.getBytes("UTF-8"));
            cipherOutputStream.close();

            byte[] vals = outputStream.toByteArray();
            return Base64.encodeToString(vals, Base64.DEFAULT);
        }
        catch(Exception e) {
// 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;

        else {
            return false;
        }

    }

    public String encryptPassword(String plainTextPassword) throws EncryptionFailedException {
        try {
            KeyStore keyStore = KeyStore.getInstance(SecurityConstants.KEYSTORE_PROVIDER_ANDROID_KEYSTORE);
            keyStore.load(null);

            KeyStore.Entry entry = keyStore.getEntry(mAlias, null);

            KeyStore.PrivateKeyEntry privateKeyEntry = (KeyStore.PrivateKeyEntry)entry;
            RSAPublicKey publicKey = (RSAPublicKey) privateKeyEntry.getCertificate().getPublicKey();

            Cipher input;
            input = Cipher.getInstance("RSA/ECB/PKCS1Padding");
            input.init(Cipher.ENCRYPT_MODE, publicKey);

            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            CipherOutputStream cipherOutputStream = new CipherOutputStream(
                    outputStream, input);
            cipherOutputStream.write(plainTextPassword.getBytes("UTF-8"));
            cipherOutputStream.close();

            byte[] vals = outputStream.toByteArray();
            return Base64.encodeToString(vals, Base64.DEFAULT);
        }
        catch(Exception e) {
            throw new EncryptionFailedException(e);