java 签名


public class CommonUtil {

    static {
        try {
            Security.addProvider(new BouncyCastleProvider());
        } catch (Exception e) {
            // System.out.println("load BouncyCastleProvider failed: " + e);
        }
    }


    /**
     * 生成随机数
     *
     * @return int 随机数
     */
    public static int rand() {
        SecureRandom sr = new SecureRandom();
        return sr.nextInt();
    }


    /**
     * BASE64 编码
     *
     * @param data 原始数据
     * @return String 编码后的字符串
     * @throws Exception 字符串转换时可能引发的异常
     */
    public static String encode(byte[] data) throws Exception {
        return new String(Base64.encode(data));
    }


    /**
     * BASE64 解码
     *
     * @param text BASE64编码的数据
     * @return byte[] 解码后的数据
     * @throws Exception 进行BASE64解码时可能产生异常
     */
    public static byte[] decode(String text) throws Exception {
        return Base64.decode(text);
    }


    /**
     * HEX(16进制) 编码
     *
     * @param data 原始数据
     * @return String 编码后的字符串
     * @throws Exception 进行字符串转换时可能产生异常
     */
    public static String hex(byte[] data) throws Exception {
        return new String(Hex.encode(data));
    }


    /**
     * HEX(16进制) 解码
     *
     * @param text HEX字符串
     * @return byte[] 解码后的数据
     * @throws Exception 进行BASE64解码时或者字符串转换时可能产生异常
     */
    public static byte[] unhex(String text) throws Exception {
        return Hex.decode(text.getBytes());
    }


    /**
     * MD5 摘要算法
     *
     * @param data 原始数据
     * @return byte[] 摘要数据
     * @throws Exception 可能抛出参数无效或算法相关的异常
     */
    public static byte[] md5(byte[] data) throws Exception {
        MD5Digest md5 = new MD5Digest();


        int outLen = md5.getDigestSize();
        byte[] out = new byte[outLen];


        md5.update(data, 0, data.length);
        md5.doFinal(out, 0);


        return out;
    }


    /**
     * SHA1 摘要算法
     *
     * @param data 原始数据
     * @return byte[] 摘要数据
     * @throws Exception 可能抛出参数无效或算法相关的异常
     */
    public static byte[] sha1(byte[] data) throws Exception {
        SHA1Digest sha1 = new SHA1Digest();


        int outLen = sha1.getDigestSize();
        byte[] out = new byte[outLen];


        sha1.update(data, 0, data.length);
        sha1.doFinal(out, 0);


        return out;
    }


    /**
     * AES 加密算法
     *
     * @param plainText 明文
     * @param password 密码
     * @return byte[] 密文
     * @throws Exception 数据、算法相关的异常
     */
    public static byte[] aesEncrypt(byte[] plainText, String password) throws Exception {
        KeyGenerator kgen = KeyGenerator.getInstance("AES");
       // kgen.init(128, new SecureRandom(password.getBytes()));
        
        SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG"); 
        secureRandom.setSeed(password.getBytes());  
        kgen.init(128, secureRandom); 
        
        SecretKey secretKey = kgen.generateKey();
        byte[] enCodeFormat = secretKey.getEncoded();
        SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.ENCRYPT_MODE, key);
        return cipher.doFinal(plainText);
    }


    /**
     * AES 解密算法
     *
     * @param cipherData 密文
     * @param password 密码
     * @return byte[] 明文
     * @throws Exception 数据、算法相关的异常
     */
    public static byte[] aesDecrypt(byte[] cipherData, String password) throws Exception {
        KeyGenerator kgen = KeyGenerator.getInstance("AES");
        //kgen.init(128, new SecureRandom(password.getBytes()));
        
        SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG"); 
        secureRandom.setSeed(password.getBytes());  
        kgen.init(128, secureRandom); 
        
        SecretKey secretKey = kgen.generateKey();
        byte[] enCodeFormat = secretKey.getEncoded();
        SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.DECRYPT_MODE, key);
        return cipher.doFinal(cipherData);
    }


    /**
     * 读取文件所有内容
     *
     * @param filename 文件名
     * @return String 文件内容
     * @throws Exception 可能触发文件访问相关的异常
     */
    public static byte[] getFileContents(String filename) throws Exception {
        FileInputStream fis = new FileInputStream(filename);
        int count = fis.available();


        byte[] data = new byte[count];
        fis.read(data);
        fis.close();


        return data;
    }


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值