com.liusoft.dlog4j.upgrade.StringUtils.java Source code

Java tutorial

Introduction

Here is the source code for com.liusoft.dlog4j.upgrade.StringUtils.java

Source

/*
 *  StringUtils.java
 * 
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Library General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *  
 *  Author: Winter Lau
 *  http://dlog4j.sourceforge.net
 *  
 */
package com.liusoft.dlog4j.upgrade;

import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

public class StringUtils extends org.apache.commons.lang.StringUtils {

    /**
     * ?????
     */
    private static final String PASSWORD_CRYPT_KEY = "__jDlog_";
    private final static String DES = "DES";

    /**
     * ????
     * @param email
     * @return
     */
    public static boolean isEmail(String email) {
        if (email == null)
            return false;
        email = email.trim();
        if (email.indexOf(' ') != -1)
            return false;

        int idx = email.indexOf('@');
        if (idx == 0 || (idx + 1) == email.length())
            return false;
        if (email.indexOf('@', idx + 1) != -1)
            return false;
        return true;
        /*
        Pattern emailer;
        if(emailer==null){
           String check = "^([a-z0-9A-Z]+[-|\\._]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
           emailer = Pattern.compile(check);
        }
         Matcher matcher = emailer.matcher(email);
         return matcher.matches();
        */
    }

    /**
     * 
     * @param src ??
     * @param key 8?
     * @return     ??
     * @throws Exception
     */
    public static byte[] encrypt(byte[] src, byte[] key) throws Exception {
        //      DES????
        SecureRandom sr = new SecureRandom();
        // ?DESKeySpec
        DESKeySpec dks = new DESKeySpec(key);
        // ?DESKeySpec??
        // SecretKey
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
        SecretKey securekey = keyFactory.generateSecret(dks);
        // Cipher??
        Cipher cipher = Cipher.getInstance(DES);
        // ?Cipher
        cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);
        // ??
        // ??
        return cipher.doFinal(src);
    }

    /**
     * 
     * @param src   ??
     * @param key   8?
     * @return      ??
     * @throws Exception
     */
    public static byte[] decrypt(byte[] src, byte[] key) throws Exception {
        //      DES????
        SecureRandom sr = new SecureRandom();
        // ?DESKeySpec
        DESKeySpec dks = new DESKeySpec(key);
        // ?DESKeySpec??
        // SecretKey
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
        SecretKey securekey = keyFactory.generateSecret(dks);
        // Cipher??
        Cipher cipher = Cipher.getInstance(DES);
        // ?Cipher
        cipher.init(Cipher.DECRYPT_MODE, securekey, sr);
        // ??
        // ??
        return cipher.doFinal(src);
    }

    /**
     * ?
     * @param data
     * @return
     * @throws Exception
     */
    public final static String decrypt(String data) {
        try {
            return new String(decrypt(hex2byte(data.getBytes()), PASSWORD_CRYPT_KEY.getBytes()));
        } catch (Exception e) {
        }
        return null;
    }

    /**
     * ?
     * @param password
     * @return
     * @throws Exception
     */
    public final static String encrypt(String password) {
        try {
            return byte2hex(encrypt(password.getBytes(), PASSWORD_CRYPT_KEY.getBytes()));
        } catch (Exception e) {
        }
        return null;
    }

    /**
     * 
     * @param b
     * @return
     */
    public static String byte2hex(byte[] b) {
        String hs = "";
        String stmp = "";
        for (int n = 0; n < b.length; n++) {
            stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
            if (stmp.length() == 1)
                hs = hs + "0" + stmp;
            else
                hs = hs + stmp;
        }
        return hs.toUpperCase();
    }

    public static byte[] hex2byte(byte[] b) {
        if ((b.length % 2) != 0)
            throw new IllegalArgumentException("??");
        byte[] b2 = new byte[b.length / 2];
        for (int n = 0; n < b.length; n += 2) {
            String item = new String(b, n, 2);
            b2[n / 2] = (byte) Integer.parseInt(item, 16);
        }
        return b2;
    }

    /**
     * ??
     * @param str
     * @param src
     * @param obj
     * @return
     */
    public static String replaceIgnoreCase(String str, String src, String obj) {
        String l_str = str.toLowerCase();
        String l_src = src.toLowerCase();
        int fromIdx = 0;
        StringBuffer result = new StringBuffer();
        do {
            int idx = l_str.indexOf(l_src, fromIdx);
            if (idx == -1)
                break;
            result.append(str.substring(fromIdx, idx));
            result.append(obj);
            fromIdx = idx + src.length();
        } while (true);
        result.append(str.substring(fromIdx));
        return result.toString();
    }

    public static void main(String[] args) {
        String pwd = decrypt("B5947D180FF18B990EB65C9107FBC8FB");
        System.out.println("pwd=" + pwd);

    }
}