如何提高加密程序的速度?

时间:2015-04-07 16:00:45

标签: java performance encryption arraylist hashmap

首先问一下程序的功能。

答:它需要一个密码

B:它为用户的每个密码字符创建一个arraylist。

C:它将创建的数组列表存储在Arraylist(ArrayList>)

D:程序逐字逐句读入。加密字符(使用ADFGVX polybius方形密码)并将加密字符添加到arraylists。

如果密码是盖子,则第一个字符在列表1中,第二个字符在列表2中,第三个字符在列表3中,第四个字符再次列在列表1中。直到读完所有字符。

E:它按照第一个字符的字母顺序对列表进行排序。 所以密码[l,i,d]将排序为[d,i,l]

F:程序然后将字符解密回原始形式,将它们添加到打印到文件的数组列表中。

孔代码如下。我读入的文件大小为3,120KB,包含大约320万个字符。 在我的机器上,孔程序将在0.7秒内完成所有操作。

有没有办法让它跑得更快? 我有一个想法是使用linkedhashmap存储arraylists而不是使用ArrayList>。

我从未使用过链接的hashmap,也不知道我将如何按字母顺序对列表进行排序。

任何可以帮助我加快计划的想法或建议都将不胜感激。 (我正在运行java 1.8)

package gmit;

import java.util.*;
import java.io.*;
import java.nio.charset.Charset;

public class project {
    // Creating Hashmap
    private static HashMap<String, String> stringmap = new HashMap<String, String>();

    // Creating String Array lists
    private static ArrayList<ArrayList<String>> mylist = new ArrayList<ArrayList<String>>();
    private static ArrayList<String> lol = new ArrayList<String>();
    // Declaring int variables
    private static int encryptioncounter=0,decryptioncounter=0,lastindexcounter=0,mylistindexcounter=0;
    // Declaring String variables
    private static String password,value;
    private static String result;


    // ----------------------------------------------- Main start --------------------------------------------------------
    public static void main(String[] args) throws IOException {
        // Method to populate HashMap
        putstuffinstrinmap();

        System.out.println("Pleaseinput your passphrase");
        // Gets user Input
        Scanner in = new Scanner(System.in);
        password = in.nextLine();

        // Starts for loop
        // Creates Array list in number of the amount of characters of the password
        // Adds created arraylists to Arraylist<List> mylist.
        for (int i = 0; i < password.length(); i++) {
            List<String> firstCharandMore= new ArrayList<>();
            char c = password.charAt(i);
            firstCharandMore.add(String.valueOf(c));

                mylist.add((ArrayList<String>) firstCharandMore);
            }

        //Starts Timer
        long startTime = System.currentTimeMillis();
        // Reads in File in UTF-8 format
        // gets back a character
        BufferedReader reader = new BufferedReader(
                new InputStreamReader(
                    new FileInputStream("WarAndPeace-LeoTolstoy.txt"),
                    Charset.forName("UTF-8")));
            int c;
            while((c = reader.read()) != -1) {
              char string = (char) c;

              // Converts Character to upper case String
              // Passes the string into encryption method
              encrypt(Character.toString(string).toUpperCase());
              // Adds one to counter
              decryptioncounter++;

            }

            // Creates new Arraylist listToSort (Copy of mylist)
            // Sorts Array lists in alphabetic order by first character from each list
            List<List<String>> listToSort = new ArrayList<>(mylist);    
            listToSort.sort((l1, l2) -> l1.get(0).compareTo(l2.get(0)));

            // Calls decryption method
            decrypt();
            writetofile();
            // Ends Timer
            long endTime   = System.nanoTime();
        long totalTime = endTime - startTime;
        // Prints Time
        double seconds = (double)totalTime / 1000000000.0;
        System.out.println(seconds+" Total time");       
    }
    // ------------------------------------ End Main -----------------------------------------

    private static void writetofile() throws FileNotFoundException, UnsupportedEncodingException {
        PrintWriter writer = new PrintWriter("finished.txt", "UTF-8");
        StringBuilder builder = new StringBuilder();
        for (String value : lol) {
            builder.append(value);
        }
        String s = builder.toString();
        //String s = lol.toString();
        writer.println(s);
        writer.close();

    }


    // -------------------------------- Encryption Start -------------------------------------
    private static void encrypt(String string) {

        // Gets
        value= stringmap.get(string);
        mylist.get(encryptioncounter).add(value);
        // Adds one to counter
        encryptioncounter++;            
        if(encryptioncounter>=password.length()){
            encryptioncounter=0;
            // Adds one to counter
            lastindexcounter++;
        }       
    }
    // --------------------------------- Encryption End ---------------------------------------

    // --------------------------------- Decryption Start -------------------------------------
    private static void decrypt() {

        int thiscounter=0;
        while(decryptioncounter   != thiscounter  ){
            for(int i=0;i <password.length() ;i++){

                String ba = mylist.get(i).get(mylistindexcounter);              
                result= stringmap.get(ba);
                lol.add(result);
                thiscounter++;
                if(mylistindexcounter >=lastindexcounter){
                    decryptioncounter =thiscounter;
                }
            }
            mylistindexcounter++;
        }       
    }
    // ---------------------------------- Decryption End ---------------------------------------

    // --------------------------------- Map population start ---------------------------------

    public static void putstuffinstrinmap(){
        stringmap.put("AA", "P");//AA - P
        stringmap.put("AD", "H");//AD - H
        stringmap.put("AF", "0");//
        stringmap.put("AG", "Q");//
        stringmap.put("AV", "G");//
        stringmap.put("AX", "6");//
        stringmap.put("DA", "4");//
        stringmap.put("DD", "M");//
        stringmap.put("DF", "E");//
        stringmap.put("DG", "A");//
        stringmap.put("DV", "1");//
        stringmap.put("DX", "Y");//
        stringmap.put("FA", "L");//
        stringmap.put("FD", "2");//
        stringmap.put("FF", "N");//
        stringmap.put("FG", "O");//
        stringmap.put("FV", "F");//
        stringmap.put("FX", "D");//
        stringmap.put("GA", "X");//
        stringmap.put("GD", "K");//
        stringmap.put("GF", "R");//
        stringmap.put("GG", "3");//
        stringmap.put("GV", "C");//
        stringmap.put("GX", "V");//
        stringmap.put("VA", "S");//
        stringmap.put("VD", "5");//
        stringmap.put("VF", "Z");//
        stringmap.put("VG", "W");//
        stringmap.put("VV", "7");//
        stringmap.put("VX", "B");//
        stringmap.put("XA", "J");//
        stringmap.put("XD", "9");//
        stringmap.put("XF", "U");//
        stringmap.put("XG", "T");//
        stringmap.put("XV", "I");//
        stringmap.put("XX", "8");//
        stringmap.put("P", "AA");//
        stringmap.put("H", "AD");//
        stringmap.put("0", "AF");//
        stringmap.put("Q", "AG");//
        stringmap.put("G", "AV");//
        stringmap.put("6", "AX");//
        stringmap.put("4", "DA");//
        stringmap.put("M", "DD");//
        stringmap.put("E", "DF");//
        stringmap.put("A", "DG");//
        stringmap.put("1", "DV");//
        stringmap.put("Y", "DX");//
        stringmap.put("L", "FA");//
        stringmap.put("2", "FD");//
        stringmap.put("N", "FF");//
        stringmap.put("O", "FG");//
        stringmap.put("F", "FV");//
        stringmap.put("D", "FX");//
        stringmap.put("X", "GA");//
        stringmap.put("K", "GD");//
        stringmap.put("R", "GF");//
        stringmap.put("3", "GG");//
        stringmap.put("C", "GV");//
        stringmap.put("V", "GX");//
        stringmap.put("S", "VA");//
        stringmap.put("5", "VD");//
        stringmap.put("Z", "VF");//
        stringmap.put("W", "VG");//
        stringmap.put("7", "VV");//
        stringmap.put("B", "VX");//
        stringmap.put("J", "XA");//
        stringmap.put("9", "XD");//
        stringmap.put("U", "XF");//
        stringmap.put("T", "XG");//
        stringmap.put("I", "XV");//
        stringmap.put("8", "XX");// 
        stringmap.put("AH", ".");//
        stringmap.put("DH", "!");//
        stringmap.put("FH", "?");//
        stringmap.put("GH", " ");//
        stringmap.put("VH", "'");//
        stringmap.put("XH", ",");//
        stringmap.put(".", "AH");//
        stringmap.put("!", "DH");//
        stringmap.put("?", "FH");//
        stringmap.put(" ", "GH");//
        stringmap.put("'", "VH");//
        stringmap.put(",", "XH");//     
        stringmap.put("AP", "]");//
        stringmap.put("DP", "[");//
        stringmap.put("FP", "~");//
        stringmap.put("GP", "&");//
        stringmap.put("VP", ":");//
        stringmap.put("XP", ";");//
        stringmap.put("AP", "-");//"
        stringmap.put("DP", "");//
        stringmap.put("FP", "*");//
        stringmap.put("GP", "^");//
        stringmap.put("VP", "\"");//
        stringmap.put(null, " ");//
    }
    // --------------------------------- Map population End -----------------------------------

}

修改

根据Andy Turner的建议,我已将System.currentTimeMillis()更改为System.nanoTime(); 所以时间测量已经改变了。 (上面的代码按此方式编辑)

0 个答案:

没有答案