/** * Generate a new AES 256 bit encryption key. Once generated, this key can * be used to replace the default key. * * @return */ public static String GEN() { return GEN((short) 128); }
/** * return true is the supplied key is a valid aes key * * @param key * @return */ public static boolean validateKey(final String key) { try { String src = "abcdefghijklmopqrstuvwxyz123567890!@#$%^&*()_+{}|:\">?<,"; String x = EN(src, key); String y = DE(x, key); //if the sample text is encryptable and decryptable, and it was actually encrypted return y.equals(src) && !x.equals(y); } catch (Throwable ex) { // log.log(Level.WARN, null, ex); return false; } }
if (System.getenv("fgsms.keyFile") != null) { key = readAllText(new File(System.getenv("fgsms.keyFile"))); } else if (key == null && System.getProperty("fgsms.keyFile") != null) { key = readAllText(new File(System.getProperty("fgsms.keyFile"))); } else if (key == null) { File f = new File(new AES().getUrl("fgsms-aes128.key")); key = readAllText(f); } catch (Exception ex) { log.log(Level.DEBUG,null,ex); try { File f = new File(AES.class.getResource("fgsms-aes128.key").toURI()); key = readAllText(f); } catch (Exception ex) { log.log(Level.DEBUG,null,ex); try { InputStream is = AES.class.getResourceAsStream("fgsms-aes128.key"); key = readAllText(is); is.close(); } catch (Exception ex) {
/** * encrypts a password AES 256 bit Requires the Unlimited Strength Crypto * Extensions * * @param clear * @return */ public static String EN(String clear) { if (stringIsNullOrEmpty(clear)) { return ""; } try { return AES.EN(clear); } catch (Exception ex) { log.log(Level.FATAL, "############################################# fgsms cannot encrypt! Check to make sure the unlimited strength JCE is installed: " + ex.getMessage()); } return ""; }
/** * Decrypts a password or other sensitive data. If the parameter is null or * empty, an empty string is returned. If the parameter is not encrypted or * was encrypted using a different key or it fails to decrypt, the original * text is returned. * * @param cipher * @return */ public static String DE(String cipher) { if (stringIsNullOrEmpty(cipher)) { return ""; } try { return AES.DE(cipher); } catch (Exception ex) { log.log(Level.FATAL, "############################################# fgsms cannot decrypt! Check to make sure the unlimited strength JCE is installed: " + ex.getMessage()); } return cipher; }
private static String readAllText(File file) { if (file == null || !file.exists()) { log.log(Level.WARN, "Referenced key does not exist" + file.getAbsolutePath()); return null; } FileInputStream stream = null; try { stream = new FileInputStream(file); String str = readAllText(stream); stream.close(); return (str); } catch (Exception e) { log.log(Level.DEBUG, "error reading key", e); return null; } finally { if (stream != null) { try { stream.close(); } catch (Exception ex) { log.log(Level.DEBUG, "error reading key", ex); } } } }