Java tutorial
/* * Copyright 2008 The Corner Team. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * 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 corner.services.impl; import java.io.UnsupportedEncodingException; import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; import org.apache.commons.codec.binary.Hex; import corner.services.EncryptService; import corner.services.captcha.CipherKey; /** * DEsedeEncryptService * * @author dong * @version $Revision: 3050 $ * @since 0.2 */ public class DESedeEncryptServiceImpl implements EncryptService { private static final String CHIPERH_CHARSET = "UTF-8"; public final static String Algorithm = "DESede"; // static { // Security.addProvider(new com.sun.crypto.provider.SunJCE()); // } private final CipherKey cipher; public DESedeEncryptServiceImpl(final CipherKey cipher) { if (cipher == null) { throw new IllegalArgumentException("The cipher must not be null."); } this.cipher = cipher; } public byte[] encrypt(byte[] src, byte[] key) { try { SecretKey deskey = new SecretKeySpec(key, Algorithm); Cipher c1 = Cipher.getInstance(Algorithm); c1.init(Cipher.ENCRYPT_MODE, deskey); return c1.doFinal(src); } catch (java.security.NoSuchAlgorithmException e1) { e1.printStackTrace(); } catch (javax.crypto.NoSuchPaddingException e2) { e2.printStackTrace(); } catch (java.lang.Exception e3) { e3.printStackTrace(); } return null; } public byte[] decrypt(byte[] src, byte[] keybyte) { try { SecretKey deskey = new SecretKeySpec(keybyte, Algorithm); Cipher c1 = Cipher.getInstance(Algorithm); c1.init(Cipher.DECRYPT_MODE, deskey); return c1.doFinal(src); } catch (java.security.NoSuchAlgorithmException e1) { e1.printStackTrace(); } catch (javax.crypto.NoSuchPaddingException e2) { e2.printStackTrace(); } catch (java.lang.Exception e3) { e3.printStackTrace(); } return null; } public String encrypt(final String src) { if (src == null) return null; try { // byte[] encoded = encrypt(src.getBytes(CHIPERH_CHARSET), cipher.getCipher()); // ? return new String(Hex.encodeHex(encoded)); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return null; } public String decrypt(final String cryptograph) { if (cryptograph == null) return null; try { // ? byte[] decoded = Hex.decodeHex(cryptograph.toCharArray()); // cryptograph.getBytes(CHIPERH_CHARSET); // byte[] srcBytes = decrypt(decoded, cipher.getCipher()); if (srcBytes == null) return null; return new String(srcBytes, CHIPERH_CHARSET); } catch (Throwable e) { e.printStackTrace(); } return null; } }