Package org.internna.iwebmvc.core.crypto.impl

Source Code of org.internna.iwebmvc.core.crypto.impl.DES3CiphererDecipherer

/*
* Copyright 2002-2007 the original author or authors.
*
* 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 org.internna.iwebmvc.core.crypto.impl;

import java.security.Security;
import javax.annotation.PostConstruct;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import org.apache.commons.codec.binary.Base64;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.internna.iwebmvc.core.crypto.AbstractCipherer;
import org.internna.iwebmvc.core.crypto.Decipherer;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

/**
* 3DES implementation based on BouncyCastle libraries.
*
* @author Jose Noheda
* @since 1.0
*/
@Component("cryptographyManager")
public class DES3CiphererDecipherer extends AbstractCipherer implements Decipherer {

    private final String ENCODING = "UTF8";

    private Cipher cipherer;
    private Cipher decipherer;

    /**
     * Initialization method. Creates DESede/ECB/PKCS5Padding keys. A new key
     * is generated with each invocation (redeploy/reboot).
     *
     * @throws java.lang.Exception
     */
    @PostConstruct
    public final DES3CiphererDecipherer init() throws Exception {
        Security.addProvider(new BouncyCastleProvider());
        cipherer = Cipher.getInstance("DESede/ECB/PKCS5Padding", "BC");
        KeyGenerator keyGen = KeyGenerator.getInstance("DESede");
        keyGen.init(168);
        SecretKey key = keyGen.generateKey();
        cipherer.init(Cipher.ENCRYPT_MODE, key);
        decipherer = Cipher.getInstance("DESede/ECB/PKCS5Padding", "BC");
        decipherer.init(Cipher.DECRYPT_MODE, key);
        if (log.isDebugEnabled()) log.debug("Cryptography loaded with [DESede/ECB/PKCS5Padding] algorityhms and initialized");
        return this;
    }

    /**
     * Performs the actual encryption using the generated key.
     * @param element a string to be encrypted
     * @return an unintelligible String
     */
    @Override
    protected String visit(final String element) {
        try {
            byte[] bytes = cipherer.doFinal(element.getBytes(ENCODING));
            bytes = Base64.encodeBase64(bytes);
            return new String(bytes, ENCODING);
        } catch (Exception ex) {
            return element;
        }
    }

    /**
     * Performs the actual deciphering using the generated key.
     *
     * @param encText
     * @return a readable String
     */
    public final String decrypt(final String encText) {
        try {
            String toDecode = StringUtils.replace(encText, " ", "+");
            byte[] bytes = toDecode.getBytes(ENCODING);
            bytes = Base64.decodeBase64(bytes);
            return new String(decipherer.doFinal(bytes), ENCODING);
        } catch (Exception ex) {
            return encText;
        }
    }

    /**
     * Detects encrypted text.
     *
     * @param test a String
     * @return <code>true</code> if the string was previously encrypted
     */
    public boolean isEncrypted(String test) {
        return StringUtils.hasText(test) ? !decrypt(test).equals(test) : false;
    }

}
TOP

Related Classes of org.internna.iwebmvc.core.crypto.impl.DES3CiphererDecipherer

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.