加密解密工具类

 

以下工具类,实现了根据密钥进行加密解密的工具。

提供了两个静态方法,encode是加密,decode是解密。非常方便解决我们的问题。

package com.cheng2839.utils;


import org.apache.commons.codec.CharEncoding;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.SecureRandom;

/**
 * 加密解密工具类
 */
public class EncryptUtil {

    private static final String DES = "DES";
    
    private static  final String ENCRYPT_KEY = "cheng2839@com_$";

    private static String keyGenerator(String res,String algorithm,String key,int keysize,boolean isEncode){
        try {
            KeyGenerator kg = KeyGenerator.getInstance(algorithm);
            if (keysize == 0) {
                byte[] keyBytes = key.getBytes(CharEncoding.UTF_8);
                kg.init(new SecureRandom(keyBytes));
            }else if (key==null) {
                kg.init(keysize);
            }else {
                byte[] keyBytes = key.getBytes(CharEncoding.UTF_8);
                kg.init(keysize, new SecureRandom(keyBytes));
            }
            SecretKey sk = kg.generateKey();
            SecretKeySpec sks = new SecretKeySpec(sk.getEncoded(), algorithm);
            Cipher cipher = Cipher.getInstance(algorithm);
            if (isEncode) {
                cipher.init(Cipher.ENCRYPT_MODE, sks);
                byte[] resBytes = res.getBytes(CharEncoding.UTF_8);
                return parseByte2HexStr(cipher.doFinal(resBytes));
            }else {
                cipher.init(Cipher.DECRYPT_MODE, sks);
                return new String(cipher.doFinal(parseHexStr2Byte(res)));
            }
        } catch (Exception e) {
        }
        return null;
    }

    private static String parseByte2HexStr(byte buf[]) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < buf.length; i++) {
            String hex = Integer.toHexString(buf[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            sb.append(hex.toUpperCase());
        }
        return sb.toString();
    }


    private static byte[] parseHexStr2Byte(String hexStr) {
        if (hexStr.length() < 1)
            return null;
        byte[] result = new byte[hexStr.length()/2];
        for (int i = 0;i< hexStr.length()/2; i++) {
            int high = Integer.parseInt(hexStr.substring(i*2, i*2+1), 16);
            int low = Integer.parseInt(hexStr.substring(i*2+1, i*2+2), 16);
            result[i] = (byte) (high * 16 + low);
        }
        return result;
    }

    /**
     * 加密
     * @param source
     * @return
     */
    public static String encode(String source) {
        return keyGenerator(source, DES, ENCRYPT_KEY, 0, true);
    }

    /**
     * 解密
     * @param cipherCode
     * @return
     */
    public static String decode(String cipherCode) {
        return keyGenerator(cipherCode, DES, ENCRYPT_KEY, 0, false);
    }
    
}

 

posted @ 2020-04-08 14:51  温柔的星空,让你感动  阅读(675)  评论(0编辑  收藏  举报