Java Homework - 文件加密

时间:2015-05-15 21:28:49

标签: java arrays for-loop encryption mapping

String letters =" abcdefghijklmnopqrstuvwxyz&#34 ;; 字符串enc =" kngcadsxbvfhjtiumylzqropwe&#34 ;;

您好,对于我的家庭作业,我必须编写一个编码或解码文件的程序,然后使用上面的映射对文件进行编码或解码。例如,每个' a'成为一个''在对文本进行编码时,每个' k'成为一个' a'解码时同样的概念,如果它是大写的,数字和其他字符不编码并保持不变。

现在我遇到的问题是如何从文件中获取每个字符的索引,然后将其与加密数组的索引相对应。正如你所看到的,我正在使用一个switch语句,但这只是需要永远,我知道api中必须有一些可以帮助我的东西,我无法找到任何东西。提前谢谢!

这是我的代码:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Encode {
    public static void main(String[] args) throws FileNotFoundException
    {
         char[] alphabet = {'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'};
         char[] decryption = {'k','n','g','c','a','d','s','x','b','v','f','h','j','t','i','u','m','y','l','z','q','r','o','p','w','e'};
         char[] alphabetLower = new char [26];
         char[] alphabetUpper = new char[26];
         char[] decryptedPassword = new char[26];
    /*   for(int i=0; i<decryptedPassword.length; i++)
         {
             decryptedPassword[i] = '';
         }*/



            //add the letters of the alphabet to alphabet lower
            for(int i=0; i<alphabet.length; i++)
            {
                alphabetLower[i] = alphabet[i];
            }
            //add the letters of the alphabet to alphabetdecryptedPassword
            for(int i=0; i<alphabet.length; i++)
            {
                alphabetUpper[i] = Character.toUpperCase(alphabet[i]);
            }


        Scanner input = new Scanner(System.in); //scanner for input file name



        String filePath = "C:/Users/omid/Desktop/";
        System.out.println("Please enter the file name for which you want to decode");
        String fileName = "password"; //input.nextLine();
        String file = filePath + fileName + ".txt";
        System.out.println("File name entered: " + file);

        //import file

        File fileEncrypted = new File(file);

        Scanner in = new Scanner(fileEncrypted);
        String document = "";

        //store entire password in document

        while(in.hasNextLine())
        {
            document = in.nextLine();
        }
        in.close();

            System.out.println("password normal: " + document);
            char[] letters = new char[document.length()];

        for(int i = 0; i<document.length(); i++)
        {
                letters[i] = document.charAt(i);
        }

        for(int i=0; i<letters.length; i++)
        {
            System.out.println(letters[i]);
        }


/*

    for(int i=0; i<letters.length; i++)
        {
            switch(letters[i])
            {
             case 'a':
                decryptedPassword[0] = decryption[0];
                     break;
                 case 'A':
                decryptedPassword[0] = decryption[0];
                     break;
                 case 'b':
                decryptedPassword[1] = decryption[1];
                     break;
                case 'B':
                    decryptedPassword[1] = decryption[1];
                     break;
                 case 'c':
                    decryptedPassword[2] = decryption[2];
                     break;
                case 'C':
                     decryptedPassword[2] = decryption[2];
                     break;
                 case 'd':
                    decryptedPassword[3] = decryption[3];
                     break;
                 case 'D':
                     decryptedPassword[3] = decryption[4];
                     break;
                 case 'e':
                    decryptedPassword[4] += 1;
                     break;
                 case 'E':
                     decryptedPassword[4] +=1;
                     break;
                 case 'f':
                    decryptedPassword[5] += 1;
                     break;
                 case 'F':
                     decryptedPassword[5] +=1;
                     break;
                 case 'g':
                     decryptedPassword[6] +=1;
                     break;
                 case 'G':
                     decryptedPassword[6] +=1;
                     break;
                 case 'h':
                     decryptedPassword[7] +=1;
                     break;
                 case 'H':
                     decryptedPassword[7] +=1;
                     break;
                 case 'i': 
                     decryptedPassword[8] +=1;
                     break;
                 case 'I':
                     decryptedPassword[8] +=1;
                     break;
                 case 'j':
                     decryptedPassword[9] +=1;
                     break;
                 case 'J':
                     decryptedPassword[9] +=1;
                     break;
                 case 'k':
                     decryptedPassword[10] +=1;
                     break;
                 case 'K':
                     decryptedPassword[10] +=1;
                     break;
                 case 'l':
                     decryptedPassword[11] +=1;
                     break;
                 case 'L':
                     decryptedPassword[11] +=1;
                     break;
                 case'm': 
                     decryptedPassword[12] +=1;
                     break;
                 case 'M':
                     decryptedPassword[12] +=1;
                     break;
                 case'n':
                     decryptedPassword[13] += 1;
                     break;
                 case 'N':
                     decryptedPassword[13] +=1;
                     break;
                 case'o':
                     decryptedPassword[14] +=1;
                    break;
                 case 'O':
                     decryptedPassword[14] +=1;
                     break;
                 case'p':
                     decryptedPassword[15] +=1;
                     break;
                 case 'P':
                     decryptedPassword[15] +=1;
                     break;
                 case'q':
                     decryptedPassword[16] +=1;
                     break;
                 case 'Q':
                     decryptedPassword[16] +=1;
                     break;
                 case'r':
                     decryptedPassword[17] +=1;
                     break;
                 case 'R':
                     decryptedPassword[17] +=1;
                     break;
                 case's':
                     decryptedPassword[18] +=1;
                     break;
                 case 'S':
                     decryptedPassword[18] +=1;
                     break;
                 case't':
                     decryptedPassword[19] +=1;
                     break;
                 case 'T':
                     decryptedPassword[19] +=1;
                     break;
                 case'u':
                     decryptedPassword[20] +=1;
                     break;
                 case 'U':
                     decryptedPassword[20] +=1;
                     break;
                 case'v':
                     decryptedPassword[21] +=1;
                     break;
                 case 'V':
                     decryptedPassword[21] +=1;
                     break;
                 case'w':
                     decryptedPassword[22] +=1;
                     break;
                 case 'W':
                     decryptedPassword[22] +=1;
                     break;
                 case'x':
                     decryptedPassword[23] +=1;
                     break;
                 case 'X':
                     decryptedPassword[23] +=1;
                     break;
                 case'y':
                     decryptedPassword[24] +=1;
                     break;
                 case 'Y':
                     decryptedPassword[24] +=1;
                     break;
                 case'z':
                     decryptedPassword[26] +=1;
                     break;
                 case 'Z':
                     decryptedPassword[26] +=1;
                     break;
            }

        }

    */

/*for(int i=0; i<decryptedPassword.length; i++)
{
    System.out.println("password decrypted: " + decryptedPassword);
}*/








    }

    private static String split(String string) {
        // TODO Auto-generated method stub
        return null;
    }


}

2 个答案:

答案 0 :(得分:2)

您可以尝试使用哈希结构。基本上每个字符映射到每个其他字符的1到1映射,以便很好地适合散列表或散列映射结构。而不是使用switch语句,只需在地图中查找字符。

HashMap<Character, Character> encryptionMap = new HashMap<Character,Character>();

for (char c : alphabet) {
    for (char d: decryption) {
        encryptionMap.put(c,d);
    }
}
....
char nextChar = "a";
char encryptedChar = encryptionMap.get(nextChar);

你需要另一个方向的加密地图和解密地图(解密&gt;字母表)。

答案 1 :(得分:0)

您应该考虑使用BufferedReader读取文件,但这是一个不同的问题。至于在不使用switch的情况下对字符进行编码,请考虑将字符内部编码为整数。你可以谷歌&#34; Ascii表&#34;查看每个字符的编码方式。例如,&#39; A&#39;被编码为65和&#39; a&#39;编码为97.您可以使用此功能来索引字母表和解密数组。如果你的字符是大写的,我们从它中减去65来获得数组中该字符的索引。所以&#39; A&#39;变为0,&#39; B&#39;变为1,等等。如果字母是小写的,我们从中减去97.

例如,假设ch包含您要解码的字母:

int index = -1;
//ch is between A and Z
if (ch >= 65 && ch <= 90){
    index = ch - 65;
} else if (ch >= 97 &7 ch <= 122){//ch is between a and z
    index = ch - 97;
}

if (index > -1){
    char encodedChar = decryption[index];
}