org.ajax4jsf.framework.util.base64.Codec Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ajax4jsf Show documentation
Show all versions of ajax4jsf Show documentation
Ajax4jsf is an open source extension to the JavaServer Faces standard that adds AJAX capability to JSF applications without requiring the writing of any JavaScript.
The newest version!
/**
* Licensed under the Common Development and Distribution License,
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.sun.com/cddl/
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package org.ajax4jsf.framework.util.base64;
import java.security.spec.KeySpec;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
/**
* @author shura (latest modification by $Author: alexsmirnov $)
* @version $Revision: 1.5 $ $Date: 2006/09/29 10:14:05 $
*
*/
public class Codec {
private Cipher e = null;
private Cipher d = null;
/**
*
*/
public Codec() {
super();
// TODO Auto-generated constructor stub
}
/**
*
*/
public Codec( String p ) throws Exception {
byte[] s = {
(byte)0xA9, (byte)0x9B, (byte)0xC8, (byte)0x32,
(byte)0x56, (byte)0x34, (byte)0xE3, (byte)0x03
};
// try {
KeySpec keySpec = new DESKeySpec(p.getBytes("UTF8"));
SecretKey key = SecretKeyFactory.getInstance("DES")
.generateSecret(keySpec);
e = Cipher.getInstance(key.getAlgorithm());
d = Cipher.getInstance(key.getAlgorithm());
// Prepare the parameters to the cipthers
// AlgorithmParameterSpec paramSpec = new IvParameterSpec(s);
e.init(Cipher.ENCRYPT_MODE, key);
d.init(Cipher.DECRYPT_MODE, key);
// } catch (Exception e) {
// // TODO: handle exception
// }
}
public String decode(String str) throws Exception {
byte[] src = str.getBytes("UTF8");
byte[] utf8 = decode(src);
// Decode using utf-8
return new String(utf8, "UTF8");
}
public String encode(String str) throws Exception {
// try {
byte[] src = str.getBytes("UTF8");
// int len = (src.length/8+1)*8;
// byte[] block = new byte[len];
// Arrays.fill(block,0,len,(byte)0x20);
// System.arraycopy(src,0,block,0,src.length);
// Decrypt
byte[] utf8 = encode(src);
// Decode using utf-8
return new String(utf8, "UTF8");
// } catch (Exception e) {
// // TODO: handle exception
// return null;
// }
}
public byte[] decode(byte[] src) throws Exception {
byte[] dec = URL64Codec.decodeBase64(src);
// Decrypt
if (null != d) {
return d.doFinal(dec);
} else {
return dec;
}
}
public byte[] encode(byte[] src) throws Exception {
byte[] dec;
if(null != e){
dec = e.doFinal(src);
} else {
dec = src;
}
// Decrypt
return URL64Codec.encodeBase64(dec);
}
}