1/*2 * Copyright 2001-2013 The Apache Software Foundation.3 * 4 * Licensed under the Apache License, Version 2.0 (the "License");5 * you may not use this file except in compliance with the License.6 * You may obtain a copy of the License at7 * 8 * http://www.apache.org/licenses/LICENSE-2.09 * 10 * Unless required by applicable law or agreed to in writing, software11 * distributed under the License is distributed on an "AS IS" BASIS,12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13 * See the License for the specific language governing permissions and14 * limitations under the License.15 *16 */17package org.apache.juddi.adminconsole;
1819import java.nio.charset.Charset;
20import javax.crypto.*;
21import javax.crypto.spec.*;
22import org.apache.commons.codec.binary.Base64;
2324import org.apache.commons.logging.Log;
25import org.apache.commons.logging.LogFactory;
2627/**28 * <summary> This program uses a AES key, retrieves its raw bytes, and then29 * reinstantiates a AES key from the key bytes.</summary> The reinstantiated key30 * is used to initialize a AES cipher for encryption and decryption. source :31 * http://java.sun.com/developer/technicalArticles/Security/AES/AES_v1.html32 *33 * @author <a href="mailto:alexoree@apache.org">Alex O'Ree</a>34 */35publicclassAES {
3637publicstaticfinal String logname = "org.apache.juddi.gui";
38publicstaticfinal Log log = LogFactory.getLog(logname);
3940/**41 * generates an AES based off of the selected key size42 *43 * @param keysize44 * @return may return null if the key is not of a supported size by the45 * current jdk46 */47publicstatic String GEN(int keysize) {
48 KeyGenerator kgen;
49try {
50 kgen = KeyGenerator.getInstance("AES");
51 kgen.init(keysize);
52 SecretKey skey = kgen.generateKey();
53 byte[] raw = skey.getEncoded();
54returnnew String(Base64.encodeBase64(raw), Charset.defaultCharset());
55 } catch (Exception ex) {
56 log.fatal("error generating key", ex);
57 }
58returnnull;
59 }
6061/**62 * Generate a new AES 256 bit encryption key. Once generated, this key63 * can be used to replace the default key.64 *65 * @return a base64 encoded key, 256 bit66 */67publicstatic String GEN() {
68return GEN(256);
69 }
7071static String EN(String cleartext, String key) throws Exception {
72 byte[] raw =//skey.getEncoded();73 Base64.decodeBase64(key.getBytes(Charset.defaultCharset())); //74 SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
75// Instantiate the cipher76 Cipher cipher = Cipher.getInstance("AES");
77 cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
78 byte[] encrypted = cipher.doFinal(cleartext.getBytes());
79returnnew String(Base64.encodeBase64(encrypted), Charset.defaultCharset());
80 }
8182static String DE(String ciphertext, String key) throws Exception {
83 byte[] raw =//skey.getEncoded();84 Base64.decodeBase64(key.getBytes(Charset.defaultCharset())); //85 SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
86 Cipher cipher = Cipher.getInstance("AES");
87 cipher.init(Cipher.DECRYPT_MODE, skeySpec);
88 byte[] original = cipher.doFinal(Base64.decodeBase64(ciphertext.getBytes(Charset.defaultCharset())));
89returnnew String(original);
90 }
9192/**93 * return true is the supplied key is a valid aes key94 *95 * @param key96 * @return true if the key is valid97 */98publicstaticboolean ValidateKey(String key) {
99try {
100 String src = "abcdefghijklmopqrstuvwxyz123567890!@#$%^&*()_+{}|:\">?<,";
101 String x = EN(src, key);
102 String y = DE(x, key);
103//if the sample text is encryptable and decryptable, and it was actually encrypted104if (y.equals(src) && !x.equals(y)) {
105returntrue;
106 }
107return false;
108 } catch (Exception ex) {
109 log.info("Key validation failed! "+ ex.getMessage());
110 log.debug("Key validation failed! "+ ex.getMessage(), ex);
111return false;
112 }
113 }
114115/**116 * encrypts a password using AES Requires the Unlimited Strength Crypto117 * Extensions118 *119 * @param clear120 * @param key121 * @return a base64 encoded cipher of the clear text using the key122 */123publicstatic String Encrypt(String clear, String key) throws Exception {
124if ((clear == null || clear.length() == 0)) {
125return"";
126 }
127if (key == null || key.length() == 0) {
128 log.fatal("The generated encryption key was null or emtpy!");
129 }
130try {
131return AES.EN(clear, key);
132 } catch (Exception ex) {
133 log.fatal("Cannot encrypt sensitive information! Check to make sure the unlimited strength JCE is installed " + ex.getMessage(), ex);
134thrownew Exception("Internal Configuration Error, See Log for details. ");
135 }
136// return "";137 }
138139/**140 * Decrypts a password or other sensitive data If the parameter is null141 * or empty, an empty string is returned. If the parameter is not142 * encrypted or was encrypted using a different key or it fails to143 * decrypt, the original text is returned.144 *145 * @param cipher encrypted text146 * @param key147 * @return the decrypted string148 */149publicstatic String Decrypt(String cipher, String key) {
150if ((cipher == null || cipher.length() == 0)) {
151return"";
152 }
153if (key == null || key.length() == 0) {
154 log.fatal("The generated encryption key was null or emtpy!");
155 }
156try {
157return AES.DE(cipher, key);
158 } catch (Exception ex) {
159 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);
160 }
161return cipher;
162163 }
164 }