List of usage examples for java.security GeneralSecurityException printStackTrace
public void printStackTrace()
From source file:com.centurylink.mdw.util.CryptUtil.java
public static void main(String[] args) { if (args.length == 0 || args.length > 2) { System.err.println("arguments: (d|e) stringToEncrypt/Decrypt"); } else {/*ww w . j av a2 s . co m*/ boolean decryptMode = false; String input = args[0]; if (args.length == 2) { decryptMode = args[0].equals("d") ? true : false; input = args[1]; } try { if (decryptMode) { String decrypted = CryptUtil.decrypt(input); System.out.println("decrypted: '" + decrypted + "'"); String encrypted = CryptUtil.encrypt(decrypted); System.out.println("encrypted: '" + encrypted + "'"); } else { String encrypted = CryptUtil.encrypt(input); System.out.println("encrypted: '" + encrypted + "'"); String decrypted = CryptUtil.decrypt(encrypted); System.out.println("decrypted: '" + decrypted + "'"); } } catch (GeneralSecurityException ex) { ex.printStackTrace(); } } }
From source file:se.vgregion.portal.cs.util.CryptoUtilImpl.java
/** * Main method used for creating initial key file. * /*ww w .j a v a 2 s.co m*/ * @param args * - not used */ public static void main(String[] args) { final String keyFile = "./howto.key"; final String pwdFile = "./howto.properties"; CryptoUtilImpl cryptoUtils = new CryptoUtilImpl(); cryptoUtils.setKeyFile(new File(keyFile)); String clearPwd = "my_cleartext_pwd"; Properties p1 = new Properties(); Writer w = null; try { p1.put("user", "liferay"); String encryptedPwd = cryptoUtils.encrypt(clearPwd); p1.put("pwd", encryptedPwd); w = new FileWriter(pwdFile); p1.store(w, ""); } catch (GeneralSecurityException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (w != null) { try { w.close(); } catch (IOException e) { e.printStackTrace(); } } } // ================== Properties p2 = new Properties(); Reader r = null; try { r = new FileReader(pwdFile); p2.load(r); String encryptedPwd = p2.getProperty("pwd"); System.out.println(encryptedPwd); System.out.println(cryptoUtils.decrypt(encryptedPwd)); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (GeneralSecurityException e) { e.printStackTrace(); } finally { if (r != null) { try { r.close(); } catch (IOException e) { e.printStackTrace(); } } } }
From source file:com.corngo.base.support.utils.security.Digests.java
private static byte[] digest(InputStream input, String algorithm) throws IOException { try {// w w w .j a va2 s . c om MessageDigest messageDigest = MessageDigest.getInstance(algorithm); int bufferLength = 8 * 1024; byte[] buffer = new byte[bufferLength]; int read = input.read(buffer, 0, bufferLength); while (read > -1) { messageDigest.update(buffer, 0, read); read = input.read(buffer, 0, bufferLength); } return messageDigest.digest(); } catch (GeneralSecurityException e) { e.printStackTrace(); return null; } }
From source file:pl.kotcrab.crypto.CryptoUtils.java
/** Generates PublicKey from encoded bytes of X509EncodedKeySpec. Bytes must be in Base64 string. * @param base64 Key from encoded bytes of {@link X509EncodedKeySpec#getEncoded()}. Encoded in Base64 string. * @return created public key *///w ww . j a v a 2 s. c o m public static PublicKey getRSAPublicKeyFromString64(String base64) { try { X509EncodedKeySpec keySpec = new X509EncodedKeySpec(Base64.decodeBase64(base64)); KeyFactory keyFactory = KeyFactory.getInstance("RSA", "BC"); return keyFactory.generatePublic(keySpec); } catch (GeneralSecurityException e) { e.printStackTrace(); } return null; }
From source file:pl.kotcrab.crypto.CryptoUtils.java
/** Generates AES key from provided password and salt. Used algorithm is PBKDF2WithHmacSHA1. * @param password password in char array, using {@link CryptoUtils#fillZeros(char[])} is recommend after generating key * @param salt salt for this key//w ww . j a va2s . c o m * @return SecretKeySpec */ public static SecretKeySpec getAESKeyFromPassword(char[] password, byte[] salt) { try { SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); KeySpec spec = new PBEKeySpec(password, salt, 65536, 256); return new SecretKeySpec(factory.generateSecret(spec).getEncoded(), "AES"); } catch (GeneralSecurityException e) { e.printStackTrace(); } return null; }
From source file:pl.kotcrab.crypto.CryptoUtils.java
/** Constructs and returns secure random. Algorithm depends on operating system, on Windows SHA1PRNG from Sun will be used and on * Unix and Mac systems NativePRNG will be used. * @return SecureRandom instance */ public static SecureRandom getSecureRandom() { try {// w ww. j a v a 2 s . c o m if (OSDetector.isUnix() || OSDetector.isMac()) return SecureRandom.getInstance("NativePRNG", "SUN"); else return SecureRandom.getInstance("SHA1PRNG", "SUN"); } catch (GeneralSecurityException e) { e.printStackTrace(); } return null; }
From source file:com.corngo.base.support.utils.security.Digests.java
/** * , ?md5sha1.// w ww . j av a 2 s.co m */ private static byte[] digest(byte[] input, String algorithm, byte[] salt, int iterations) { try { MessageDigest digest = MessageDigest.getInstance(algorithm); if (salt != null) { digest.update(salt); } byte[] result = digest.digest(input); for (int i = 1; i < iterations; i++) { digest.reset(); result = digest.digest(result); } return result; } catch (GeneralSecurityException e) { e.printStackTrace(); return null; } }
From source file:com.alphabetbloc.accessmrs.utilities.NetworkUtils.java
public static HttpClient getHttpClient() { HttpClient client = null;/*from w w w . j a v a2s .c o m*/ try { if (App.DEBUG) Log.v(TAG, "httpClient is null, download is creating a new client"); SSLContext sslContext = createSslContext(); MySSLSocketFactory socketFactory = new MySSLSocketFactory(sslContext, new BrowserCompatHostnameVerifier()); client = createHttpClient(socketFactory); } catch (GeneralSecurityException e) { Log.e(TAG, "Could not load the trust manager"); e.printStackTrace(); } catch (IOException e) { Log.e(TAG, "Could not load the trust manager. Ensure credential storage is available"); e.printStackTrace(); } return client; }
From source file:com.edduarte.protbox.Protbox.java
private static void saveAllRegistries() { try {//from w w w. j a va 2 s. c om Cipher cipher = Cipher.getInstance("AES"); for (PairPanel c : trayApplet.getPairPanels()) { PReg toSerialize = c.getRegistry(); // stops the registry, which stops the running threads and processes toSerialize.stop(); File file = new File(Constants.REGISTRIES_DIR, toSerialize.id); // encrypt directories using the inserted password at the beginning of the application try (ByteArrayOutputStream out = new ByteArrayOutputStream(); ObjectOutputStream stream = new ObjectOutputStream(out)) { stream.writeObject(toSerialize); stream.flush(); byte[] data = out.toByteArray(); cipher.init(Cipher.ENCRYPT_MODE, registriesPasswordKey); data = cipher.doFinal(data); FileUtils.writeByteArrayToFile(file, data); } catch (GeneralSecurityException ex) { logger.error("Invalid password! Registry {} not saved!", toSerialize.toString()); ex.printStackTrace(); } } } catch (GeneralSecurityException | IOException ex) { if (Constants.verbose) { logger.info("Error while saving registries.", ex); } } }
From source file:org.sipfoundry.openfire.ws.WebSocketPlugin.java
public void trustAllCerts() { ProtocolSocketFactory sf;/*from ww w. j av a 2s .co m*/ try { sf = new EasySSLProtocolSocketFactory(); Protocol p = new Protocol("https", sf, 443); Protocol.registerProtocol("https", p); } catch (GeneralSecurityException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }