package cn.com.yunguangche.common;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;
public class TurlKeyHelper {
public TurlKeyHelper() {
setSeq("mUE3fSJger5tHZ7IwnTVpBbP24jqQL89hMRa16FdKyOG0sDNlWAvuYcxoXkiCz");
}
private String Seq;
private String getSeq() {
return Seq;
}
private void setSeq(String value) {
Seq = value;
}
/**
* 生成随机的0-9a-zA-Z字符串
*
* @return
*/
private static String GenerateKeys() {
String[] chars = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z".split("[,]", -1);
Random seekRand = new Random(new Date().getTime());
for (int i = 0; i < 100000; i++) {
int r = seekRand.nextInt(chars.length);
String f = chars[0];
chars[0] = chars[r - 1];
chars[r - 1] = f;
}
return String.join("", chars);
}
/**
* 10进制转换为62进制
*
* @param id 十进制数
*/
private String Convert(long id) {
if (id < 62) {
return getSeq().charAt((int) id) + "";
}
int y = (int) (id % 62);
long x = id / 62;
return Convert(x) + getSeq().charAt(y);
}
/**
* 将62进制转为10进制
*
* @param num
*/
private long Convert(String num) {
long v = 0;
int len = num.length();
for (int i = len - 1; i >= 0; i--) {
int t = getSeq().indexOf(num.charAt(i));
double s = (len - i) - 1;
long m = (long) (Math.pow(62, s) * t);
v += m;
}
return v;
}
/**
* 混淆id为字符串
*
* @param id
*/
public final String Mixup(long id) {
id = id + (25 * 10000);
System.out.println(id);
String key = Convert(id);
int s = 0;
for (char c : key.toCharArray()) {
s += c;
}
int len = key.length();
int x = (s % len);
char[] arr = key.toCharArray();
char[] newarr = new char[arr.length];
System.arraycopy(arr, x, newarr, 0, len - x);
System.arraycopy(arr, 0, newarr, len - x, x);
StringBuilder newKey = new StringBuilder();
for (char c : newarr) {
newKey.append(c);
}
return newKey.toString();
}
/**
* 解开混淆字符串
*
* @param key
* @return
*/
public final long UnMixup(String key) {
int s = 0;
for (char c : key.toCharArray()) {
s += c;
}
int len = key.length();
int x = (s % len);
x = len - x;
char[] arr = key.toCharArray();
char[] newarr = new char[arr.length];
System.arraycopy(arr, x, newarr, 0, len - x);
System.arraycopy(arr, 0, newarr, len - x, x);
StringBuilder newKey = new StringBuilder();
for (char c : newarr) {
newKey.append(c);
}
long convert = Convert(newKey.toString());
return convert - (25 * 10000);
}
public static void RepeatTest() {
TurlKeyHelper test = new TurlKeyHelper();
//long id = 1;
//var key = test.Mixup(id);
//var retId = test.UnMixup(key);
//var bytes= Encoding.ASCII.GetBytes(key);
//var str = string.Join("", bytes);
List<String> liKeys = new ArrayList<>();
List<Long> ids = new ArrayList<>();
List<Long> uns = new ArrayList<>();
List<String> urls = new ArrayList<>();
try {
//加密
for (int i = 1; i <= 10; i++) {
String s = ConfigUtil.getHostName() + "/invite/to/RP/";
long l = Long.parseLong("1311205709820678146" + "");
ids.add(l);
String mixup = test.Mixup(l);
liKeys.add(mixup);
s += mixup;
urls.add(s);
}
//解密
for (String liKey : liKeys) {
uns.add(test.UnMixup(liKey));
}
List<String> collect = liKeys.stream().distinct().collect(Collectors.toList());
System.out.println("加密前数据==》" + ids);
System.out.println("加密后数据==》" + liKeys);
System.out.println("解密后数据==》" + uns);
System.out.println("原数组长度==》" + liKeys.size());
System.out.println("去重后数组长度==》" + collect.size());
System.out.println("加密后邀请路径==》" + urls);
} catch (RuntimeException e) {
e.printStackTrace();
throw e;
}
}
public static void main(String[] args) {
TurlKeyHelper.RepeatTest();
}
}
处理密码的算法
最新推荐文章于 2024-10-04 20:20:08 发布