首先,先创建一个cube类,方便按路径移动和画图,直接贴码
package com.test.sshkeygen; import static com.test.sshkeygen.Cube.CubeType.A; import static com.test.sshkeygen.Cube.CubeType.MMM; /** */ public class Cube { public static enum CubeType{ MMM,LL,RR,TT,BB,A,B,C,D } public CubeType cubeType; public Cube() { } public int horizontalMax; public int verticalMax; public Cube(int horizontal, int vertical) { this.horizontal = horizontal; this.vertical = vertical; } public int getHorizontalMax() { return horizontalMax; } public void setHorizontalMax(int horizontalMax) { this.horizontalMax = horizontalMax; } public int getVerticalMax() { return verticalMax; } public void setVerticalMax(int verticalMax) { this.verticalMax = verticalMax; } public CubeType getCubeType() { return cubeType; } public void setCubeType(CubeType cubeType) { this.cubeType = cubeType; } private int horizontal; private int vertical; public int getHorizontal() { return horizontal; } public void setHorizontal(int horizontal) { this.horizontal = horizontal; } public int getVertical() { return vertical; } public void setVertical(int vertical) { this.vertical = vertical; } public void turnLT(){ //↖↖↖↖↖↖↖↖↖↖↖↖↖↖↖↖↖↖↖↖↖↖↖↖↖ if (getCubeType()==CubeType.LL||getCubeType()==CubeType.C){ this.vertical--; }else if (getCubeType()==CubeType.A){ //无操作 }else if (getCubeType()==CubeType.TT||getCubeType()==CubeType.B){ this.horizontal--; }else{ this.vertical--; this.horizontal--; } setCubeTypeAdvanced(); } public void turnRT(){ //↗↗↗↗↗↗↗↗↗↗↗↗↗↗↗↗↗↗↗↗↗↗ if (getCubeType()==CubeType.RR||getCubeType()==CubeType.D){ this.vertical--; }else if (getCubeType()==CubeType.B){ //无操作 }else if (getCubeType()==CubeType.TT||getCubeType()==CubeType.B){ this.horizontal++; }else{ this.vertical--; this.horizontal++; } setCubeTypeAdvanced(); } public void turnLB(){ //↙↙↙↙↙↙↙↙↙↙↙↙↙↙↙↙↙↙↙↙↙↙ if (getCubeType()==CubeType.LL||getCubeType()==CubeType.A){ this.vertical++; }else if (getCubeType()==CubeType.C){ //无操作 }else if (getCubeType()==CubeType.BB||getCubeType()==CubeType.D){ this.horizontal--; }else{ this.vertical++; this.horizontal--; } setCubeTypeAdvanced(); } public void turnRB(){ //↘↘↘↘↘↘↘↘↘↘↘↘↘↘↘↘↘↘↘↘↘↘↘↘ if (getCubeType()==CubeType.RR||getCubeType()==CubeType.B){ this.vertical++; }else if (getCubeType()==CubeType.D){ //无操作 }else if (getCubeType()==CubeType.BB||getCubeType()==CubeType.C){ this.horizontal++; }else{ this.vertical++; this.horizontal++; } setCubeTypeAdvanced(); } public void setCubeTypeAdvanced() { int h=getHorizontal(); int v=getVertical(); int hmax=getHorizontalMax(); int vmax=getVerticalMax(); if (h==0&&v==0){ setCubeType(CubeType.A); }else if (h==0&&v==vmax){ setCubeType(CubeType.C); }else if (h==hmax&&v==0){ setCubeType(CubeType.B); }else if (h==hmax&&v==vmax){ setCubeType(CubeType.D); }else if (h>0&&h<hmax&&v==0){ setCubeType(CubeType.TT); }else if (h>0&&h<hmax&&v==vmax){ setCubeType(CubeType.BB); }else if (v>0&&v<vmax&&h==0){ setCubeType(CubeType.LL); }else if (v>0&&v<vmax&&h==hmax){ setCubeType(CubeType.RR); }else { setCubeType(MMM); } } }然后是测试类:
package com.test.sshkeygen; import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ; import com.sun.xml.internal.bind.v2.model.core.ID; import java.util.*; /** */ public class RandomartImgTest { public static void main(String[] args) { char[] charArr={ ' ','.','o','+','=','*','B','O','X','@','%','&','#','/','^','S','E' }; String s0="09:1d:0f:da:c8:fd:e9:40:53:42:99:76:97:ee:9f:b7"; String s00="09:1d:27:da:83:dc:fe:94:d0:40:99:76:97:ee:9f:b7"; String s="fc:94:b0:c1:e5:b0:98:7c:58:43:99:76:97:ee:9f:b7"; getPic(charArr, s0); getPic(charArr, s00); } private static void getPic(char[] charArr, String s) { String [] arr=StringTo16(s); String [] arrFinal=StringSplitTo4(arr); // ↖ 左上 00 // ↗ 右上 01 // ↙ 左下 10 // ↘ 右下 11 // horizontal 0-16 // vertical 0-8 // 数字的值 v*17+h 从76开始 即 h为8,v为4 Cube c=new Cube(8,4); c.setHorizontalMax(16); c.setVerticalMax(8); c.setCubeType(Cube.CubeType.MMM); List<Integer> list=new ArrayList<>(); list.add(76); Set<Integer> uniqueSet = null; for (int i=0;i<arrFinal.length;i++) { String ss=arrFinal[i]; if (ss.equals("00")){ c.turnLT(); }else if (ss.equals("01")){ c.turnRT(); }else if (ss.equals("10")){ c.turnLB(); }else if (ss.equals("11")){ c.turnRB(); } // System.out.println("horizontal:"+c.getHorizontal()+"vertical:"+c.getVertical()); list.add(c.getHorizontal()+c.getVertical()*(c.getHorizontalMax()+1)); } for (int ilist:list) { System.out.println(""+ilist+""); } uniqueSet = new HashSet(list); Integer iMAX = list.get(list.size() - 1); Map<Integer,String> map=new HashMap<>(); for (Integer temp : uniqueSet) { if (temp.intValue()==list.get(0).intValue()){ map.put(temp,"S"); }else if (temp.intValue()==iMAX.intValue()){ map.put(temp,"E"); }else { map.put(temp.intValue(),String.valueOf(charArr[Collections.frequency(list, temp)])); } System.out.print(temp + ": " + Collections.frequency(list, temp)+"|"); } for (Map.Entry entry:map.entrySet()) { System.out.println("key:"+entry.getKey()+"value:"+entry.getValue()); } System.out.println(""); System.out.println("-----------------"); for (int iVer=0;iVer<=c.getVerticalMax();iVer++){ List<String>listV=new ArrayList<>(); for (int iHor=0;iHor<=c.getHorizontalMax();iHor++){ if (map.containsKey(17*iVer+iHor)){ listV.add(map.get(17*iVer+iHor)); }else{ listV.add(" "); } /*for (Map.Entry entry:map.entrySet()){ Integer key= (Integer) entry.getKey(); String value= (String) entry.getValue(); if (((key/c.getHorizontalMax()-1)==iVer)&&(key%c.getHorizontalMax()==iHor)){ listV.add(value); }else{ listV.add("~"); } }*/ } for (String sFinal:listV) { System.out.print(sFinal); } System.out.println(""); } System.out.println("-----------------"); System.out.println("set size :"+(uniqueSet.size()+1)); System.out.println("list size :"+(list.size()+1)); /*验证 正确 int [] iArr={76, 58, 76, 94, 112, 94, 78, 62, 78, 60, 42, 60, 76, 60, 42, 24, 42, 26, 10, 26, 44, 26, 8, 26, 42, 24, 40, 24, 40, 22, 40, 58, 42, 24, 40, 24, 8, 26, 8, 7, 8, 9, 25, 9, 25, 41, 25, 43, 27, 45, 29, 13, 29, 45, 63, 79, 97, 115, 133, 117, 133, 151, 135, 152, 151}; for (int idex=0;idex<list.size();idex++) { int ii=list.get(idex); int iii=iArr[idex]; if (ii!=iii){ System.out.println("错误"); } System.out.println(ii==iii); }*/ } public static String[] StringSplitTo4(String [] arr){ String [] arrFinal=new String [arr.length*4]; for ( int i=0;i<arr.length;i++) { String s1=arr[i].substring(6,8); String s2=arr[i].substring(4,6); String s3=arr[i].substring(2,4); String s4=arr[i].substring(0,2); arrFinal[4*i]=s1; arrFinal[4*i+1]=s2; arrFinal[4*i+2]=s3; arrFinal[4*i+3]=s4; } return arrFinal; } public static String[] StringTo16(String s){ String[] arr = s.split(":"); for ( int i=0;i<arr.length;i++) { String ss=arr[i]; String sss=hexString2binaryString(ss); arr[i]=sss; } return arr; } public static String hexString2binaryString(String hexString) { if (hexString == null || hexString.length() % 2 != 0) return null; String bString = "", tmp; for (int i = 0; i < hexString.length(); i++) { tmp = "0000" + Integer.toBinaryString(Integer.parseInt(hexString .substring(i, i + 1), 16)); bString += tmp.substring(tmp.length() - 4); } return bString; } }
测试数据:仅仅知道每个元素出现的位置和次数,就能确定整个路径?这个图能保证路径唯一吗? 我是做后端的,这个我用java代码实现了,但感觉还是有点问题,难道完全寄托于MD5不容易相等的特性?那也不对啊,路径不同可能图是相同的。。。。。String s0="09:1d:0f:da:c8:fd:e9:40:53:42:99:76:97:ee:9f:b7";
最后结果:
----------------- .=o. . . *+*. o =.*..o o + .. S o. o . . . . o . E. -----------------
String s0="09:1d:0f:da:c8:fd:e9:40:53:42:99:76:97:ee:9f:b7";
String s1="09:1d:27:da:83:dc:fe:94:d0:40:99:76:97:ee:9f:b7";
我测过了 这俩的图形一样的 说明了这种方法不可靠,只是看起来直观点而已,还是直接比原始数据靠谱