JAva暗号化復号タイムスタンプ

9360 ワード

今日はタイムスタンプを21ビットの大文字と小文字の列に暗号化したjavaコードをお届けします.復号コードを添付します.考えは簡単です.娯楽だけです.
private Map codeMap; 
private String englishChars;
private ArrayList changeIndex;
public SimpleDateFormat sdf;

/**
     *     
     * @author huzhen
     * @since 2017/7/12
     * @param date date()    
     * */
    public String enTimeCode(Date enDate){
        String dataStr = null;
        try{
            dataStr = this.getSdf().format(enDate);
            String enStr = "";
            for(int i=0; ithis.getCodeMap().get(dataStr.charAt(i));
                enStr = enStr + transLetter;
            }
            int changeCusor = 0;
            StringBuffer retStr = new StringBuffer(generateRandString(21));
            for(int i=0; iif(this.getChangeIndex().contains(i)){
                    retStr.setCharAt(i, enStr.charAt(changeCusor));
                    changeCusor++;
                }
            }
            return retStr.toString();
        }catch(Exception e){
            //e.printStackTrace(); 
            System.out.println(e.getMessage());
            return "";
        }
    }

/**
     *          
     * @author huzhen
     * @since 2017/7/12
     * @param date date()    
     * */
    public Date deTimeCode(String enStr){
        String dateStr = "";
        try{
            String toDecodeStr = "";
            for(int i=0; iif(this.getChangeIndex().contains(i)){
                    toDecodeStr = toDecodeStr + enStr.charAt(i);
                }               
            }
            for(int i=0; ichar deChar = valueGetKey(this.getCodeMap(), String.valueOf(toDecodeStr.charAt(i))).get(0);
                if(deChar != '\0'){
                    dateStr += deChar;
                }               
            }
        return this.getSdf().parse(dateStr);
        }catch(Exception e){
            //e.printStackTrace(); 
            System.out.println(enStr + "     !");
            return null;
        }
    }


private Map getCodeMap() {
        if(codeMap == null){
            codeMap = new HashMap();
            codeMap.put('0', "I");
            codeMap.put('1', "L");
            codeMap.put('2', "O");
            codeMap.put('3', "V");
            codeMap.put('4', "E");
            codeMap.put('5', "M");
            codeMap.put('6', "Y");
            codeMap.put('7', "D");
            codeMap.put('8', "N");
            codeMap.put('9', "S");
        }
        return codeMap;
    }

    private static ArrayList valueGetKey(Map map, String value) {
        Set> set = map.entrySet();
        ArrayList arr = new ArrayList();
        Iterator> it = set.iterator();
        while(it.hasNext()) {
             Map.Entry entry = (Map.Entry)it.next();
             if(entry.getValue().equals(value)) {
                   char s = (char)entry.getKey();
                   arr.add(s);
             }
        }
        return arr;
    }


    private String getEnglishChars() {
        if(englishChars == null){
            englishChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        }
        return englishChars;
    }

    private SimpleDateFormat getSdf() {
        if(sdf == null){
            sdf = new SimpleDateFormat("yyyyMMdd");
        }
        return sdf;
    }

    public ArrayList getChangeIndex() {
        if(changeIndex == null){
            changeIndex = new ArrayList();
            changeIndex.add(1);
            changeIndex.add(6);
            changeIndex.add(9);
            changeIndex.add(13);
            changeIndex.add(14);
            changeIndex.add(17);
            changeIndex.add(19);
            changeIndex.add(20);
        }
        return changeIndex;
    }

/**
     *                 
     * @author huzhen
     * @since 2017/7/12
     * @param 
     * */
    private String generateRandString(int bits){
        String retStr = "";
        for(int i=0; ithis.getEnglishChars().charAt((int)(Math.random() * 26));
        }
        return retStr;
    }