001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.wicket.util.crypt;
018:
019: import java.io.UnsupportedEncodingException;
020: import java.security.GeneralSecurityException;
021:
022: import javax.crypto.Cipher;
023:
024: import org.apache.wicket.WicketRuntimeException;
025: import org.slf4j.Logger;
026: import org.slf4j.LoggerFactory;
027:
028: /**
029: * Abstract base class for JCE based ICrypt implementations.
030: *
031: * @author Juergen Donnerstag
032: */
033: public abstract class AbstractCrypt implements ICrypt {
034: /** Default encryption key */
035: private static final String DEFAULT_ENCRYPTION_KEY = "WiCkEt-CrYpT";
036:
037: /** Encoding used to convert java String from and to byte[] */
038: private static final String CHARACTER_ENCODING = "UTF-8";
039:
040: /** Log. */
041: private static final Logger log = LoggerFactory
042: .getLogger(AbstractCrypt.class);
043:
044: /** Key used to de-/encrypt the data */
045: private String encryptionKey = DEFAULT_ENCRYPTION_KEY;
046:
047: /**
048: * Constructor
049: */
050: public AbstractCrypt() {
051: }
052:
053: /**
054: * Decrypts a string into a string.
055: *
056: * @param text
057: * text to decript
058: * @return the decrypted text
059: */
060: public final String decryptUrlSafe(final String text) {
061: try {
062: byte[] encrypted = Base64UrlSafe.decodeBase64(text
063: .getBytes());
064: return new String(decryptByteArray(encrypted),
065: CHARACTER_ENCODING);
066: } catch (UnsupportedEncodingException ex) {
067: throw new WicketRuntimeException(ex.getMessage());
068: }
069: }
070:
071: /**
072: * Encrypt a string into a string using URL safe Base64 encoding.
073: *
074: * @param plainText
075: * text to encrypt
076: * @return encrypted string
077: */
078: public final String encryptUrlSafe(final String plainText) {
079: try {
080: byte[] cipherText = encryptStringToByteArray(plainText);
081: return new String(Base64UrlSafe.encodeBase64(cipherText));
082: } catch (GeneralSecurityException e) {
083: log.error("Unable to encrypt text '" + plainText + "'", e);
084: return null;
085: }
086: }
087:
088: /**
089: * Get encryption private key
090: *
091: * @return encryption private key
092: */
093: public String getKey() {
094: return this .encryptionKey;
095: }
096:
097: /**
098: * Set encryption private key
099: *
100: * @param key
101: * private key to make de-/encryption unique
102: */
103: public void setKey(final String key) {
104: this .encryptionKey = key;
105: }
106:
107: /**
108: * Crypts the given byte array
109: *
110: * @param input
111: * byte array to be crypted
112: * @param mode
113: * crypt mode
114: * @return the input crypted. Null in case of an error
115: * @throws GeneralSecurityException
116: */
117: protected abstract byte[] crypt(final byte[] input, final int mode)
118: throws GeneralSecurityException;
119:
120: /**
121: * Decrypts an encrypted, but Base64 decoded byte array into a byte array.
122: *
123: * @param encrypted
124: * byte array to decrypt
125: * @return the decrypted text
126: */
127: private final byte[] decryptByteArray(final byte[] encrypted) {
128: try {
129: return crypt(encrypted, Cipher.DECRYPT_MODE);
130: } catch (GeneralSecurityException e) {
131: throw new WicketRuntimeException(
132: "Unable to decrypt the text '"
133: + encrypted.toString() + "'", e);
134: }
135: }
136:
137: /**
138: * Encrypts the given text into a byte array.
139: *
140: * @param plainText
141: * text to encrypt
142: * @return the string encrypted
143: * @throws GeneralSecurityException
144: */
145: private final byte[] encryptStringToByteArray(final String plainText)
146: throws GeneralSecurityException {
147: try {
148: return crypt(plainText.getBytes(CHARACTER_ENCODING),
149: Cipher.ENCRYPT_MODE);
150: } catch (UnsupportedEncodingException ex) {
151: throw new WicketRuntimeException(ex.getMessage());
152: }
153: }
154: }
|