### Eclipse Workspace Patch 1.0 #P juddi-gui-war Index: src/main/java/org/apache/juddi/webconsole/AES.java =================================================================== --- src/main/java/org/apache/juddi/webconsole/AES.java (revision 1558993) +++ src/main/java/org/apache/juddi/webconsole/AES.java (working copy) @@ -19,6 +19,8 @@ import javax.crypto.*; import javax.crypto.spec.*; +import org.apache.commons.codec.binary.Base64; +import org.apache.commons.codec.binary.Hex; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -32,173 +34,122 @@ */ public class AES { - public static final String logname = "org.apache.juddi.gui"; - public static final Log log = LogFactory.getLog(logname); - - /** - * Turns array of bytes into string - * - * @param buf Array of bytes to convert to hex string - * @return Generated hex string - */ - private static String asHex(byte buf[]) { - //return new String(buf); - StringBuilder strbuf = new StringBuilder(buf.length * 2); - int i; - - for (i = 0; i < buf.length; i++) { - if (((int) buf[i] & 0xff) < 0x10) { - strbuf.append("0"); - } - strbuf.append(Long.toString((int) buf[i] & 0xff, 16)); - } - - return strbuf.toString(); + public static final String logname = "org.apache.juddi.gui"; + public static final Log log = LogFactory.getLog(logname); + + /** + * generates an AES based off of the selected key size + * + * @param keysize + * @return may return null if the key is not of a supported size by the + * current jdk + */ + public static String GEN(int keysize) { + KeyGenerator kgen; + try { + kgen = KeyGenerator.getInstance("AES"); + kgen.init(keysize); + SecretKey skey = kgen.generateKey(); + byte[] raw = skey.getEncoded(); + return Base64.encodeBase64String(raw); + } catch (Exception ex) { + log.fatal("error generating key", ex); } + return null; + } - private static byte[] hexToBytes(String s) { - //return s.getBytes(); - return hexToBytes(s.toCharArray()); - } - private static final char[] kDigits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', - 'b', 'c', 'd', 'e', 'f'}; + /** + * 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(256); + } - private static byte[] hexToBytes(char[] hex) { - int length = hex.length / 2; - byte[] raw = new byte[length]; - for (int i = 0; i < length; i++) { - int high = Character.digit(hex[i * 2], 16); - int low = Character.digit(hex[i * 2 + 1], 16); - int value = (high << 4) | low; - if (value > 127) { - value -= 256; - } - raw[i] = (byte) value; - } - return raw; - } + static String EN(String cleartext, String key) throws Exception { + byte[] raw =//skey.getEncoded(); + Base64.decodeBase64(key); + SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); + // Instantiate the cipher + Cipher cipher = Cipher.getInstance("AES"); + cipher.init(Cipher.ENCRYPT_MODE, skeySpec); + byte[] encrypted = cipher.doFinal(cleartext.getBytes()); + return Base64.encodeBase64String(encrypted); + } - /** - * generates an AES based off of the selected key size - * - * @param keysize - * @return may return null if the key is not of a supported size by the - * current jdk - */ - public static String GEN(int keysize) { - KeyGenerator kgen; - try { - kgen = KeyGenerator.getInstance("AES"); - kgen.init(keysize); - SecretKey skey = kgen.generateKey(); - byte[] raw = skey.getEncoded(); - return asHex(raw); - } catch (Exception ex) { - log.fatal("error generating key", ex); - } - return null; - } + + static String DE(String ciphertext, String key) throws Exception { + byte[] raw =//skey.getEncoded(); + Base64.decodeBase64(key); // + SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); + Cipher cipher = Cipher.getInstance("AES"); + cipher.init(Cipher.DECRYPT_MODE, skeySpec); + byte[] original = cipher.doFinal(Base64.decodeBase64(ciphertext)); + return new String(original); + } - /** - * 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(256); + /** + * return true is the supplied key is a valid aes key + * + * @param key + * @return + */ + public static boolean ValidateKey(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 + if (y.equals(src) && !x.equals(y)) { + return true; + } + return false; + } catch (Exception ex) { + log.info("Key validation failed!", ex); + return false; } + } - static String EN(String cleartext, String key) throws Exception { - byte[] raw =//skey.getEncoded(); - hexToBytes(key); // - SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); - // Instantiate the cipher - Cipher cipher = Cipher.getInstance("AES"); - cipher.init(Cipher.ENCRYPT_MODE, skeySpec); - byte[] encrypted = cipher.doFinal(cleartext.getBytes()); - return asHex(encrypted); + /** + * encrypts a password using AES Requires the Unlimited Strength Crypto + * Extensions + * + * @param clear + * @return + */ + public static String Encrypt(String clear, String key) { + if ((clear == null || clear.length() == 0)) { + return ""; } - - static String DE(String ciphertext, String key) throws Exception { - byte[] raw =//skey.getEncoded(); - hexToBytes(key); // - SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); - Cipher cipher = Cipher.getInstance("AES"); - cipher.init(Cipher.DECRYPT_MODE, skeySpec); - byte[] original = cipher.doFinal(hexToBytes(ciphertext)); - return new String(original); + try { + return AES.EN(clear, key); + } catch (Exception ex) { + log.fatal("Cannot encrypt sensitive information! Check to make sure the unlimited strength JCE is installed " + ex.getMessage()); } + return ""; + } - /** - * return true is the supplied key is a valid aes key - * - * @param key - * @return - */ - public static boolean ValidateKey(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 - if (y.equals(src) && !x.equals(y)) { - return true; - } - return false; - } catch (Exception ex) { - log.info("Key validation failed!", ex); - return false; - } + /** + * 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 Decrypt(String cipher, String key) { + if ((cipher == null || cipher.length() == 0)) { + return ""; } - - /** - * encrypts a password using AES Requires the Unlimited Strength Crypto - * Extensions - * - * @param clear - * @param key - * @return - */ - public static String Encrypt(String clear, String key) throws Exception { - if ((clear == null || clear.length() == 0)) { - return ""; - } - if (key == null || key.length() == 0) { - log.fatal("The generated encryption key was null or emtpy!"); - } - try { - return AES.EN(clear, key); - } catch (Exception ex) { - log.fatal("Cannot encrypt sensitive information! Check to make sure the unlimited strength JCE is installed " + ex.getMessage(), ex); - throw new Exception("Internal Configuration Error, See Log for details. "); - } - // return ""; + try { + return AES.DE(cipher, key); + } catch (Exception ex) { + log.fatal("trouble decrypting data, check to make sure the unlimited strength JCE is installed. If this error occured during deployment, I'll automatically try a smaller key size. " + ex.getMessage()); } + return cipher; - /** - * 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 encrypted text - * @param key - * @return - */ - public static String Decrypt(String cipher, String key) { - if ((cipher == null || cipher.length() == 0)) { - return ""; - } - if (key == null || key.length() == 0) { - log.fatal("The generated encryption key was null or emtpy!"); - } - try { - return AES.DE(cipher, key); - } catch (Exception ex) { - log.fatal("trouble decrypting data, check to make sure the unlimited strength JCE is installed. If this error occured during deployment, I'll automatically try a smaller key size. " + ex.getMessage(), ex); - } - return cipher; - - } + } }