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;
    }

猜你喜欢

转载自blog.csdn.net/qq_35029061/article/details/85028125