java的base64与unicode加密

java的base64加密

base64加密方法

    import java.util.Base64;
	public static String getEncodedBase64(String plainText) {
		String encoded = null;
		if(StringUtil.isEmpty(plainText))
		{
			return encoded;
		}
		try {
			byte[] bytes = plainText.getBytes("UTF-8");
			encoded = Base64.getEncoder().encodeToString(bytes);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return encoded;
	}

base64解密方法

public static String getDecodedBase64(String plainText) {
		byte[] decoded = null;
		String data = null;
		if(StringUtil.isEmpty(plainText))
		{
			return data;
		}
		try {
			byte[] bytes = plainText.getBytes("UTF-8");
			decoded = Base64.getDecoder().decode(bytes);
			data = new String(decoded, "UTF-8");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return data;
	}

base64加密具体使用

加密
if(!StringUtil.isEmpty(member.getContact())){
			member.setContact(ComUtils.getEncodedBase64(member.getContact()));
			}

解密
if(!StringUtil.isEmpty(member.getContact())){
				member.setContact(ComUtils.getDecodedBase64(member.getContact()));
			}

java的unicode的加密

加密字符串,进一步提高系统的安全性

   /**
     * @param src 未加密字符串
     * @return String 加密后的字符串
     */
    public static String escapeStr(String src) {
        String ret = "";

        if (src == null) {
            return ret;
        }

        for (int i = src.length() - 1; i >= 0; i--) {
            int iCh = src.substring(i, i + 1).hashCode();

            if (iCh == 10) {
                iCh = 15;
            } else if (iCh == 13) {
                iCh = 16;
            } else if (iCh == 32) {
                iCh = 17;
            } else if (iCh == 9) {
                iCh = 18;
            } else {
                iCh = iCh + 5;
            }

            ret += (char) iCh;
        }
        return ret;
    }

java的unicode的解密

   对应解开客户端进行简单加密的字符串,进一步提高系统的安全性
   原理:对应客户端加密的字符串进行拆解,转为Unicode对应的数字,对每一个数字进行恢复的反向调整。

    /**
     * @param src 原加密字符串
     * @return String 解密后的字符串
     */
    public static String unEscapeStr(String src) {
        String ret = "";

        if (src == null) {
            return ret;
        }

        for (int i = src.length() - 1; i >= 0; i--) {
            int iCh = src.substring(i, i + 1).hashCode();

            if (iCh == 15) {
                iCh = 10;
            } else if (iCh == 16) {
                iCh = 13;
            } else if (iCh == 17) {
                iCh = 32;
            } else if (iCh == 18) {
                iCh = 9;
            } else {
                iCh = iCh - 5;
            }

            ret += (char) iCh;
        }
        return ret;
    }

 

 

 

 

 

 

 

 

 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wespten

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值