字符串加密

Encrypt 加密

创建Encrypt 类

package com.dx.util;

import java.io.UnsupportedEncodingException;

public class Encrypt {
	// 加密方法
	public static String doCrypt(String strOriginal, int s) {
		String strReturn = new String();
		char charTemp;
		int intCrpytion;
		int intRnd;
		intRnd = s;
		StringBuffer strOriToB = new StringBuffer(strOriginal);

		strOriToB = strOriToB.reverse();
		for (int i = 0; i < strOriToB.length(); i++) {
			charTemp = strOriToB.charAt(i);
			intCrpytion = (int) charTemp;
			intCrpytion = intCrpytion ^ (intRnd % 32);
			strReturn += (char) intCrpytion;
		}
		strReturn = strReturn + (char) 1;

		// 返回加密后的字符串
		return strReturn;
	}

	// 加密方法
	public static String doCrypt(String strOriginal) {
		String strReturn = new String();
		char charTemp;
		int intCrpytion;
		int intRnd;
		intRnd = 37;// (int)(100*Math.random()+1);
		StringBuffer strOriToB = new StringBuffer(strOriginal);

		strOriToB = strOriToB.reverse();
		for (int i = 0; i < strOriToB.length(); i++) {
			charTemp = strOriToB.charAt(i);
			intCrpytion = (int) charTemp;
			intCrpytion = intCrpytion ^ (intRnd % 32);
			strReturn += (char) intCrpytion;
		}
		strReturn = strReturn + (char) intRnd;

		// 返回加密后的字符串
		return strReturn;
	}

	// 解密方法
	public static String reCrypt(String strCryptograph) {
		if (strCryptograph == null || "".equals(strCryptograph) || "null".equals(strCryptograph)) {
			return "";
		}
		String strReturn = new String();
		int intTemp;
		int intCrypt;
		String strTemp = new String();
		intTemp = (int) strCryptograph.charAt(strCryptograph.length() - 1);
		strTemp = strCryptograph.substring(0, strCryptograph.length() - 1);
		for (int i = 0; i < strTemp.length(); i++) {
			intCrypt = (int) strTemp.charAt(i);
			intCrypt = intCrypt ^ (intTemp % 32);
			strReturn += (char) intCrypt;
		}
		StringBuffer strRe = new StringBuffer(strReturn);
		strRe = strRe.reverse();
		strReturn = new String(strRe);
		return strReturn;
	}

	/**
	 * 将字符串转成URLEncoder
	 * 
	 * @param str
	 *            待转字符串
	 * @return URLEncoder字符串
	 */
	public String convert(String str) {
		String restr;
		try {
			restr = java.net.URLEncoder.encode(str, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			restr = "";
		}
		return (restr);
	}

	/**
	 * 将unicode 字符串
	 * 
	 * @param str
	 *            待转字符串
	 * @return 普通字符串
	 */
	public String revert(String str) {
		String restr = "";
		try {
			restr = java.net.URLDecoder.decode(str, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			restr = "";
		}
		return restr;
	}
}

调用示例

//加密调用
String mobileCrypt = Encrypt.doCrypt("15937165616");
//解密调用
String mobile = Encrypt .reCrypt(.revert("34303426%3C04%25"));// 手机号
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值