Java Code Examples for java.security.GeneralSecurityException#getCause()

The following examples show how to use java.security.GeneralSecurityException#getCause() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PrivateKeyLoader.java    From factura-electronica with Apache License 2.0 6 votes vote down vote up
private byte[] extractProtectedPrivateKey(InputStream privateKeyInputStream, String keyPassword) {
    byte[] bytes;

    try {
        if(keyPassword == null) {
            bytes = ByteStreams.toByteArray(privateKeyInputStream);
        } else {
            bytes = new PKCS8Key(privateKeyInputStream, keyPassword.toCharArray()).getDecryptedBytes();
        }
    } catch (GeneralSecurityException e) {
        throw new KeyException("La contraseña del certificado no es correcta", e.getCause());
    } catch (IOException ioe){
        throw new KeyException(ioe.getMessage(), ioe.getCause());
    }

    return bytes;
}
 
Example 2
Source File: CryptoRandomFactoryTest.java    From commons-crypto with Apache License 2.0 6 votes vote down vote up
@Test
public void testFailingRandom() {
    final Properties props = new Properties();
    props.setProperty(CryptoRandomFactory.CLASSES_KEY, FailingRandom.class.getName());
    try {
        CryptoRandomFactory.getCryptoRandom(props);
        Assert.fail("Expected GeneralSecurityException");
    } catch (final GeneralSecurityException e) {
        Throwable cause = e.getCause();
        Assert.assertEquals(RuntimeException.class, cause.getClass());
        cause = cause.getCause();
        Assert.assertEquals(InvocationTargetException.class, cause.getClass());
        cause = cause.getCause();
        Assert.assertEquals(UnsatisfiedLinkError.class, cause.getClass());
    }
}
 
Example 3
Source File: OsCryptoRandomTest.java    From commons-crypto with Apache License 2.0 6 votes vote down vote up
@Test
public void testInvalidRandom() {
    final Properties props = new Properties();
    props.setProperty(CryptoRandomFactory.CLASSES_KEY, OsCryptoRandom.class.getName());
    // Invalid device
    props.setProperty(CryptoRandomFactory.DEVICE_FILE_PATH_KEY, "");
    try {
        CryptoRandomFactory.getCryptoRandom(props);
        fail("Expected GeneralSecurityException");
    } catch (final GeneralSecurityException e) {
        Throwable cause;
        cause = e.getCause();
        Assert.assertEquals(RuntimeException.class, cause.getClass());
        cause = cause.getCause();
        Assert.assertEquals(InvocationTargetException.class, cause.getClass());
        cause = cause.getCause();
        Assert.assertEquals(RuntimeException.class, cause.getClass());
        cause = cause.getCause();
        Assert.assertEquals(FileNotFoundException.class, cause.getClass());
    }
}
 
Example 4
Source File: PrivateKeyLoader.java    From factura-electronica with Apache License 2.0 5 votes vote down vote up
/**
 *
 * @param privateKeyInputStream private key's input stream
 * @param keyPassword           private key password
 *
 * @throws KeyException thrown when any security exception occurs
 */
public void setPrivateKey(InputStream privateKeyInputStream, String keyPassword) {

    byte[] privateKeyByte = this.extractProtectedPrivateKey(privateKeyInputStream, keyPassword);
    PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(privateKeyByte);

    try {
        this.key = KeyFactory.getInstance("RSA").generatePrivate(pkcs8EncodedKeySpec);
    }catch (GeneralSecurityException gse) {
        throw new KeyException(
                "Error al obtener la información del certificado debido a su codificación",
                gse.getCause());
    }
}
 
Example 5
Source File: BaseEncryptionManager.java    From samples-android with Apache License 2.0 5 votes vote down vote up
@Override
public void recreateCipher() {
    try {
        mCipher = createCipher(mTransformationString);
    } catch (GeneralSecurityException e) {
        throw new RuntimeException("Failed init Cipher", e.getCause());
    }
    resetTimer();
}