java实现简单加密<工具类>

base64


import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.charset.Charset;

/**
 * base64解析 工具类
 */
public class HttpBase64Utils {

    private final static BASE64Decoder decoder = new BASE64Decoder();
    private final static BASE64Encoder encoder = new BASE64Encoder();

    /**
     * 解密
     *
     * @param
     * @return
     */
    public static InputStream decoderInputStream(InputStream inputStream) throws Exception {
        try {
            byte[] bytes = decoder.decodeBuffer(inputStream);
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
            return byteArrayInputStream;
        } catch (Exception e) {
            throw new Exception("数据解密异常!");
        }
    }

    /**
     * 解密
     *
     * @param
     * @return
     */
    public static InputStream decoderOfByte(byte[] bytes) throws Exception {
        try {
            byte[] bytes1 = decoder.decodeBuffer(new String(bytes, Charset.defaultCharset().name())
                    .replaceAll(" ", "")
                    .replaceAll("\n", ""));
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes1);
            return byteArrayInputStream;
        } catch (Exception e) {
            throw new Exception("数据解密异常!");
        }
    }

    /**
     * 解密 字符
     * @return
     * @throws Exception
     */
    public static String decoder(String buff) throws Exception {
        try {
            byte[] bytes = decoder.decodeBuffer(String.valueOf(buff));
            return new String(bytes);
        } catch (Exception e) {
            throw new Exception("数据解密异常!");
        }
    }

    /**
     * 加密
     *
     * @param
     * @return
     * @throws Exception
     */
    public static String encoder(String conter) throws Exception {
        try {
            byte[] bytes = conter.getBytes(Charset.defaultCharset().name());
            String encode = encoder.encode(bytes);
            return encode;
        } catch (Exception e) {
            throw new Exception("数据加密异常!");
        }
    }

}

MD5

import java.security.MessageDigest;

/**
 * md5加密工具类
 */
public class Md5Utils {

    private static final String hexDigits[] = { "0", "1", "2", "3", "4", "5",
            "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };

    /**
     * 将字节数组转换成16进制字符串
     * @param b
     * @return
     */
    public static String byteArraytoHexString(byte b[]){
        StringBuffer stringBuffer=new StringBuffer();
        for(int i=0;i<b.length;i++){
            stringBuffer.append(byteToHexString(b[i]));
        }
        return stringBuffer.toString();
    }

    /**
     * 字节 转换为 字符串
     * 32为位
     * @param b
     * @return
     */
    private static String byteToHexString(byte b) {
        int flag=b;
        if(flag<0){
            flag+=256;
        }
        int flag1=flag/16;
        int flag2=flag%16;
       return hexDigits[flag1]+hexDigits[flag2];
    }

    /**
     * md5加密
     * @param origin 目标字符串
     * @param charsetname 字符编码
     * @return
     */
    public static String MD5Encode(String origin, String charsetname) {
        String resultString = null;
        try {
            resultString = new String(origin);
            MessageDigest md = MessageDigest.getInstance("MD5");
            if (charsetname == null || "".equals(charsetname))
                resultString = byteArraytoHexString(md.digest(resultString
                        .getBytes()));
            else
                resultString = byteArraytoHexString(md.digest(resultString
                        .getBytes(charsetname)));
        } catch (Exception exception) {
        }
        return resultString;
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值