此加密算法是Z变幻,然后我对该加密方法进行了改变和解析。下面的是一个完整的例子,您可以直接跑。
加密
此加密算法是Z变幻,然后我对该加密方法进行了改变和解析。
解密
public String decode(String codeString) {
/**
* 解密规则:
* 1.字符串倒数第3和第4位是字符串长度的标识位
* 2.字符串倒数第1和第2位是字符串箱子大小的标识位
*/
if (codeString == null || codeString.trim().length() <= 4) {
throw new RRException("无效的请求");
}
int boxSize = 0;
int length = 0;
String strBoxSize = codeString.substring(codeString.length() - 2, codeString.length());
String strLength = codeString.substring(codeString.length() - 4, codeString.length() - 2);
try {
boxSize = Integer.parseInt(strBoxSize);
length = Integer.parseInt(strLength);
} catch (Exception ex) {
throw new RRException("无效的请求");
}
//长度不对 无效的请求
if (codeString.length() != length + 4 || boxSize < 2) {
throw new RRException("无效的请求");
}
// String sr=codeString.substring(0, codeString.length() - 4);
return dConvert(codeString, boxSize);
}
/**
* 加密解密逻辑完善
* @param s2
* @param numRows
* @return
*/
public static String dConvert(String s2, int numRows) {
if (s2.length() <= numRows || numRows == 1)
return s2;
String s = s2.substring(0, s2.length() - 4);
int index = 0;
List<String> result = new ArrayList<>();
for (int i = 0; i < s.length(); ++i) {
result.add("");
}
for (int i = 0; i < numRows; ++i) {
if (i == 0) { //读取第一行
for (int j = 0; j < s.length(); j += 2 * numRows - 2) {
String tag = s.charAt(index) + "";
System.out.println(String.format("%d-%s-%d", index, tag, j));
result.set(j, tag);
index++;
}
} else if (i == numRows - 1) { //读取最后
for (int j = numRows - 1; j < s.length(); j += 2 * numRows - 2) {
String tag = s.charAt(index) + "";
result.set(j, tag);
System.out.println(String.format("%d-%s-%d", index, tag, j));
index++;
}
} else { //读取中间行
for (int j = i; j < s.length(); j += 2 * numRows - 2) {
// result.append(s.charAt(j));
String tag = s.charAt(index) + "";
result.set(j, tag);
System.out.println(String.format("%d-%s-%d", index, tag, j));
index = index + 1;
if (j + 2 * numRows - 2 - 2 * i < s.length()) {
int z = j + 2 * numRows - 2 - 2 * i;
String tag2 = s.charAt(index) + "";
result.set(z, tag2);
index++;
}
}
}
}
StringBuffer stringBuffer = new StringBuffer();
result.forEach(s1 -> stringBuffer.append(s1));
return stringBuffer.toString();
}
public static void main(String[] args) {
VerificationServiceImple VerificationServiceImple = new VerificationServiceImple();
//算法的最后一位是模
System.out.println("原始串:" + "202004307552726016");
String aa = VerificationServiceImple.convert("202004307552726016", 4);
System.out.println("加密后:" + aa);
String sou = VerificationServiceImple.dConvert(aa, 4);
System.out.println("解密后:" + sou);
}
解密
/**
* s实现对核销码的加密
*
* @param s
* @param numRows 箱子的大小
* @return 加密原理=原始码加密+原始码长度
*/
public static String convert(String s, int numRows) {
if (s.length() <= numRows || numRows == 1)
return s;
StringBuilder result = new StringBuilder();
for (int i = 0; i < numRows; ++i) {
if (i == 0) { //读取第一行
for (int j = 0; j < s.length(); j += 2 * numRows - 2)
result.append(s.charAt(j));
} else if (i == numRows - 1) { //读取最后
for (int j = numRows - 1; j < s.length(); j += 2 * numRows - 2)
result.append(s.charAt(j));
} else { //读取中间行
for (int j = i; j < s.length(); j += 2 * numRows - 2) {
result.append(s.charAt(j));
if (j + 2 * numRows - 2 - 2 * i < s.length())
result.append(s.charAt(j + 2 * numRows - 2 - 2 * i));
}
}
}
if (numRows < 10) {
return result.toString() + result.length() + "0" + numRows;
} else {
//不考虑大于100
return result.toString() + result.length() + "" + numRows;
}
}