Search in sources :

Example 1 with BlockCipher

use of de.flexiprovider.api.BlockCipher in project core by jcryptool.

the class BlockCipherEngine method init.

@Override
public KeyObject init(IFlexiProviderOperation operation) {
    // $NON-NLS-1$
    LogUtil.logInfo("initializing block cipher engine");
    this.operation = operation;
    char[] password = new char[4];
    Key key = null;
    if (operation.useCustomKey()) {
        try {
            SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance(operation.getAlgorithmDescriptor().getAlgorithmName(), // $NON-NLS-1$
            "FlexiCore");
            SecretKeySpec keySpec = new SecretKeySpec(operation.getKeyBytes(), operation.getAlgorithmDescriptor().getAlgorithmName());
            key = (Key) secretKeyFactory.generateSecret(keySpec);
        } catch (Exception e) {
            LogUtil.logError(FlexiProviderEnginesPlugin.PLUGIN_ID, "Exception while creating key", e, // $NON-NLS-1$
            true);
            return null;
        }
    } else {
        // password may be contained in the ActionItem, otherwise prompt
        if (operation.getPassword() != null) {
            password = operation.getPassword();
        } else {
            password = promptPassword();
        }
        if (password != null) {
            try {
                key = (Key) KeyStoreManager.getInstance().getSecretKey(operation.getKeyStoreAlias(), password);
                // save in the operation if no exception occurred
                operation.setPassword(password);
            } catch (UnrecoverableEntryException e) {
                JCTMessageDialog.showInfoDialog(new Status(IStatus.INFO, FlexiProviderEnginesPlugin.PLUGIN_ID, Messages.ExAccessKeystorePassword, e));
                return null;
            } catch (Exception e) {
                LogUtil.logError(FlexiProviderEnginesPlugin.PLUGIN_ID, "Exception while accessing a secret key", e, // $NON-NLS-1$
                true);
                return null;
            }
        }
    }
    if (key != null) {
        try {
            String fullCipherName = // Name of algorithm
            operation.getAlgorithmDescriptor().getAlgorithmName() + // $NON-NLS-1$
            "/" + // Name of mode
            ((BlockCipherDescriptor) operation.getAlgorithmDescriptor()).getMode() + // $NON-NLS-1$
            "/" + // Name of padding
            ((BlockCipherDescriptor) operation.getAlgorithmDescriptor()).getPadding();
            cipher = Registry.getBlockCipher(fullCipherName);
            if (operation.getOperation().equals(OperationType.ENCRYPT)) {
                ((BlockCipher) cipher).initEncrypt(key, ((BlockCipherDescriptor) operation.getAlgorithmDescriptor()).getModeParameters(), operation.getAlgorithmDescriptor().getAlgorithmParameterSpec(), FlexiProviderEnginesPlugin.getSecureRandom());
            } else {
                ((BlockCipher) cipher).initDecrypt(key, ((BlockCipherDescriptor) operation.getAlgorithmDescriptor()).getModeParameters(), operation.getAlgorithmDescriptor().getAlgorithmParameterSpec());
            }
            initialized = true;
        } catch (NoSuchAlgorithmException e) {
            LogUtil.logError(FlexiProviderEnginesPlugin.PLUGIN_ID, "NoSuchAlgorithmException while initializing a block cipher engine", e, // $NON-NLS-1$
            true);
            return null;
        } catch (NoSuchPaddingException e) {
            LogUtil.logError(FlexiProviderEnginesPlugin.PLUGIN_ID, "NoSuchPaddingException while initializing a block cipher engine", e, // $NON-NLS-1$
            true);
            return null;
        } catch (InvalidKeyException e) {
            LogUtil.logError(FlexiProviderEnginesPlugin.PLUGIN_ID, Messages.BlockCipherEngine_5, e, true);
            return null;
        } catch (InvalidAlgorithmParameterException e) {
            LogUtil.logError(FlexiProviderEnginesPlugin.PLUGIN_ID, "InvalidAlgorithmParameterException while initializing a block cipher engine", e, // $NON-NLS-1$
            true);
            return null;
        }
    }
    return new KeyObject(key, password);
}
Also used : Status(org.eclipse.core.runtime.Status) IStatus(org.eclipse.core.runtime.IStatus) InvalidAlgorithmParameterException(de.flexiprovider.api.exceptions.InvalidAlgorithmParameterException) BlockCipher(de.flexiprovider.api.BlockCipher) NoSuchPaddingException(de.flexiprovider.api.exceptions.NoSuchPaddingException) NoSuchAlgorithmException(de.flexiprovider.api.exceptions.NoSuchAlgorithmException) InvalidKeyException(de.flexiprovider.api.exceptions.InvalidKeyException) NoSuchAlgorithmException(de.flexiprovider.api.exceptions.NoSuchAlgorithmException) InvalidKeyException(de.flexiprovider.api.exceptions.InvalidKeyException) InvalidAlgorithmParameterException(de.flexiprovider.api.exceptions.InvalidAlgorithmParameterException) NoSuchPaddingException(de.flexiprovider.api.exceptions.NoSuchPaddingException) UnrecoverableEntryException(java.security.UnrecoverableEntryException) SecretKeySpec(de.flexiprovider.api.keys.SecretKeySpec) UnrecoverableEntryException(java.security.UnrecoverableEntryException) SecretKeyFactory(javax.crypto.SecretKeyFactory) Key(de.flexiprovider.api.keys.Key)

Example 2 with BlockCipher

use of de.flexiprovider.api.BlockCipher in project core by jcryptool.

the class BlockCipherWizardDialog method nextPressed.

public void nextPressed() {
    // $NON-NLS-1$
    LogUtil.logInfo("next pressed");
    if (wizard.hasAlgorithmParameterSpecPage() && getCurrentPage().getName().equals("AlgorithmParameterWizardPage")) {
        // $NON-NLS-1$
        Object[] values = wizard.getAlgorithmParameterValues();
        for (Object value : values) {
            // $NON-NLS-1$ //$NON-NLS-2$
            LogUtil.logInfo("Value: " + value + " of type: " + value.getClass().getName());
        }
        try {
            dummyKey = wizard.getDummyKey();
            BlockCipher cipher = Registry.getBlockCipher(algorithm.getName());
            AlgorithmParameterSpec spec = Reflector.getInstance().instantiateParameterSpec(algorithm.getParameterSpecClassName(), values);
            cipher.initEncrypt(dummyKey, null, spec, FlexiProviderAlgorithmsPlugin.getSecureRandom());
            modeBlockSize = cipher.getBlockSize();
            wizard.setModeBlockSize(modeBlockSize);
        } catch (NoSuchAlgorithmException e) {
            // $NON-NLS-1$
            LogUtil.logError(FlexiProviderAlgorithmsPlugin.PLUGIN_ID, "NoSuchAlgorithmException while initializing a block cipher", e, true);
        } catch (NoSuchPaddingException e) {
            // $NON-NLS-1$
            LogUtil.logError(FlexiProviderAlgorithmsPlugin.PLUGIN_ID, "NoSuchPaddingException while initializing a block cipher", e, true);
        } catch (InvalidKeyException e) {
            LogUtil.logError(FlexiProviderAlgorithmsPlugin.PLUGIN_ID, Messages.BlockCipherWizardDialog_2, e, true);
        } catch (InvalidAlgorithmParameterException e) {
            // $NON-NLS-1$
            LogUtil.logError(FlexiProviderAlgorithmsPlugin.PLUGIN_ID, "InvalidAlgorithmParameterException while initializing a block cipher", e, true);
        } catch (SecurityException e) {
            // $NON-NLS-1$
            LogUtil.logError(FlexiProviderAlgorithmsPlugin.PLUGIN_ID, "SecurityException while initializing a block cipher", e, true);
        } catch (IllegalArgumentException e) {
            // $NON-NLS-1$
            LogUtil.logError(FlexiProviderAlgorithmsPlugin.PLUGIN_ID, "IllegalArgumentException while initializing a block cipher", e, true);
        } catch (ClassNotFoundException e) {
            // $NON-NLS-1$
            LogUtil.logError(FlexiProviderAlgorithmsPlugin.PLUGIN_ID, "ClassNotFoundException while initializing a block cipher", e, true);
        } catch (NoSuchMethodException e) {
            // $NON-NLS-1$
            LogUtil.logError(FlexiProviderAlgorithmsPlugin.PLUGIN_ID, "NoSuchMethodException while initializing a block cipher", e, true);
        } catch (InstantiationException e) {
            // $NON-NLS-1$
            LogUtil.logError(FlexiProviderAlgorithmsPlugin.PLUGIN_ID, "InstantiationException while initializing a block cipher", e, true);
        } catch (IllegalAccessException e) {
            // $NON-NLS-1$
            LogUtil.logError(FlexiProviderAlgorithmsPlugin.PLUGIN_ID, "IllegalAccessException while initializing a block cipher", e, true);
        } catch (InvocationTargetException e) {
            // $NON-NLS-1$
            LogUtil.logError(FlexiProviderAlgorithmsPlugin.PLUGIN_ID, "InvocationTargetException while initializing a block cipher", e, true);
        }
    }
    super.nextPressed();
}
Also used : InvalidAlgorithmParameterException(de.flexiprovider.api.exceptions.InvalidAlgorithmParameterException) BlockCipher(de.flexiprovider.api.BlockCipher) NoSuchPaddingException(de.flexiprovider.api.exceptions.NoSuchPaddingException) NoSuchAlgorithmException(de.flexiprovider.api.exceptions.NoSuchAlgorithmException) InvalidKeyException(de.flexiprovider.api.exceptions.InvalidKeyException) InvocationTargetException(java.lang.reflect.InvocationTargetException) AlgorithmParameterSpec(de.flexiprovider.api.parameters.AlgorithmParameterSpec)

Aggregations

BlockCipher (de.flexiprovider.api.BlockCipher)2 InvalidAlgorithmParameterException (de.flexiprovider.api.exceptions.InvalidAlgorithmParameterException)2 InvalidKeyException (de.flexiprovider.api.exceptions.InvalidKeyException)2 NoSuchAlgorithmException (de.flexiprovider.api.exceptions.NoSuchAlgorithmException)2 NoSuchPaddingException (de.flexiprovider.api.exceptions.NoSuchPaddingException)2 Key (de.flexiprovider.api.keys.Key)1 SecretKeySpec (de.flexiprovider.api.keys.SecretKeySpec)1 AlgorithmParameterSpec (de.flexiprovider.api.parameters.AlgorithmParameterSpec)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 UnrecoverableEntryException (java.security.UnrecoverableEntryException)1 SecretKeyFactory (javax.crypto.SecretKeyFactory)1 IStatus (org.eclipse.core.runtime.IStatus)1 Status (org.eclipse.core.runtime.Status)1