Example usage for java.security GeneralSecurityException GeneralSecurityException

List of usage examples for java.security GeneralSecurityException GeneralSecurityException

Introduction

In this page you can find the example usage for java.security GeneralSecurityException GeneralSecurityException.

Prototype

public GeneralSecurityException(String message, Throwable cause) 

Source Link

Document

Creates a GeneralSecurityException with the specified detail message and cause.

Usage

From source file:de.openflorian.crypt.CipherKeyGenerator.java

/**
 * Generate Cipher Secret<br/>// ww w  .ja  v a  2s .  co m
 * <br/>
 * Secret is generated by Blowfish {@link KeyGenerator} and a system default
 * {@link SecureRandom} provider and Base64 encoded afterwards.
 * 
 * @return Base64 encoded {@link SecureRandom} generated encryption key
 * @throws GeneralSecurityException
 */
public static String generateKey() throws GeneralSecurityException {
    try {
        KeyGenerator gen = KeyGenerator.getInstance("Blowfish");
        gen.init(192, new SecureRandom());
        SecretKey key = gen.generateKey();

        return new String(new Base64().encode(key.getEncoded())).trim();
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        throw new GeneralSecurityException(e.getMessage(), e);
    }
}

From source file:de.openflorian.crypt.provider.BlowfishCipher.java

@Override
public String decrypt(String str) throws GeneralSecurityException {
    if (key == null || key.isEmpty())
        throw new IllegalStateException("The key is not set or is length=0.");

    if (str == null)
        return null;

    try {/*from   ww w. jav a2s . c o m*/
        SecretKeySpec keySpec;
        keySpec = new SecretKeySpec(key.getBytes("UTF8"), "Blowfish");

        Cipher cipher = Cipher.getInstance("Blowfish/ECB/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, keySpec);

        return new String(cipher.doFinal(Base64.decodeBase64(str.getBytes("UTF8"))), "UTF8");
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        throw new GeneralSecurityException(e.getMessage(), e);
    }
}

From source file:de.openflorian.crypt.provider.BlowfishCipher.java

@Override
public String encrypt(String str) throws GeneralSecurityException {
    if (key == null || key.isEmpty())
        throw new IllegalStateException("The Blowfish Secret is not set or is length=0.");

    if (str == null)
        return null;

    try {/*from   w  ww .  j a v  a 2s  . c om*/
        SecretKeySpec keySpec;
        keySpec = new SecretKeySpec(key.getBytes("UTF8"), "Blowfish");

        Cipher cipher = Cipher.getInstance("Blowfish/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, keySpec);

        return new String(new Base64().encode(cipher.doFinal(str.getBytes("UTF8")))).trim();
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        throw new GeneralSecurityException(e.getMessage(), e);
    }
}

From source file:org.glite.slcs.pki.bouncycastle.X509PrincipalUtil.java

/**
 * Creates a {@link X509Principal} with the given name.
 * <p>//from   w  w w .  j a  v  a 2 s.  c om
 * In the <code>name</code> the RDNs, like <code>CN=B+CN=A</code>, will
 * be sorted alphabetically. Literal characters like <code>+</code>,
 * <code>=</code> must be escaped.
 * 
 * @param name
 *            The {@link X509Principal} name.
 * @return the {@link X509Principal}.
 * @throws GeneralSecurityException
 *             if an error occurs.
 */
public X509Principal createX509Principal(String name) throws GeneralSecurityException {
    Vector<DERObjectIdentifier> oids = new Vector<DERObjectIdentifier>();
    Vector<Object> values = new Vector<Object>();
    Vector<Boolean> added = new Vector<Boolean>();
    start_ = false;
    try {
        LdapName ldapName = new LdapName(name);
        LOG.debug("RDNs: " + ldapName.getRdns());
        Rdn[] rdnArray = new Rdn[ldapName.getRdns().size()];
        ldapName.getRdns().toArray(rdnArray);
        for (int i = rdnArray.length - 1; i >= 0; i--) {
            readRdn(rdnArray[i], oids, values, added);
            start_ = false;
        }
        X509Principal principal = buildX509Principal(oids, values, added);
        return principal;
    } catch (Exception e) {
        // NamingException or IOException
        LOG.error("Fail to create X509Principal(" + name + ")", e);
        throw new GeneralSecurityException("Fail to create X509Principal(" + name + "): " + e.getMessage(), e);
    }
}

From source file:net.shibboleth.idp.cas.proxy.impl.HttpClientProxyAuthenticator.java

@Override
protected int authenticateProxyCallback(@Nonnull final URI callbackUri,
        @Nullable final TrustEngine<? super X509Credential> x509TrustEngine) throws GeneralSecurityException {

    CloseableHttpClient httpClient = null;
    CloseableHttpResponse response = null;
    try {/*from ww  w. j  a  va 2s. c om*/
        httpClient = createHttpClient(x509TrustEngine);
        log.debug("Attempting to connect to {}", callbackUri);
        final HttpGet request = new HttpGet(callbackUri);
        request.setConfig(RequestConfig.custom().setConnectTimeout(timeout).setSocketTimeout(timeout).build());
        response = httpClient.execute(request);
        return response.getStatusLine().getStatusCode();
    } catch (ClientProtocolException e) {
        throw new GeneralSecurityException("HTTP protocol error", e);
    } catch (SSLException e) {
        if (e.getCause() instanceof CertificateException) {
            throw (CertificateException) e.getCause();
        }
        throw new GeneralSecurityException("SSL connection error", e);
    } catch (IOException e) {
        throw new GeneralSecurityException("IO error", e);
    } finally {
        close(response);
        close(httpClient);
    }
}

From source file:net.shibboleth.idp.cas.authn.PkixProxyAuthenticator.java

@Override
protected int authenticateProxyCallback(final URI callbackUri) throws GeneralSecurityException {
    CloseableHttpClient httpClient = null;
    CloseableHttpResponse response = null;
    try {//  ww  w  .ja  v  a2s.  c o m
        httpClient = createHttpClient();
        log.debug("Attempting to connect to {}", callbackUri);
        final HttpGet request = new HttpGet(callbackUri);
        request.setConfig(RequestConfig.custom().setConnectTimeout(timeout).setSocketTimeout(timeout).build());
        response = httpClient.execute(request);
        return response.getStatusLine().getStatusCode();
    } catch (ClientProtocolException e) {
        throw new RuntimeException("HTTP protocol error", e);
    } catch (SSLException e) {
        if (e.getCause() instanceof CertificateException) {
            throw (CertificateException) e.getCause();
        }
        throw new GeneralSecurityException("SSL connection error", e);
    } catch (IOException e) {
        throw new RuntimeException("IO error", e);
    } finally {
        close(response);
        close(httpClient);
    }
}

From source file:edu.uiuc.ncsa.myproxy.MyProxyLogon.java

protected void handleException(Throwable t, String msg) throws IOException, GeneralSecurityException {
    if (t instanceof SSLHandshakeException) {
        throw new GeneralException("Error connecting to server:" + t.getMessage(), t);
    }//  w  ww.  ja v  a 2  s  .  c  o  m
    if (t instanceof IOException) {
        throw (IOException) t;
    }
    if (t instanceof GeneralSecurityException) {
        throw (GeneralSecurityException) t;
    }

    throw new GeneralSecurityException("Error: " + msg, t);
}

From source file:net.java.sip.communicator.impl.certificate.CertificateServiceImpl.java

public SSLContext getSSLContext(X509TrustManager trustManager) throws GeneralSecurityException {
    try {/*from   w  ww.j a  va  2  s.  c  om*/
        KeyStore ks = KeyStore
                .getInstance(System.getProperty("javax.net.ssl.keyStoreType", KeyStore.getDefaultType()));
        KeyManagerFactory kmFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());

        String keyStorePassword = System.getProperty("javax.net.ssl.keyStorePassword");
        if (System.getProperty("javax.net.ssl.keyStore") != null) {
            ks.load(new FileInputStream(System.getProperty("javax.net.ssl.keyStore")), null);
        } else {
            ks.load(null, null);
        }

        kmFactory.init(ks, keyStorePassword == null ? null : keyStorePassword.toCharArray());
        return getSSLContext(kmFactory.getKeyManagers(), trustManager);
    } catch (Exception e) {
        throw new GeneralSecurityException("Cannot init SSLContext", e);
    }
}

From source file:net.java.sip.communicator.impl.certificate.CertificateServiceImpl.java

public SSLContext getSSLContext(KeyManager[] keyManagers, X509TrustManager trustManager)
        throws GeneralSecurityException {
    try {//ww w.j  a  va2  s .  c o  m
        SSLContext sslContext = SSLContext.getInstance("TLS");
        sslContext.init(keyManagers, new TrustManager[] { trustManager }, null);

        return sslContext;
    } catch (Exception e) {
        throw new GeneralSecurityException("Cannot init SSLContext", e);
    }
}