Example usage for java.security NoSuchAlgorithmException getMessage

List of usage examples for java.security NoSuchAlgorithmException getMessage

Introduction

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

Prototype

public String getMessage() 

Source Link

Document

Returns the detail message string of this throwable.

Usage

From source file:com.app.mvc.http.ext.AuthSSLProtocolSocketFactory.java

private SSLContext createSSLContext() {
    try {//from www  .j  a  va 2 s .  co  m
        KeyManager[] keymanagers = null;
        TrustManager[] trustmanagers = null;
        if (this.keystoreUrl != null) {
            KeyStore keystore = createKeyStore(this.keystoreUrl, this.keystorePassword);
            if (log.isDebugEnabled()) {
                Enumeration aliases = keystore.aliases();
                while (aliases.hasMoreElements()) {
                    String alias = (String) aliases.nextElement();
                    Certificate[] certs = keystore.getCertificateChain(alias);
                    if (certs != null) {
                        log.debug("Certificate chain '" + alias + "':");
                        for (int c = 0; c < certs.length; c++) {
                            if (certs[c] instanceof X509Certificate) {
                                X509Certificate cert = (X509Certificate) certs[c];
                                log.debug(" Certificate " + (c + 1) + ":");
                                log.debug("  Subject DN: " + cert.getSubjectDN());
                                log.debug("  Signature Algorithm: " + cert.getSigAlgName());
                                log.debug("  Valid from: " + cert.getNotBefore());
                                log.debug("  Valid until: " + cert.getNotAfter());
                                log.debug("  Issuer: " + cert.getIssuerDN());
                            }
                        }
                    }
                }
            }
            keymanagers = createKeyManagers(keystore, this.keystorePassword);
        }
        if (this.truststoreUrl != null) {
            KeyStore keystore = createKeyStore(this.truststoreUrl, this.truststorePassword);
            if (log.isDebugEnabled()) {
                Enumeration aliases = keystore.aliases();
                while (aliases.hasMoreElements()) {
                    String alias = (String) aliases.nextElement();
                    log.debug("Trusted certificate '" + alias + "':");
                    Certificate trustedcert = keystore.getCertificate(alias);
                    if (trustedcert != null && trustedcert instanceof X509Certificate) {
                        X509Certificate cert = (X509Certificate) trustedcert;
                        log.debug("  Subject DN: " + cert.getSubjectDN());
                        log.debug("  Signature Algorithm: " + cert.getSigAlgName());
                        log.debug("  Valid from: " + cert.getNotBefore());
                        log.debug("  Valid until: " + cert.getNotAfter());
                        log.debug("  Issuer: " + cert.getIssuerDN());
                    }
                }
            }
            trustmanagers = createTrustManagers(keystore);
        }
        SSLContext sslcontext = SSLContext.getInstance("SSL");
        sslcontext.init(keymanagers, trustmanagers, null);
        return sslcontext;
    } catch (NoSuchAlgorithmException e) {
        log.error(e.getMessage(), e);
        throw new AuthSSLInitializationError("Unsupported algorithm exception: " + e.getMessage());
    } catch (KeyStoreException e) {
        log.error(e.getMessage(), e);
        throw new AuthSSLInitializationError("Keystore exception: " + e.getMessage());
    } catch (GeneralSecurityException e) {
        log.error(e.getMessage(), e);
        throw new AuthSSLInitializationError("Key management exception: " + e.getMessage());
    } catch (IOException e) {
        log.error(e.getMessage(), e);
        throw new AuthSSLInitializationError("I/O error reading keystore/truststore file: " + e.getMessage());
    }
}

From source file:eu.europa.ec.markt.dss.signature.xades.XAdESProfileC.java

/**
 * Gives back the JAXB DigestAlgAndValue data structure.
 * /*from   ww  w. ja  v  a  2s.c  om*/
 * @param data
 * @param xadesObjectFactory
 * @param xmldsigObjectFactory
 * @param digestAlgorithm
 * @return
 */
private DigestAlgAndValueType getDigestAlgAndValue(byte[] data, DigestAlgorithm digestAlgorithm) {
    DigestAlgAndValueType digestAlgAndValue = xadesObjectFactory.createDigestAlgAndValueType();

    DigestMethodType digestMethod = getXmldsigObjectFactory().createDigestMethodType();
    digestAlgAndValue.setDigestMethod(digestMethod);
    String xmlDigestAlgorithm = digestAlgorithm.getXmlId();
    digestMethod.setAlgorithm(xmlDigestAlgorithm);

    MessageDigest messageDigest;
    try {
        messageDigest = MessageDigest.getInstance(digestAlgorithm.getName());
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException("message digest algo error: " + e.getMessage(), e);
    }
    byte[] digestValue = messageDigest.digest(data);
    digestAlgAndValue.setDigestValue(digestValue);

    return digestAlgAndValue;
}

From source file:org.miloss.fgsms.bueller.AuthSSLProtocolSocketFactory.java

private SSLContext createSSLContext() {
    try {//from  w  w  w.  j  av a  2  s .  com
        KeyManager[] keymanagers = null;
        TrustManager[] trustmanagers = null;
        if (this.keystoreUrl != null) {
            KeyStore keystore = createKeyStore(this.keystoreUrl, this.keystorePassword);
            if (LOG.isDebugEnabled()) {
                Enumeration aliases = keystore.aliases();
                while (aliases.hasMoreElements()) {
                    String alias = (String) aliases.nextElement();
                    Certificate[] certs = keystore.getCertificateChain(alias);
                    if (certs != null) {
                        LOG.debug("Certificate chain '" + alias + "':");
                        for (int c = 0; c < certs.length; c++) {
                            if (certs[c] instanceof X509Certificate) {
                                X509Certificate cert = (X509Certificate) certs[c];
                                LOG.debug(" Certificate " + (c + 1) + ":");
                                LOG.debug("  Subject DN: " + cert.getSubjectDN());
                                LOG.debug("  Signature Algorithm: " + cert.getSigAlgName());
                                LOG.debug("  Valid from: " + cert.getNotBefore());
                                LOG.debug("  Valid until: " + cert.getNotAfter());
                                LOG.debug("  Issuer: " + cert.getIssuerDN());
                            }
                        }
                    }
                }
            }
            keymanagers = createKeyManagers(keystore, this.keystorePassword);
        }
        if (this.truststoreUrl != null) {
            KeyStore keystore = createKeyStore(this.truststoreUrl, this.truststorePassword);
            if (LOG.isDebugEnabled()) {
                Enumeration aliases = keystore.aliases();
                while (aliases.hasMoreElements()) {
                    String alias = (String) aliases.nextElement();
                    LOG.debug("Trusted certificate '" + alias + "':");
                    Certificate trustedcert = keystore.getCertificate(alias);
                    if (trustedcert != null && trustedcert instanceof X509Certificate) {
                        X509Certificate cert = (X509Certificate) trustedcert;
                        LOG.debug("  Subject DN: " + cert.getSubjectDN());
                        LOG.debug("  Signature Algorithm: " + cert.getSigAlgName());
                        LOG.debug("  Valid from: " + cert.getNotBefore());
                        LOG.debug("  Valid until: " + cert.getNotAfter());
                        LOG.debug("  Issuer: " + cert.getIssuerDN());
                    }
                }
            }
            trustmanagers = createTrustManagers(keystore);
        }
        SSLContext sslcontext = SSLContext.getInstance("SSL");
        sslcontext.init(keymanagers, trustmanagers, null);
        return sslcontext;
    } catch (NoSuchAlgorithmException e) {
        LOG.error(e.getMessage(), e);
        // throw new AuthSSLInitializationError("Unsupported algorithm exception: " + e.getMessage());
    } catch (KeyStoreException e) {
        LOG.error(e.getMessage(), e);
        //  throw new AuthSSLInitializationError("Keystore exception: " + e.getMessage());
    } catch (GeneralSecurityException e) {
        LOG.error(e.getMessage(), e);
        // throw new AuthSSLInitializationError("Key management exception: " + e.getMessage());
    } catch (IOException e) {
        LOG.error(e.getMessage(), e);
        //   throw new AuthSSLInitializationError("I/O error reading keystore/truststore file: " + e.getMessage());
    }
    return null;
}

From source file:ch.bfh.evoting.alljoyn.MessageEncrypter.java

/**
 * Method that encrypts data//w w w .  j a va  2s  .c o  m
 * @param data The data which should be encrypted
 * @return The encrypted bytes, null if encryption failed
 * 
 */
public byte[] encrypt(byte[] data) {
    //Inspired from http://stackoverflow.com/questions/992019/java-256-bit-aes-password-based-encryption

    Cipher cipher;
    try {
        cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

        // cipher.getParameters() seems to return null on Android 4.3 (Bug?)
        // Solution implemented from here: 
        // https://code.google.com/p/android/issues/detail?id=58191
        byte[] iv;
        if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) {
            iv = generateIv();
            cipher.init(Cipher.ENCRYPT_MODE, secretKey, new IvParameterSpec(iv));
        } else {
            cipher.init(Cipher.ENCRYPT_MODE, secretKey);
            AlgorithmParameters params = cipher.getParameters();
            iv = params.getParameterSpec(IvParameterSpec.class).getIV();
        }

        byte[] cipherText = cipher.doFinal(data);

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        outputStream.write(iv);
        outputStream.write(cipherText);

        return outputStream.toByteArray();
    } catch (NoSuchAlgorithmException e) {
        Log.d(TAG, e.getMessage() + " ");
        e.printStackTrace();
        return null;
    } catch (NoSuchPaddingException e) {
        Log.d(TAG, e.getMessage() + " ");
        e.printStackTrace();
        return null;
    } catch (InvalidKeyException e) {
        Log.d(TAG, e.getMessage() + " ");
        e.printStackTrace();
        return null;
    } catch (InvalidParameterSpecException e) {
        Log.d(TAG, e.getMessage() + " ");
        e.printStackTrace();
        return null;
    } catch (IllegalBlockSizeException e) {
        Log.d(TAG, e.getMessage() + " ");
        e.printStackTrace();
        return null;
    } catch (BadPaddingException e) {
        Log.d(TAG, e.getMessage() + " ");
        e.printStackTrace();
        return null;
    } catch (IOException e) {
        Log.d(TAG, e.getMessage() + " ");
        e.printStackTrace();
        return null;
    } catch (InvalidAlgorithmParameterException e) {
        Log.d(TAG, e.getMessage() + " ");
        e.printStackTrace();
        return null;
    }
}

From source file:eu.europa.esig.dss.DSSUtils.java

/**
 * @param digestAlgorithm/*from  ww w .  ja  va  2  s. c  o  m*/
 * @return
 * @throws NoSuchAlgorithmException
 */
public static MessageDigest getMessageDigest(final DigestAlgorithm digestAlgorithm) {
    try {
        final String digestAlgorithmOid = digestAlgorithm.getOid();
        final MessageDigest messageDigest = MessageDigest.getInstance(digestAlgorithmOid);
        return messageDigest;
    } catch (NoSuchAlgorithmException e) {
        throw new DSSException("Digest algorithm '" + digestAlgorithm.getName() + "' error: " + e.getMessage(),
                e);
    }
}

From source file:com.amazonaws.ipnreturnurlvalidation.SignatureUtilsForOutbound.java

private boolean validateSignatureV1(Map<String, String> parameters) throws SignatureException {

    if (this.awsSecretKey == null) {
        throw new SignatureException("Signature can not be verified without aws secret key.");
    }//from  w  w  w.  ja  v  a2  s .co m

    String stringToSign = calculateStringToSignV1(parameters);
    String signature = parameters.get(SIGNATURE_KEYNAME);

    String result;
    try {
        SecretKeySpec signingKey = new SecretKeySpec(this.awsSecretKey.getBytes(), "HmacSHA1");
        Mac mac = Mac.getInstance("HmacSHA1");
        mac.init(signingKey);
        byte[] rawHmac = mac.doFinal(stringToSign.getBytes("UTF-8"));
        result = new String(Base64.encodeBase64(rawHmac));
    } catch (NoSuchAlgorithmException e) {
        throw new SignatureException("Failed to generate HMAC : " + e.getMessage());
    } catch (InvalidKeyException e) {
        throw new SignatureException("Failed to generate HMAC : " + e.getMessage());
    } catch (UnsupportedEncodingException e) {
        throw new SignatureException("Failed to generate HMAC : " + e.getMessage());
    }

    return result.equals(signature);
}

From source file:server.PokemonRequisition.java

public String login(String username, String password) {
    String message = "";

    try {//from   ww  w  .j  a v  a  2  s. c o  m
        connection.connect();

        MessageDigest md = null;

        SQLQuery query = connection.newQuery();

        // Busca um usurio com as devidas credenciais
        query.select("*").from("users").where().equal("username", username)._and().equal("password", password);

        ResultSet rs = connection.execute(query);

        Player player = null;

        if (rs.next()) {
            try {
                md = MessageDigest.getInstance("MD5");

                boolean playerOnline = rs.getBoolean("online");

                // Se player estiver online
                if (playerOnline) {
                    System.out.println("User " + username + " already logged!");
                    message = Reply.FAIL.toString();
                } else {
                    // Utiliza nome e data corrente para gerar o hash md5 do validationCode
                    String nameAndDate = rs.getString("username") + ":"
                            + Calendar.getInstance().getTimeInMillis();

                    md.update(nameAndDate.getBytes());
                    String validationCode = new String(Hex.encodeHex(md.digest()));

                    //                        System.out.println("Creating Player...");

                    // TODO Recuperar todos os dados do usurio (pokemons)
                    player = new Player(rs.getInt("id"), validationCode);
                    player.setName(rs.getString("username"));

                    PlayerServer p = new PlayerServer(player.getId(), player.getName(),
                            player.getVerificationCode());

                    // TODO No utilizar diretamente os valores
                    p.setWidth(16);
                    p.setHeight(24);

                    obsPlayerOnline.add(p);

                    //                        System.out.println("Player created...");

                    // Serializa player
                    //                        System.out.println("Player serializing...");

                    Gson gson = new Gson();
                    message = gson.toJson(player);

                    //                        System.out.println("Player serialized...");
                }

                //                    System.out.println("PlayerString Size: " + message.length() + " bytes!");
                //                    System.out.println("Message Encrypted: " + message);
            } catch (NoSuchAlgorithmException e) {
                message = Reply.FAIL.toString();
                System.err.println(this.getClass().getName() + ": " + e.getMessage());
            } catch (IOException e) {
                message = Reply.FAIL.toString();
                System.err.println(this.getClass().getName() + ": " + e.getMessage());
            }
        } else {
            System.out.println("ResultSet is empty!");
        }

        rs.close();

        connection.disconnect();
    } catch (SQLException e) {
        message = Reply.FAIL.toString();
    }

    return message;
}

From source file:au.edu.monash.merc.capture.util.httpclient.ssl.AuthSSLProtocolSocketFactory.java

@SuppressWarnings("rawtypes")
private SSLContext createSSLContext() {
    try {/*  w  w w. j  a v a2 s  .  c o  m*/
        KeyManager[] keymanagers = null;
        TrustManager[] trustmanagers = null;
        if (this.keystoreUrl != null) {
            KeyStore keystore = createKeyStore(this.keystoreUrl, this.keystorePassword);
            if (LOG.isDebugEnabled()) {
                Enumeration aliases = keystore.aliases();
                while (aliases.hasMoreElements()) {
                    String alias = (String) aliases.nextElement();
                    Certificate[] certs = keystore.getCertificateChain(alias);
                    if (certs != null) {
                        LOG.debug("Certificate chain '" + alias + "':");
                        for (int c = 0; c < certs.length; c++) {
                            if (certs[c] instanceof X509Certificate) {
                                X509Certificate cert = (X509Certificate) certs[c];
                                LOG.debug(" Certificate " + (c + 1) + ":");
                                LOG.debug("  Subject DN: " + cert.getSubjectDN());
                                LOG.debug("  Signature Algorithm: " + cert.getSigAlgName());
                                LOG.debug("  Valid from: " + cert.getNotBefore());
                                LOG.debug("  Valid until: " + cert.getNotAfter());
                                LOG.debug("  Issuer: " + cert.getIssuerDN());
                            }
                        }
                    }
                }
            }
            keymanagers = createKeyManagers(keystore, this.keystorePassword);
        }
        if (this.truststoreUrl != null) {
            KeyStore keystore = createKeyStore(this.truststoreUrl, this.truststorePassword);
            if (LOG.isDebugEnabled()) {
                Enumeration aliases = keystore.aliases();
                while (aliases.hasMoreElements()) {
                    String alias = (String) aliases.nextElement();
                    LOG.debug("Trusted certificate '" + alias + "':");
                    Certificate trustedcert = keystore.getCertificate(alias);
                    if (trustedcert != null && trustedcert instanceof X509Certificate) {
                        X509Certificate cert = (X509Certificate) trustedcert;
                        LOG.debug("  Subject DN: " + cert.getSubjectDN());
                        LOG.debug("  Signature Algorithm: " + cert.getSigAlgName());
                        LOG.debug("  Valid from: " + cert.getNotBefore());
                        LOG.debug("  Valid until: " + cert.getNotAfter());
                        LOG.debug("  Issuer: " + cert.getIssuerDN());
                    }
                }
            }
            trustmanagers = createTrustManagers(keystore);
        }
        SSLContext sslcontext = SSLContext.getInstance("SSL");
        sslcontext.init(keymanagers, trustmanagers, null);
        return sslcontext;
    } catch (NoSuchAlgorithmException e) {
        LOG.error(e.getMessage(), e);
        throw new AuthSSLInitializationError("Unsupported algorithm exception: " + e.getMessage());
    } catch (KeyStoreException e) {
        LOG.error(e.getMessage(), e);
        throw new AuthSSLInitializationError("Keystore exception: " + e.getMessage());
    } catch (GeneralSecurityException e) {
        LOG.error(e.getMessage(), e);
        throw new AuthSSLInitializationError("Key management exception: " + e.getMessage());
    } catch (IOException e) {
        LOG.error(e.getMessage(), e);
        throw new AuthSSLInitializationError("I/O error reading keystore/truststore file: " + e.getMessage());
    }
}

From source file:org.dita.dost.ant.PluginInstallTask.java

private String getFileHash(final File file) {
    try (DigestInputStream digestInputStream = new DigestInputStream(
            new BufferedInputStream(new FileInputStream(file)), MessageDigest.getInstance("SHA-256"))) {
        IOUtils.copy(digestInputStream, new NullOutputStream());
        final MessageDigest digest = digestInputStream.getMessageDigest();
        final byte[] sha256 = digest.digest();
        return printHexBinary(sha256);
    } catch (NoSuchAlgorithmException e) {
        throw new IllegalArgumentException(e);
    } catch (IOException e) {
        throw new BuildException("Failed to calculate file checksum: " + e.getMessage(), e);
    }//from  ww  w  .  j  a  va2 s  . c  o m
}

From source file:com.evolveum.midpoint.prism.crypto.AESProtector.java

public String getSecretKeyDigest(SecretKey key) throws EncryptionException {
    MessageDigest sha1;//from   w ww .  j a  v a  2  s.co m
    try {
        sha1 = MessageDigest.getInstance(KEY_DIGEST_TYPE);
    } catch (NoSuchAlgorithmException ex) {
        throw new EncryptionException(ex.getMessage(), ex);
    }

    return Base64.encode(sha1.digest(key.getEncoded()));
}