org.suren.autotest.web.framework.util.Encryptor.java Source code

Java tutorial

Introduction

Here is the source code for org.suren.autotest.web.framework.util.Encryptor.java

Source

/*
 * 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.suren.autotest.web.framework.util;

import java.io.ByteArrayOutputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;

/**
 * 
 * @author suren
 * @date 20151124 ?5:04:05
 */
public class Encryptor {
    private SecretKey key;
    private Cipher cipher;
    private byte[] cipherByte;

    public static final String ALG_DES = "DES";
    private static Encryptor encryptor;

    private Encryptor() {
    }

    private Encryptor(String algorithm, String keyStr) throws NoSuchAlgorithmException, NoSuchPaddingException {
        key = getSecretKey(keyStr, ALG_DES);

        cipher = Cipher.getInstance(algorithm);
    }

    public static Encryptor getInstance(String algorithm, String secretKey)
            throws NoSuchAlgorithmException, NoSuchPaddingException {
        if (encryptor == null) {
            if (StringUtils.isBlank(secretKey)) {
                secretKey = "http://surenpi.com";
            }

            encryptor = new Encryptor(algorithm, secretKey);
        }

        return encryptor;
    }

    public byte[] encrypt(String str) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
        cipher.init(Cipher.ENCRYPT_MODE, key);
        cipherByte = cipher.doFinal(str.getBytes());
        return cipherByte;
    }

    public String encryptStr(String str)
            throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
        byte[] encryptArray = encrypt(str);

        StringBuffer strBuf = new StringBuffer();
        for (byte e : encryptArray) {
            strBuf.append(intToChar((e >> 4) & 0x0f));
            strBuf.append(intToChar(e & 0x0f));
        }

        return strBuf.toString();
    }

    public byte[] decrypt(byte[] buf) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
        cipher.init(Cipher.DECRYPT_MODE, key);
        cipherByte = cipher.doFinal(buf);
        return cipherByte;
    }

    public String decryptStr(byte[] buf)
            throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
        return new String(decrypt(buf));
    }

    /**
     * decrypt string
     * @see #encryptStr(String)
     * @param encryptStr
     * @return
     * @throws InvalidKeyException
     * @throws IllegalBlockSizeException
     * @throws BadPaddingException
     * @throws NullPointerException if encryptStr is null
     */
    public String decryptStr(String encryptStr)
            throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
        if (encryptStr == null) {
            throw new NullPointerException();
        }

        encryptStr = encryptStr.toUpperCase();

        ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
        int len = encryptStr.length();
        for (int i = 0; i < len; i += 2) {
            int b = ((charToByte(encryptStr.charAt(i)) << 4) & 0xff) | charToByte(encryptStr.charAt(i + 1));
            byteOut.write(b);
        }

        return decryptStr(byteOut.toByteArray());
    }

    private byte charToByte(char c) {
        return (byte) "0123456789ABCDEF".indexOf(c);
    }

    private char intToChar(int b) {
        return "0123456789ABCDEF".charAt(b);
    }

    public void clean() {
        cipherByte = null;
    }

    public static SecretKey getSecretKey(String desKey, String algorithm) {
        if (StringUtils.isBlank(desKey)) {
            throw new IllegalArgumentException("?");
        }
        return getSecretKey(Base64.decodeBase64(desKey), algorithm);
    }

    /** 
     * ?
     *  
     * @param desKey 
     * @param algorithm ??? {@link #DES}? {@link #DESEDE}?{@link #AES}?
     *            {@link #BLOWFISH}?{@link #RC2}? {@link #RC4}
     * @return 
     */
    public static SecretKey getSecretKey(byte[] desKey, String algorithm) {
        if (ALG_DES.equalsIgnoreCase(algorithm)) {
            try {
                SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(algorithm);
                DESKeySpec dks = new DESKeySpec(desKey);
                return keyFactory.generateSecret(dks);
            } catch (NoSuchAlgorithmException e) {
                throw new RuntimeException(e.getCause());
            } catch (Exception e) {
                throw new IllegalArgumentException("?", e);
            }
        }

        return new SecretKeySpec(desKey, algorithm);
    }
}