// 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;
/*
* Copyright (C) 2013 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package ca.alexland.renewpass.utils;





/**
 * Taken from https://developer.android.com/samples/BasicAndroidKeyStore and
 * https://github.com/obaro/SimpleKeystoreApp with modifications by Alex Land
 */
public class KeyStoreUtil {
    private String mAlias;

    public KeyStoreUtil(String alias) {
        this.mAlias = alias;
    }

    public boolean createKeys(Context context) {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
            // Create a start and end time, for the validity range of the key pair that's about to be
            // generated.
            Calendar start = Calendar.getInstance();
            Calendar end = Calendar.getInstance();
            end.add(Calendar.YEAR, 1);


            // The KeyPairGeneratorSpec object is how parameters for your key pair are passed
            // to the KeyPairGenerator.  For a fun home game, count how many classes in this sample
            // start with the phrase "KeyPair".
            KeyPairGeneratorSpec spec =
// 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;

/*
* Copyright (C) 2013 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package ca.alexland.renewpass.utils;





/**
 * Taken from https://developer.android.com/samples/BasicAndroidKeyStore and
 * https://github.com/obaro/SimpleKeystoreApp with modifications by Alex Land
 */
public class KeyStoreUtil {
    private String mAlias;

    public KeyStoreUtil(String alias) {
        this.mAlias = alias;
    }

    public boolean createKeys(Context context) {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
            // Create a start and end time, for the validity range of the key pair that's about to be
            // generated.
            Calendar start = Calendar.getInstance();
            Calendar end = Calendar.getInstance();
            end.add(Calendar.YEAR, 1);


            // The KeyPairGeneratorSpec object is how parameters for your key pair are passed
            // to the KeyPairGenerator.  For a fun home game, count how many classes in this sample
            // start with the phrase "KeyPair".
            KeyPairGeneratorSpec spec =
                new KeyPairGeneratorSpec.Builder(context)