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"));// 手机号