⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 strhelp.java

📁 企业人力资源管理
💻 JAVA
字号:
package com.y2.hr.human.common;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.Array;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class StrHelp {

	/**
	 * 将乱码字符转码
	 * 
	 * @param s
	 * @return
	 */
	public static String toCN(String s) {
		try {
			return new String(s.getBytes("iso-8859-1"), "GBK");
		} catch (UnsupportedEncodingException e) {
			return s;
		}
	}

	/**
	 * 通用验证 主要用于防范SQL注入
	 * 
	 * @param input
	 *            用于验证的值
	 * @return 验证结果,如果为true则通过
	 */
	public static boolean checkInput(String input) {
		boolean result = true;
		if (input.indexOf(";insert into") > 0 || input.indexOf(";update") > 0
				|| input.indexOf(";delete from") > 0
				|| input.indexOf(";drop table") > 0
				|| input.indexOf(";turncate table") > 0
				|| input.indexOf(";exec") > 0) {
			result = false;
		}
		return result;
	}

	/**
	 * 自定义加密字符串 加密规则:转化为字符数组,字符取ASC码值双位减2,单位加3,再反转返回
	 * 可自行设定加密规则
	 * @param str
	 * @return
	 */
	public static String encrypt(String str) {
		StringBuffer temp = new StringBuffer();
		char[] ch = str.toCharArray();
		for (int i = 0; i < ch.length; i++) {
			int a = (int) ch[i];
			if (i % 2 == 0) {
				a += 3;
			} else {
				a -= 2;
			}
			ch[i] = (char) a;
		}
		for (int i = 0; i < ch.length; i++) {
			char charTemp = ch[ch.length - 1 - i];
			temp.append(charTemp);
		}
		return temp.toString();
	}

	/**
	 * 解密自定义加密字符串
	 * 
	 * @param str
	 * @return
	 */
	public static String unencrypt(String str) {
		StringBuffer temp = new StringBuffer();
		char[] ch = str.toCharArray();
		for (int i = 0; i < ch.length; i++) {
			int a = (int) ch[i];
			if (i % 2 == 0) {
				a += 2;
			} else {
				a -= 3;
			}
			ch[i] = (char) a;
		}
		for (int i = 0; i < ch.length; i++) {
			char charTemp = ch[ch.length - 1 - i];
			temp.append(charTemp);
		}
		return temp.toString();
	}
	
	/**
	 * 替换文本域中的回车符为换行符,再输出至页面
	 * @param str
	 * @return
	 */
	public static String htmlEncode(String str) {
		return str.replace("\n", "<br/>");
	}

	public static void main(String[] args) {
		System.out.println(encrypt("abcd"));
		System.out.println(unencrypt(encrypt("abcd")));
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -