club.jmint.crossing.specs.Security.java Source code

Java tutorial

Introduction

Here is the source code for club.jmint.crossing.specs.Security.java

Source

/*
 * Copyright 2016 The Crossing Project
 *
 * The Crossing Project licenses this file to you 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 club.jmint.crossing.specs;

import java.security.MessageDigest;
import java.security.SecureRandom;

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

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

import club.jmint.crossing.runtime.ErrorCode;
import club.jmint.crossing.utils.CrossLog;

/**
 * @author shc
 *
 */
public class Security {

    public static String MD5(String str) {
        char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
        try {
            byte[] btInput = str.getBytes();
            MessageDigest mdInst = MessageDigest.getInstance("MD5");
            mdInst.update(btInput);
            byte[] md = mdInst.digest();

            int j = md.length;
            char mdstr[] = new char[j * 2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte byte0 = md[i];
                mdstr[k++] = hexDigits[byte0 >>> 4 & 0xf];
                mdstr[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(mdstr);
        } catch (Exception e) {
            CrossLog.printStackTrace(e);
            return null;
        }
    }

    public static String crossingDecrypt(String encrypt, String dekey, String m) throws CrossException {
        String decryptData = desDecrypt(encrypt, dekey);
        return decryptData;
    }

    public static String crossingEncrypt(String plain, String enkey, String m) throws CrossException {
        String encryptData = desEncrypt(plain, enkey);
        return encryptData;
    }

    public static String crossingSign(String source, String signKey, String m) {
        String signValue = MD5(source + signKey);
        return signValue;
    }

    //??/?/? 
    //DES??-->>ECB????CBC??CFB????OFB???
    public static final String CIPHER_DES_ALGORITHM = "DES/ECB/PKCS5Padding";

    public static String desEncrypt(String data, String key) throws CrossException {
        String ret = null;
        try {
            DESKeySpec desKey = new DESKeySpec(key.getBytes("UTF-8"));
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey securekey = keyFactory.generateSecret(desKey);

            Cipher cipher = Cipher.getInstance(CIPHER_DES_ALGORITHM);
            SecureRandom random = new SecureRandom();
            cipher.init(Cipher.ENCRYPT_MODE, securekey, random);
            byte[] results = cipher.doFinal(data.getBytes("UTF-8"));
            ret = Base64.encodeBase64String(results);
        } catch (Exception e) {
            CrossLog.printStackTrace(e);
            throw new CrossException(ErrorCode.COMMON_ERR_ENCRYPTION.getCode(),
                    ErrorCode.COMMON_ERR_ENCRYPTION.getInfo());
        }
        return ret;
    }

    public static String desDecrypt(String data, String key) throws CrossException {
        String ret = null;
        try {
            DESKeySpec desKey = new DESKeySpec(key.getBytes("UTF-8"));
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey securekey = keyFactory.generateSecret(desKey);

            Cipher cipher = Cipher.getInstance(CIPHER_DES_ALGORITHM);
            cipher.init(Cipher.DECRYPT_MODE, securekey);

            ret = new String(cipher.doFinal(Base64.decodeBase64(data)));
        } catch (Exception e) {
            CrossLog.printStackTrace(e);
            throw new CrossException(ErrorCode.COMMON_ERR_DECRYPTION.getCode(),
                    ErrorCode.COMMON_ERR_DECRYPTION.getInfo());
        }
        return ret;
    }

    //    public static void main(String[] args) throws Exception {
    //        String source = "amigoxiejkdajfkljnvcxv.alfdajlsfhiqeporueqwjk;dasfjkadjsfkdajfkdajsfkdsajkfjjkj";
    //        System.out.println(": " + source);
    //        String key = "ILOVEYOu";
    //        String encryptData = desEncrypt(source, key);
    //        System.out.println("?: " + encryptData);
    //        String decryptData = desDecrypt(encryptData, key);
    //        System.out.println("?: " + decryptData);
    //        System.out.println(source.equals(decryptData));
    //    }

}