Example usage for java.security NoSuchAlgorithmException printStackTrace

List of usage examples for java.security NoSuchAlgorithmException printStackTrace

Introduction

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

Prototype

public void printStackTrace() 

Source Link

Document

Prints this throwable and its backtrace to the standard error stream.

Usage

From source file:configuration.Key.java

/**
 * Creates a random key with the given parameters.
 * /*from w w w. jav a2s .c o  m*/
 * @param keySize
 *            the length of the key in bits.
 * @param version
 *            the version of the key.
 * @param algorithm
 *            the string representation of the cipher or HMAC algorithm
 *            used.
 * @return the random key with the specified parameters or <code>null</code>
 *         , if the key cannot be generated.
 */
public static Key randomKey(int keySize, int version, String algorithm) {
    if (keySize < 1) {
        throw new IllegalArgumentException("keySize has to be at least one!");
    }
    if (version < 1) {
        throw new IllegalArgumentException("version must be at least one!");
    }
    if (algorithm == null) {
        throw new NullPointerException("algorithm may not be null!");
    }

    SecretKey secretKey = null;

    try {
        KeyGenerator generator = KeyGenerator.getInstance(getSecretKeyAlgorithm(algorithm));
        generator.init(keySize);
        secretKey = generator.generateKey();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }

    return (secretKey != null) ? new Key(secretKey, version, algorithm) : null;
}

From source file:com.aqnote.shared.cryptology.asymmetric.DSA.java

public static KeyPair genKeyPair(int bit) {
    try {/*from   w  w w  . ja  v a 2s . c o  m*/
        KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(ALGORITHM, JCE_PROVIDER);
        keyPairGen.initialize(bit, new SecureRandom());
        return keyPairGen.generateKeyPair();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (NoSuchProviderException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:org.transdroid.daemon.Tfb4rt.Tfb4rtAdapter.java

/**
 * Calculate the MD5 hash of a password to use with the Torrentflux-b4rt dispatcher requests.
 * @param pass The plain text password//from w  w w . j  a v a  2 s  .  co  m
 * @return A hex-formatted MD5-hashed string of the password
 */
public static String md5Pass(String pass) {
    try {
        MessageDigest m = MessageDigest.getInstance("MD5");
        byte[] data = pass.getBytes();
        m.update(data, 0, data.length);
        BigInteger i = new BigInteger(1, m.digest());
        return String.format("%1$032X", i);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.aqnote.shared.cryptology.asymmetric.DSA.java

/**
 * ?public key// w  w w .  ja va2 s  .  c  om
 * 
 * @throws RuntimeException key
 */
public static PublicKey readPublicKey(byte[] keyBytes) throws RuntimeException {
    try {
        KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
        byte[] encodedKey = Base64.decodeBase64(keyBytes);
        EncodedKeySpec keySpec = new X509EncodedKeySpec(encodedKey);
        return keyFactory.generatePublic(keySpec);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (InvalidKeySpecException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.aqnote.shared.cryptology.asymmetric.DSA.java

/**
 * ?private key//from   w w w. j a  v  a  2 s  .  c om
 * 
 * @throws RuntimeException key
 */
public static PrivateKey readPrivateKey(byte[] keyBytes) throws RuntimeException {
    try {
        KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
        byte[] encodedKey = Base64.decodeBase64(keyBytes);
        EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(encodedKey);
        return keyFactory.generatePrivate(keySpec);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (InvalidKeySpecException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.camel.trainreserve.JDKHttpsClient.java

public static String doGet(String url) {
    InputStream in = null;//from  w ww  .ja  va  2 s  .  c o  m
    BufferedReader br = null;
    StringBuffer str_return = new StringBuffer();
    try {
        SSLContext sc = SSLContext.getInstance("SSL");
        sc.init(null, new TrustManager[] { new DefaultTrustManager() }, new java.security.SecureRandom());
        URL console = new URL(url);
        HttpsURLConnection conn = (HttpsURLConnection) console.openConnection();
        conn.setSSLSocketFactory(sc.getSocketFactory());
        conn.setHostnameVerifier(new TrustAnyHostnameVerifier());
        conn.connect();
        in = conn.getInputStream();
        br = new BufferedReader(new InputStreamReader(in));
        String line = null;
        while ((line = br.readLine()) != null) {
            str_return = str_return.append(line);
        }
        conn.disconnect();
    } catch (NoSuchAlgorithmException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (KeyManagementException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        try {
            br.close();
            in.close();
        } catch (Exception e) {
        }
    }
    return str_return.toString();
}

From source file:com.camel.trainreserve.JDKHttpsClient.java

public static ByteArrayOutputStream doGetImg(String url, String cookieStr) {
    InputStream in = null;/*  w w  w  .  jav  a  2  s  .  co  m*/
    ByteArrayOutputStream outStream = null;
    try {
        SSLContext sc = SSLContext.getInstance("SSL");
        sc.init(null, new TrustManager[] { new DefaultTrustManager() }, new SecureRandom());

        URL console = new URL(url);
        HttpsURLConnection conn = (HttpsURLConnection) console.openConnection();
        conn.setRequestProperty("Cookie", cookieStr);
        conn.setSSLSocketFactory(sc.getSocketFactory());
        conn.setHostnameVerifier(new TrustAnyHostnameVerifier());
        conn.connect();
        in = conn.getInputStream();
        outStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = 0;
        while ((len = in.read(buffer)) != -1) {
            outStream.write(buffer, 0, len);
        }
        conn.disconnect();
    } catch (NoSuchAlgorithmException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (KeyManagementException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        try {
            in.close();
        } catch (Exception e) {
        }
    }
    return outStream;
}

From source file:net.longfalcon.newsj.util.EncodingUtil.java

public static String md5Hash(String input) {
    if (_md5Digest == null) {
        try {/*from   www .  j  a v  a  2s  .c  om*/
            _md5Digest = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            // this will not fail.
            e.printStackTrace();
            return null;
        }
    }

    byte[] digest = _md5Digest.digest(input.getBytes());

    StringBuilder sb = new StringBuilder();

    for (byte b : digest) {
        sb.append(String.format("%02x", b));
    }

    return sb.toString();
}

From source file:net.longfalcon.newsj.util.EncodingUtil.java

/**
 * TODO: move to better algorithm?/*from  w  w w.j  a v  a  2  s .com*/
 * @param input
 * @return
 */
public static String sha1Hash(String input) {
    if (_sha1Digest == null) {
        try {
            _sha1Digest = MessageDigest.getInstance("SHA-1");
        } catch (NoSuchAlgorithmException e) {
            // this will not fail.
            e.printStackTrace();
            return null;
        }
    }

    byte[] digest = _sha1Digest.digest(input.getBytes());

    StringBuilder sb = new StringBuilder();

    for (byte b : digest) {
        sb.append(String.format("%02x", b));
    }

    return sb.toString();
}

From source file:com.jiangyifen.ec2.globaldata.license.LicenseManager.java

/**
 * 32 md5 //from  w ww.j  av a  2s .co m
 * 
 * @param plainText
 * @return
 */
private static String bit32_md5(String plainText) {
    if (StringUtils.isEmpty(plainText)) {
        return null;
    }

    try {
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(plainText.getBytes());
        byte b[] = md.digest();

        int i;

        StringBuffer buf = new StringBuffer("");
        for (int offset = 0; offset < b.length; offset++) {
            i = b[offset];
            if (i < 0)
                i += 256;
            if (i < 16)
                buf.append("0");
            buf.append(Integer.toHexString(i));
        }
        return buf.toString();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();

        // never happen
        return null;
    }
}