密码学-AES-算法-Java工具类实现


    最近在项目中使用到了AES算法,对AES算法在Java中的使用,我编写了一个工具类。

Java 的 AES加解码主要支持以下的几种模式与填充方式:


算法/模式/填充                 字节加密后数据长度       不满16字节加密后长度  
AES/CBC/NoPadding                   16                          不支持  
AES/CBC/PKCS5Padding                32                          16  
AES/CBC/ISO10126Padding             32                          16  
AES/CFB/NoPadding                   16                          原始数据长度  
AES/CFB/PKCS5Padding                32                          16  
AES/CFB/ISO10126Padding             32                          16  
AES/ECB/NoPadding                   16                          不支持  
AES/ECB/PKCS5Padding                32                          16  
AES/ECB/ISO10126Padding             32                          16  
AES/OFB/NoPadding                   16                          原始数据长度  
AES/OFB/PKCS5Padding                32                          16  
AES/OFB/ISO10126Padding             32                          16  
AES/PCBC/NoPadding                  16                          不支持  
AES/PCBC/PKCS5Padding               32                          16  
AES/PCBC/ISO10126Padding            32                          16


现整理如下:

为了使得代码更规范,我将AES解码部分分为了4个类,

如下图所示:



AESEncryptUtil       加解密的核心模块,内含示例代码

AESType                 表明使用的密钥长度 128 , 192, 256

EncodeType           AES几种模式的加解密类型

TypeConvert          工具类,完成字符转换等工作


注意在AESEncryptUtil , 我并没有使用随机源,目前我不太清楚随机源是用来做什么的。。。

希望知道的同学踊跃留言,有红包奖励




AESEncryptUtil       加解密的核心模块,内含示例代码

package com.yaobaling.spark.clickcube.encrypt.aes;

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

import javax.crypto.*;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;


/**
 * Created by szh on 2018/7/10.
 *
 * @author szh
 * @date 2018/7/10
 */
public class AESEncryptUtil {

    public static final String CHARSET = "UTF-8";

    /**
     * @param isEncrypt
     * @param source
     * @param key
     * @param iv
     * @param type
     * @param encodeType
     * @return
     * @throws UnsupportedEncodingException
     * @throws InvalidAlgorithmParameterException
     * @throws NoSuchAlgorithmException
     * @throws NoSuchPaddingException
     * @throws BadPaddingException
     * @throws IllegalBlockSizeException
     * @throws InvalidKeyException
     */
    public static byte[] encryptOrDecrypt(boolean isEncrypt, byte[] source, byte[] key, byte[] iv, AESType type, String encodeType) throws UnsupportedEncodingException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchPaddingException, BadPaddingException, IllegalBlockSizeException, InvalidKeyException {
        if (isEncrypt) {
            byte[] encodeByte = encryptOrDecrypt(Cipher.ENCRYPT_MODE, source, key, iv, type, encodeType);

            //TODO 测试代码
            //String encodeStr = TypeConvert.bytesToHexString(encodeByte);
            return encodeByte;
        } else {
            byte[] decodeByte = encryptOrDecrypt(Cipher.DECRYPT_MODE, source, key, iv, type, encodeType);

            //TODO 测试代码
            //String decodeStr = new String(decodeByte, CHARSET);

            return decodeByte;
        }
    }


    private static byte[] encryptOrDecrypt(int mode, byte[] byteContent, byte[] key, byte[] iv, AESType type, String modeAndPadding) throws InvalidKeyException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, NoSuchPaddingException, BadPaddingException, IllegalBlockSizeException {

//        // *****  对于是否存在1,2,3,4 步奏还存在疑惑
//        //1.构造密钥生成器,指定为AES算法,不区分大小写
//        KeyGenerator keygen=KeyGenerator.getInstance("AES");
//        //2.根据ecnodeRules规则初始化密钥生成器
//        //生成一个指定位的随机源,根据传入的字节数组
//        keygen.init(type.getValue(), new SecureRandom(key));
//        //3.产生原始对称密钥
//        SecretKey secretKey=keygen.generateKey();
//        //4.获得原始对称密钥的字节数组
//        byte [] raw = secretKey.getEncoded();

       //5.根据字节数组生成AES密钥
        SecretKey keySpec=new SecretKeySpec(key, "AES");

        //6.根据指定算法AES自成密码器
        Cipher cipher = Cipher.getInstance(modeAndPadding);// 创建密码器

        if (null != iv) {
            //指定一个初始化向量 (Initialization vector,IV), IV 必须是16位
            cipher.init(mode, keySpec, new IvParameterSpec(iv));
        } else {
            cipher.init(mode, keySpec);
        }
        byte[] result = cipher.doFinal(byteContent);
        return result;
    }


    /**
     * 指定一个初始化向量 (Initialization vector,IV),IV 必须是16位
     */
    public static final byte[] getIV() throws Exception {
        return "1234567812345678".getBytes(CHARSET);
    }


    public static void main(String[] args) throws Exception {

//        String content = "在线助手";
//        // 生成密钥需要的密码值
//        String key = "www.it399.com";
//        byte[] encrypt;
//        byte[] decrypt;
//
//        encrypt = encryptOrDecrypt(true,content.getBytes(CHARSET),key,null,AESType.AES_128,EncodeType.AES_DEFAULT);
//
//        System.out.println(new String(encrypt));
//
//        decrypt = encryptOrDecrypt(false,encrypt,key,null,AESType.AES_128,EncodeType.AES_DEFAULT);
//
//        System.out.println(new String(decrypt));



//        String content = "56.2";
//        String key = "0123456789abcdef";
//        byte[] encrypt = encryptOrDecrypt(true, content.getBytes("utf-8"), key.getBytes("utf-8"), null, AESType.AES_128, EncodeType.AES_ECB_PKCS5Padding);
//        //String line = Base64EncryptUtil.safeUrlBase64Encode(encrypt);
//        String line = Base64.encodeBase64URLSafeString(encrypt);
//        System.out.println(line);

        String content = "56.2";
        String key = "91efa40df1a5e576";
        byte[] encrypt = encryptOrDecrypt(true, content.getBytes("utf-8"), key.getBytes("utf-8"), null, AESType.AES_128, EncodeType.AES_ECB_PKCS5Padding);
        //String line = Base64EncryptUtil.safeUrlBase64Encode(encrypt);
        String line = Base64.encodeBase64URLSafeString(encrypt);
        System.out.println(line);

//
//
//        String content2 = "sjaY7IUVQaSgu7IidixBPw";
//        String key2 = "0123456789abcdef";
//        byte[] urlDecode = Base64EncryptUtil.safeUrlBase64Decode(content2);
//        byte[] decrypt = encryptOrDecrypt(false,urlDecode,key2,null,AESType.AES_128,EncodeType.AES_ECB_PKCS5Padding);
//        System.out.println(new String(decrypt));


//        String content = "在线助手";
//        // 生成密钥需要的密码值
//        String key = "www.it399.com";
//        byte[] encrypt;
//
//
//        /**
//         * AES加密方式一:AES不指定模式和填充,默认为 ECB/PKCS5Padding
//         */
//        System.out.println("【0】AES不指定模式和填充,默认为 ECB/PKCS5Padding,输入可以不是16字节,也不需要填充向量\n");
//        //128
//        encrypt = encryptOrDecrypt(true,content.getBytes(CHARSET),key,null,AESType.AES_128,EncodeType.AES_DEFAULT);
//        encryptOrDecrypt(false,encrypt,key,null,AESType.AES_128,EncodeType.AES_DEFAULT);
//        //192
//        encrypt = encryptOrDecrypt(true,content.getBytes(CHARSET),key,getIV(),AESType.AES_192,EncodeType.AES_DEFAULT);
//        encryptOrDecrypt(false,encrypt,key,getIV(),AESType.AES_192,EncodeType.AES_DEFAULT);
//        //256
//        encrypt = encryptOrDecrypt(true,content.getBytes(CHARSET),key,null,AESType.AES_256,EncodeType.AES_DEFAULT);
//        encryptOrDecrypt(false,encrypt,key,null,AESType.AES_256,EncodeType.AES_DEFAULT);


//        /**
//         * 1.1 AES/CBC (需要填充向量16*n)
//         * AES/CBC/NoPadding
//         * AES/CBC/PKCS5Padding
//         * AES/CBC/ISO10126Padding
//         */
//        System.out.println("【1.1】AES_CBC_NoPadding模式,需要填充向量,待加密必须是16*n");
//        content = "在线助手在线助手在线助手在线助手";
//        key = "www.it399.com";
//        encrypt = encryptOrDecrypt(true,content.getBytes(CHARSET),key,getIV(),AESType.AES_128,EncodeType.AES_CBC_NoPadding);
//        encryptOrDecrypt(false,encrypt,key,getIV(),AESType.AES_128,EncodeType.AES_CBC_NoPadding);
//        encrypt = encryptOrDecrypt(true,content.getBytes(CHARSET),key,getIV(),AESType.AES_192,EncodeType.AES_CBC_PKCS5Padding);
//        encryptOrDecrypt(false,encrypt,key,getIV(),AESType.AES_192,EncodeType.AES_CBC_PKCS5Padding);
//        encrypt = encryptOrDecrypt(true,content.getBytes(CHARSET),key,getIV(),AESType.AES_256,EncodeType.AES_CBC_ISO10126Padding);
//        encryptOrDecrypt(false,encrypt,key,getIV(),AESType.AES_256,EncodeType.AES_CBC_ISO10126Padding);


//        /**
//         * 1.2 AES/CFB
//         * AES/CBC/NoPadding
//         * AES/CBC/PKCS5Padding
//         * AES/CBC/ISO10126Padding
//         */
//        System.out.println("【1.2】AES_CFB_NoPadding模式\n");
//        content = "在线助手";
//        // 生成密钥需要的密码值
//        key = "http://www.it399.com";
//        encrypt = encryptOrDecrypt(true,content.getBytes(CHARSET),key,getIV(),AESType.AES_128,EncodeType.AES_CFB_PKCS5Padding);
//        encryptOrDecrypt(false,encrypt,key,getIV(),AESType.AES_128,EncodeType.AES_CFB_PKCS5Padding);
//        encrypt = encryptOrDecrypt(true,content.getBytes(CHARSET),key,getIV(),AESType.AES_192,EncodeType.AES_CFB_PKCS5Padding);
//        encryptOrDecrypt(false,encrypt,key,getIV(),AESType.AES_192,EncodeType.AES_CFB_PKCS5Padding);
//        encrypt = encryptOrDecrypt(true,content.getBytes(CHARSET),key,getIV(),AESType.AES_256,EncodeType.AES_CFB_PKCS5Padding);
//        encryptOrDecrypt(false,encrypt,key,getIV(),AESType.AES_256,EncodeType.AES_CFB_PKCS5Padding);


//        /**
//         * 1.2 AES/ECB
//         * AES/ECB/NoPadding
//         * AES/ECB/PKCS5Padding
//         * AES/ECB/ISO10126Padding
//         */
//        System.out.println("【1.3】AES_ECB模式");
//        content = "在线助手";
//        // 生成密钥需要的密码值
//        key = "http://www.it399.com";
//        encrypt = encryptOrDecrypt(true,content.getBytes(CHARSET),key,null,AESType.AES_128,EncodeType.AES_ECB_PKCS5Padding);
//        encryptOrDecrypt(false,encrypt,key,null,AESType.AES_128,EncodeType.AES_ECB_PKCS5Padding);
//        encrypt = encryptOrDecrypt(true,content.getBytes(CHARSET),key,null,AESType.AES_192,EncodeType.AES_ECB_PKCS5Padding);
//        encryptOrDecrypt(false,encrypt,key,null,AESType.AES_192,EncodeType.AES_ECB_PKCS5Padding);
//        encrypt = encryptOrDecrypt(true,content.getBytes(CHARSET),key,null,AESType.AES_256,EncodeType.AES_ECB_PKCS5Padding);
//        encryptOrDecrypt(false,encrypt,key,null,AESType.AES_256,EncodeType.AES_ECB_PKCS5Padding);


//        /**
//         * 1.4 AES/OFB
//         * AES/OFB/NoPadding
//         * AES/OFB/PKCS5Padding
//         * AES/OFB/ISO10126Padding
//         */
//        System.out.println("【1.4】AES_OFB模式");
//        content = "在线助手";
//        // 生成密钥需要的密码值
//        key = "http://www.it399.com";
//        encrypt = encryptOrDecrypt(true, content.getBytes(CHARSET), key, getIV(), AESType.AES_128, EncodeType.AES_OFB_PKCS5Padding);
//        encryptOrDecrypt(false, encrypt, key, getIV(), AESType.AES_128, EncodeType.AES_OFB_PKCS5Padding);
//        encrypt = encryptOrDecrypt(true, content.getBytes(CHARSET), key, getIV(), AESType.AES_192, EncodeType.AES_OFB_PKCS5Padding);
//        encryptOrDecrypt(false, encrypt, key, getIV(), AESType.AES_192, EncodeType.AES_OFB_PKCS5Padding);
//        encrypt = encryptOrDecrypt(true, content.getBytes(CHARSET), key, getIV(), AESType.AES_256, EncodeType.AES_OFB_PKCS5Padding);
//        encryptOrDecrypt(false, encrypt, key, getIV(), AESType.AES_256, EncodeType.AES_OFB_PKCS5Padding);


//        /**
//         * 1.5 AES/PCBC
//         * AES/PCBC/NoPadding
//         * AES/PCBC/PKCS5Padding
//         * AES/PCBC/ISO10126Padding
//         */
//        System.out.println("【1.5】AES_PCBC模式");
//        content = "在线助手";
//        // 生成密钥需要的密码值
//        key = "http://www.it399.com";
//        encrypt = encryptOrDecrypt(true, content.getBytes(CHARSET), key, getIV(), AESType.AES_128, EncodeType.AES_PCBC_PKCS5Padding);
//        encryptOrDecrypt(false, encrypt, key, getIV(), AESType.AES_128, EncodeType.AES_PCBC_PKCS5Padding);
//        encrypt = encryptOrDecrypt(true, content.getBytes(CHARSET), key, getIV(), AESType.AES_192, EncodeType.AES_PCBC_PKCS5Padding);
//        encryptOrDecrypt(false, encrypt, key, getIV(), AESType.AES_192, EncodeType.AES_PCBC_PKCS5Padding);
//        encrypt = encryptOrDecrypt(true, content.getBytes(CHARSET), key, getIV(), AESType.AES_256, EncodeType.AES_PCBC_PKCS5Padding);
//        encryptOrDecrypt(false, encrypt, key, getIV(), AESType.AES_256, EncodeType.AES_PCBC_PKCS5Padding);


//        /**1.3 AES/CBC
//         * AES_CBC_NoPadding模式(填充向量可选)
//         */
//        System.out.println("【1.3】AES_CBC_NoPadding模式");
//        content = "在线助手在线助手在线助手在线助手";
//        // 生成密钥需要的密码值
//        key = "www.it399.com";
//        encrypt = encryptOrDecrypt(true, content.getBytes(CHARSET), key, getIV(), AESType.AES_128, EncodeType.AES_CBC_NoPadding);
//        encryptOrDecrypt(false, encrypt, key, getIV(), AESType.AES_128, EncodeType.AES_CBC_NoPadding);
//        content = "在线助手";
//        // 生成密钥需要的密码值
//        key = "www.it399.com";
//        encrypt = encryptOrDecrypt(true, content.getBytes(CHARSET), key, getIV(), AESType.AES_192, EncodeType.AES_CBC_PKCS5Padding);
//        encryptOrDecrypt(false, encrypt, key, getIV(), AESType.AES_192, EncodeType.AES_CBC_PKCS5Padding);
//        encrypt = encryptOrDecrypt(true, content.getBytes(CHARSET), key, getIV(), AESType.AES_256, EncodeType.AES_CBC_ISO10126Padding);
//        encryptOrDecrypt(false, encrypt, key, getIV(), AESType.AES_256, EncodeType.AES_CBC_ISO10126Padding);


//        /**
//         * 2.1 AES/CFB 128/192/256位加解密
//         * AES_CFB_NoPadding模式(填充向量可选)
//         */
//        System.out.println("【2.1】AES_CFB_NoPadding模式,需要填充向量\n");
//        content = "在线助手";
//        // 生成密钥需要的密码值
//        key = "www.it399.com";
//        encrypt = encryptOrDecrypt(true,content.getBytes(CHARSET),key,null,AESType.AES_128,EncodeType.AES_CFB_NoPadding);
//        encryptOrDecrypt(false,encrypt,key,null,AESType.AES_128,EncodeType.AES_CFB_NoPadding);
//        encrypt = encryptOrDecrypt(true,content.getBytes(CHARSET),key,null,AESType.AES_192,EncodeType.AES_CFB_NoPadding);
//        encryptOrDecrypt(false,encrypt,key,null,AESType.AES_192,EncodeType.AES_CFB_NoPadding);
//        encrypt = encryptOrDecrypt(true,content.getBytes(CHARSET),key,null,AESType.AES_256,EncodeType.AES_CFB_NoPadding);
//        encryptOrDecrypt(false,encrypt,key,null,AESType.AES_256,EncodeType.AES_CFB_NoPadding);


//        /**
//         * 2.2 AES/CFB
//         * AES_CFB_NoPadding模式(填充向量可选)
//         */
//        System.out.println("【2.2】AES_CFB_NoPadding模式\n");
//        content = "在线助手";
//        // 生成密钥需要的密码值
//        key = "www.it399.com";
//        encrypt = encryptOrDecrypt(true,content.getBytes(CHARSET),key,null,AESType.AES_128,EncodeType.AES_CFB_PKCS5Padding);
//        encryptOrDecrypt(false,encrypt,key,null,AESType.AES_128,EncodeType.AES_CFB_PKCS5Padding);
//        encrypt = encryptOrDecrypt(true,content.getBytes(CHARSET),key,null,AESType.AES_192,EncodeType.AES_CFB_PKCS5Padding);
//        encryptOrDecrypt(false,encrypt,key,null,AESType.AES_192,EncodeType.AES_CFB_PKCS5Padding);
//        encrypt = encryptOrDecrypt(true,content.getBytes(CHARSET),key,null,AESType.AES_256,EncodeType.AES_CFB_PKCS5Padding);
//        encryptOrDecrypt(false,encrypt,key,null,AESType.AES_256,EncodeType.AES_CFB_PKCS5Padding);


//        /**2.3 AES/CFB
//         * AES_CFB_NoPadding模式(填充向量可选)
//         */
//        System.out.println("【2.3】AES_CFB_NoPadding模式\n");
//        content = "在线助手";
//        // 生成密钥需要的密码值
//        key = "www.it399.com";
//        encrypt = encryptOrDecrypt(true,content.getBytes(CHARSET),key,null,AESType.AES_128,EncodeType.AES_CFB_ISO10126Padding);
//        encryptOrDecrypt(false,encrypt,key,null,AESType.AES_128,EncodeType.AES_CFB_ISO10126Padding);
//        encrypt = encryptOrDecrypt(true,content.getBytes(CHARSET),key,null,AESType.AES_192,EncodeType.AES_CFB_ISO10126Padding);
//        encryptOrDecrypt(false,encrypt,key,null,AESType.AES_192,EncodeType.AES_CFB_ISO10126Padding);
//        encrypt = encryptOrDecrypt(true,content.getBytes(CHARSET),key,null,AESType.AES_256,EncodeType.AES_CFB_ISO10126Padding);
//        encryptOrDecrypt(false,encrypt,key,null,AESType.AES_256,EncodeType.AES_CFB_ISO10126Padding);

    }


}




AESType                 表明使用的密钥长度 128 , 192, 256

package com.yaobaling.spark.clickcube.encrypt.aes;

/**
 * Created by szh on 2018/7/10.
 *
 * @author szh
 * @date 2018/7/10
 */
public enum AESType {

    AES_128(128), AES_192(192), AES_256(256);

    public int value;

    private AESType(int value) {
        this.value = value;
    }

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }
}



EncodeType           AES几种模式的加解密类型

package com.yaobaling.spark.clickcube.encrypt.aes;

/**
 * Created by szh on 2018/7/10.
 *
 * @author szh
 * @date
 */
public class EncodeType {


    //    算法/模式/填充                 16字节加密后数据长度       不满16字节加密后长度
    //    AES/CBC/NoPadding                   16                          不支持
    //    AES/CBC/PKCS5Padding                32                          16
    //    AES/CBC/ISO10126Padding             32                          16
    //    AES/CFB/NoPadding                   16                          原始数据长度
    //    AES/CFB/PKCS5Padding                32                          16
    //    AES/CFB/ISO10126Padding             32                          16
    //    AES/ECB/NoPadding                   16                          不支持
    //    AES/ECB/PKCS5Padding                32                          16
    //    AES/ECB/ISO10126Padding             32                          16
    //    AES/OFB/NoPadding                   16                          原始数据长度
    //    AES/OFB/PKCS5Padding                32                          16
    //    AES/OFB/ISO10126Padding             32                          16
    //    AES/PCBC/NoPadding                  16                          不支持
    //    AES/PCBC/PKCS5Padding               32                          16
    //    AES/PCBC/ISO10126Padding            32                          16

    //默认为 ECB/PKCS5Padding
    public final static String AES_DEFAULT = "AES";
    public final static String AES_CBC_NoPadding = "AES/CBC/NoPadding";
    public final static String AES_CBC_PKCS5Padding = "AES/CBC/PKCS5Padding";
    public final static String AES_CBC_ISO10126Padding = "AES/CBC/ISO10126Padding";
    public final static String AES_CFB_NoPadding = "AES/CFB/NoPadding";
    public final static String AES_CFB_PKCS5Padding = "AES/CFB/PKCS5Padding";
    public final static String AES_CFB_ISO10126Padding = "AES/CFB/ISO10126Padding";
    public final static String AES_ECB_NoPadding = "AES/ECB/NoPadding";
    public final static String AES_ECB_PKCS5Padding = "AES/ECB/PKCS5Padding";
    public final static String AES_ECB_ISO10126Padding = "AES/ECB/ISO10126Padding";
    public final static String AES_OFB_NoPadding = "AES/OFB/NoPadding";
    public final static String AES_OFB_PKCS5Padding = "AES/OFB/PKCS5Padding";
    public final static String AES_OFB_ISO10126Padding = "AES/OFB/ISO10126Padding";
    public final static String AES_PCBC_NoPadding = "AES/PCBC/NoPadding";
    public final static String AES_PCBC_PKCS5Padding = "AES/PCBC/PKCS5Padding";
    public final static String AES_PCBC_ISO10126Padding = "AES/PCBC/ISO10126Padding";

}


TypeConvert          工具类,完成字符转换等工作

package com.yaobaling.spark.clickcube.encrypt.aes;

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

/**
 * Created by szh on 2018/7/10.
 *
 * @author szh
 * @date 2018/7/10
 */
public class TypeConvert {


    /**
     * 字符串转换成十六进制字符串
     */
    public static String str2HexStr(String str) {
        char[] chars = "0123456789ABCDEF".toCharArray();
        StringBuilder sb = new StringBuilder("");
        byte[] bs = str.getBytes();
        int bit;
        for (int i = 0; i < bs.length; i++) {
            bit = (bs[i] & 0x0f0) >> 4;
            sb.append(chars[bit]);
            bit = bs[i] & 0x0f;
            sb.append(chars[bit]);
        }
        return sb.toString();
    }

    /**
     * Convert hex string to byte[]
     *
     * @param hexString the hex string
     * @return byte[]
     */
    public static byte[] hexStringToBytes(String hexString) {
        if (hexString == null || hexString.equals("")) {
            return null;
        }
        hexString = hexString.toUpperCase();
        int length = hexString.length() / 2;
        char[] hexChars = hexString.toCharArray();
        byte[] d = new byte[length];
        for (int i = 0; i < length; i++) {
            int pos = i * 2;
            d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
        }
        return d;
    }

    /**
     * Convert char to byte
     *
     * @param c char
     * @return byte
     */
    private static byte charToByte(char c) {
        return (byte) "0123456789ABCDEF".indexOf(c);
    }


    /**
     * 数组转换成十六进制字符串
     * @param bArray byte[]
     * @return HexString
     */
    public static final String bytesToHexString(byte[] bArray) {
        if (bArray == null || bArray.length==0){
            return null;
        }
        StringBuffer sb = new StringBuffer(bArray.length);
        String sTemp;
        for (int i = 0; i < bArray.length; i++) {
            sTemp = Integer.toHexString(0xFF & bArray[i]);
            if (sTemp.length() < 2){
                sb.append(0);
            }
            sb.append(sTemp.toUpperCase());
        }
        return sb.toString();
    }


    /**
     * 十六进制字符串转换成字符串
     * @param hexStr
     * @return String
     */
    public static String hexStr2Str(String hexStr) {
        String str = "0123456789ABCDEF";
        char[] hexs = hexStr.toCharArray();
        byte[] bytes = new byte[hexStr.length() / 2];
        int n;
        for (int i = 0; i < bytes.length; i++) {
            n = str.indexOf(hexs[2 * i]) * 16;
            n += str.indexOf(hexs[2 * i + 1]);
            bytes[i] = (byte) (n & 0xff);
        }
        return new String(bytes);
    }

    /**
     * @param hexString String str = "000AB"
     * @return
     */
    public static int hexString2Int(String hexString){
        Integer num = Integer.valueOf(hexString,16);
        return num;
    }

    /**
     * 把byte转为字符串的bit
     */
    public static String byteToBitString(byte b) {
        return ""
                + (byte) ((b >> 7) & 0x1) + (byte) ((b >> 6) & 0x1)
                + (byte) ((b >> 5) & 0x1) + (byte) ((b >> 4) & 0x1)
                + (byte) ((b >> 3) & 0x1) + (byte) ((b >> 2) & 0x1)
                + (byte) ((b >> 1) & 0x1) + (byte) ((b >> 0) & 0x1);
    }

    /**
     * 把byte转为字符串数组的bit
     */
    public static String[] byteToBitStrings(byte b) {
        String[] bit = new String[8];
        bit[0] = ""+ (byte) ((b >> 7) & 0x1);
        bit[1] = ""+ (byte) ((b >> 6) & 0x1);
        bit[2] = ""+ (byte) ((b >> 5) & 0x1);
        bit[3] = ""+ (byte) ((b >> 4) & 0x1);
        bit[4] = ""+ (byte) ((b >> 3) & 0x1);
        bit[5] = ""+ (byte) ((b >> 2) & 0x1);
        bit[6] = ""+ (byte) ((b >> 1) & 0x1);
        bit[7] = ""+ (byte) ((b >> 0) & 0x1);
        return bit;
    }

    //base64字符串转byte[]
    public static byte[] base64String2ByteFun(String base64Str){
        return Base64.decodeBase64(base64Str);
    }

    //byte[]转base64
    public static String byte2Base64StringFun(byte[] b){
        return Base64.encodeBase64String(b);
    }

    public static void main(String[] args){
        String hexString = "3A60432A5C01211F291E0F4E0C132825";
        byte[] result = hexStringToBytes(hexString);
        System.out.println(new String(result));
        System.out.println(bytesToHexString(result));
    }


}


展开阅读全文

没有更多推荐了,返回首页