yakivmospan / scytale

One tool to manage key generation, key storing and encryption on different APIs of Android.
432 stars 45 forks source link

Uncaught exception thrown by finalizer #13

Open VictorUrielP opened 6 years ago

VictorUrielP commented 6 years ago

Hi. I am getting this exception and can't reallize where it is comming from.

07-06 12:56:28.426 22855-22918/com.tecnocen.beneficiosandroid.ap E/System: Uncaught exception thrown by finalizer 07-06 12:56:28.445 22855-22918/com.tecnocen.beneficiosandroid.ap E/System: java.lang.IllegalStateException: Binder has been finalized! at android.os.BinderProxy.transactNative(Native Method) at android.os.BinderProxy.transact(Binder.java:628) at android.security.IKeystoreService$Stub$Proxy.abort(IKeystoreService.java:1411) at android.security.KeyStore.abort(KeyStore.java:885) at android.security.keystore.AndroidKeyStoreCipherSpiBase.finalize(AndroidKeyStoreCipherSpiBase.java:744) at android.security.keystore.AndroidKeyStoreRSACipherSpi$PKCS1Padding.finalize(AndroidKeyStoreRSACipherSpi.java) at java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:222) at java.lang.Daemons$FinalizerDaemon.run(Daemons.java:209) at java.lang.Thread.run(Thread.java:762)

This is my class, and I am providing it as a Singleton with Dagger 2:

public class KeyStoreHelperImpl implements KeyStoreHelper {

    private static final String STORE_NAME = "STORE_NAME";

    private Crypto crypto;
    private KeyPair keyPair;

    @Inject
    public KeyStoreHelperImpl(@ApplicationContext Context context) {

        // Create and save key
        Store store = new Store(context, STORE_NAME, BASE_CYPHER_PASSWORD.toCharArray());
        final int keysize = 512;

        if(!store.hasKey(STORE_NAME)) {
            // Create store with specific name and password
            final String alias = STORE_NAME;

            final Calendar start = Calendar.getInstance();
            final Calendar end = Calendar.getInstance();
            end.add(Calendar.YEAR, 1);

            // Create a key store params, some of them are specific per platform
            // Check KeyProps doc for more info
            KeyProps keyProps = new KeyProps.Builder()
                    .setAlias(alias)
                    .setPassword(BASE_CYPHER_PASSWORD.toCharArray())
                    .setKeySize(keysize)
                    .setKeyType("RSA")
                    .setSerialNumber(BigInteger.ONE)
                    .setSubject(new X500Principal("CN=" + alias + " CA Certificate"))
                    .setStartDate(start.getTime())
                    .setEndDate(end.getTime())
                    .setBlockModes("ECB")
                    .setEncryptionPaddings("PKCS1Padding")
                    .setSignatureAlgorithm("SHA256WithRSAEncryption")
                    .build();

            // Generate KeyPair depending on KeyProps
            keyPair = store.generateAsymmetricKey(keyProps);

        } else {
            keyPair =  store.getAsymmetricKey(STORE_NAME, BASE_CYPHER_PASSWORD.toCharArray());
        }

        // Encrypt/Dencrypt data using buffer with or without Initialisation Vectors
        // This additional level of safety is required on 23 API level for
        // some algorithms. Specify encryption/decryption block size to use buffer for
        // large data when using block based algorithms (such as RSA)

        final int encryptionBlockSize = keysize / 8 - 11; // as specified for RSA/ECB/PKCS1Padding keys
        final int decryptionBlockSize = keysize / 8; // as specified for RSA/ECB/PKCS1Padding keys

        crypto = new Crypto("RSA/ECB/PKCS1Padding", encryptionBlockSize, decryptionBlockSize);
    }

    @Override
    public String encryptString(String string) {
        if(string != null)
        return crypto.encrypt(string, keyPair.getPublic(), false);
        return null;
    }

    @Override
    public String decryptString(String string) {
        if(string != null)
        return crypto.decrypt(string, keyPair.getPrivate(), false);
        return null;
    }
}`