package com.hx.util;
|
|
import java.util.*;
|
|
/***
|
* �?单的加密程序,和解密程序
|
*
|
* @author mq.c
|
*
|
*/
|
public class SimpleEncrypt {
|
|
/**
|
* 获取指定长度随机的字符串
|
*
|
* @param getNum
|
* @return
|
*/
|
private static String generateCheckCode(Integer getNum) {
|
// abcdefghijklmnopqrstuvwxyz
|
// String chars =
|
// "123456789.AB2CD3EF7G;HI5JK5LMN/OPQR8ST.UVW7X9YZ?~!@#$%^&*()_+{}:>?<|/.,';][=-";
|
// String chars =
|
// "123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz~!@#$%^&*()_+{}|:<>?/.,';][\\=-";
|
String chars = "123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789abcdefghijklmnopqrstuvwxyz._?";
|
String[] charsx = chars.split("");
|
// String[] charsx = chars.split("");
|
List<String> list = Arrays.asList(charsx);
|
Collections.shuffle(list);
|
StringBuffer buff = new StringBuffer();
|
Random random = new Random();
|
for (int i = 0; i < getNum; i++) {
|
int rand = random.nextInt(list.size());
|
// buff.append(chars.charAt(rand));
|
buff.append(list.get(rand));
|
}
|
return buff.toString();
|
}
|
|
/**
|
* 对字符串进行简易加密(不要在意那种很容易被人破解的小细节)
|
* 该加密采用"前面13位随机码"+dataString+"后9位随机码",组拼成一个字符串,进行BASE64加密得出的加密值
|
*
|
* @param dataString
|
* 要进行加密的数据源
|
* @return
|
*/
|
public static String simpleEnCode(String dataString) {
|
// 组拼随机值
|
String ranString = generateCheckCode(13).toString() + dataString + generateCheckCode(9).toString();
|
String enCode = new String(Base64.getEncoder().encode(ranString.getBytes()));
|
return enCode;
|
}
|
|
/***
|
* 对用com.bang.units.SimpleEncrypt.simpleEnCode加密过的字符串进行解密
|
*
|
* 该加密采用"前面13位随机码"+dataString+"后9位随机码",组拼成一个字符串,进行BASE64加密得出的加密值
|
*
|
* @param dataString
|
* 要进行解密的数据源
|
* @return
|
*/
|
public static String simpleDeCode(String dataString) throws Exception {
|
// 进行解密sun.misc.BASE64Decoder.BASE64Decoder()
|
|
byte[] enCode = Base64.getDecoder().decode(dataString);
|
// 解得随机值
|
String ranString = new String(enCode);
|
|
// System.out.println("simpleDeCode:" + ranString);
|
// 截取出有效位置参数
|
String daString = ranString.substring(13, ranString.length() - 9);
|
|
return daString;
|
}
|
|
public static void main(String[] args) throws Exception {
|
String chars = "123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ?~!@#$%^&*()_+{}:>?<|/.,';][=-`";
|
String[] charsx = chars.split("");
|
List<String> list = Arrays.asList(charsx);
|
Collections.shuffle(list);
|
// for (String string : list) {
|
// System.out.println(string);
|
// }
|
|
String str = "15648sadf";
|
String eStr = simpleEnCode(str);
|
System.out.println(eStr);
|
String dStr = simpleDeCode(eStr);
|
System.out.println(dStr);
|
|
}
|
|
}
|