3

我正在尝试制作一个基本的加密程序,该程序使用随机数来加密整个字母表,加密用户提交的短语,然后将其解密回原始短语,但我发现它很难。谁能帮忙指出我的错误!它不应该将两个字母编码为同一个字母,即a 和b 不应该都与c 匹配。

public class MainClass {
public static final int ALPHASIZE = 26;
public static final char[] Lalpha =
    { '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'
    };
public static final char[] Ualpha =
    {'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',
    };
protected static char[] encryptU = new char[ALPHASIZE];
protected static int[] decrypt = new int[ALPHASIZE];
protected static char[] encryptL = new char[ALPHASIZE];



Random rgenerator = new Random();

public MainClass(){
    int randNum = rgenerator.nextInt(ALPHASIZE);


    for(int i=0; i<ALPHASIZE ; i++)
    {
        //makes sure that it won't assign a letter to itself or to one that has already been assigned

        do {
            randNum = rgenerator.nextInt(26);

         } while (randNum%26==0 &&Arrays.asList(encryptU).contains(Ualpha[randNum]));       

        encryptU[i] = Ualpha[randNum]; 
        encryptL[i] = Lalpha[randNum];
        decrypt[i] = randNum;

    }
}

public String encrypt(String secret)
{
    System.out.println(Arrays.toString(encryptU));
        int position = 0;
    char[] mess = secret.toCharArray();
    for(int i = 0 ; i<mess.length;i++)
    {
        if(Character.isUpperCase(mess[i]))
        {
    for(int j = 0; j < encryptU.length; j++) {
        if(mess[i]==Ualpha[j]) {
            position = j;
    }
    mess[i] = encryptU[position];

        }

        }

        if(Character.isLowerCase(mess[i]))
        {
    for(int j = 0; j < encryptU.length; j++) {
        if(mess[i]==Lalpha[j]) {
            position = j;
    }
    mess[i] = encryptL[position];

        }

        }

    }
    return new String(mess);
}

public String decrypt(String secret)
{
    char[] mess = secret.toCharArray();
    for(int i = 0 ; i<mess.length;i++)
    {
        if(Character.isUpperCase(mess[i]))
                {
                for(int j = 0; j<ALPHASIZE; j++){
                    if(mess[i]==encryptU[j]){
                        mess[i] = Ualpha[j];

                        }
                    }
                }           

        if(Character.isLowerCase(mess[i]))
        {                   
            for(int j = 0; j<ALPHASIZE; j++){
                if(mess[i]==encryptL[j]){
                    mess[i] = Lalpha[j];

                    }
                }
            }
    }

    return new String(mess);
}
}
4

3 回答 3

2

您应该真正考虑使用 aMap来存储字符/编码对。哦,要创建这些随机对,您可以将角色添加到 aList并使用,Collections.shuffle而不是自己重新发明轮子。


让我演示仅使用Lalpha(仅小写字母)。你想要一些类似的东西:

List<Character> l = new ArrayList<Character>(Lalpha.length); 

for (char c : Lalpha)
    l.add(c);

Collections.shuffle(l);

Map<Character, Character> encoding = new HashMap<Character, Character>(Lalpha.length);
Map<Character, Character> decoding = new HashMap<Character, Character>(Lalpha.length);

for (int i = 0 ; i < Lalpha.length ; i++) {
    encoding.put(Lalpha[i], l.get(i));
    decoding.put(l.get(i), Lalpha[i]);
}

现在假设我们想要对字符串进行编码/解码helloworld,我们会这样做:

String s = "helloworld";    

// Encode:
String enc = "";
for (char c : s.toCharArray())
    enc += encoding.get(c);

System.out.println(enc);

// Decode:
String dec = "";
for (char c : enc.toCharArray())
    dec += decoding.get(c);

System.out.println(dec);

输出(许多可能之一):

vjwwmtmcwz
helloworld

当然,您可以使用相同的想法合并大写字母和what-not。

于 2012-10-06T21:18:39.313 回答
1

听起来您需要生成允许字母的排列。这就是我对小写字母的处理方式:

public char[] permutation =
{ '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'
};


public generatePermutation()
{
    Random r = new Random();
    char tmp;
    int rand;

    for(int i = 0; i < permutation.length; ++i)
    {
        rand = r.nextInt(permutation.length - i);
        tmp = permutation[i];
        permutation[i] = permutation[rand];
        permutation[rand] = tmp;
    }
}

最后,您可以通过执行访问此数组进行加密permutation[inputChar-'a'](假设您已经确定 inputChar 是小写字母)。对于解密,您会找到与您的输入字符匹配的字母并将“a”添加到索引中。

于 2012-10-06T21:12:43.960 回答
0

如果您在创建字母集和“加密”集之间的随机映射时遇到问题,您可以从以下开始:

List<Character> alphabet = new LinkedList<Character>(
        new String[] {'a', 'b', ..., 'Y', 'Z'});

List<Character> shuffledAlphabet = new LinkedList<Character>(alphabet);
Collections.shuffle(shuffledAlphabet);

Map<Character, Character> encryptionMap = new HashMap<Character, Character>();
Map<Character, Character> decryptionMap = new HashMap<Character, Character>();
for (int i=0; i < alphabet.size(); i++) {
    encryptionMap.put(alphabet.get(i), shuffledAlphabet.get(i));
    decryptionMap.put(shuffledAlphabet.get(i), alphabet.get(i));
}

现在,您通过获取键入的 String 的每个字符、在 encryptionMap 上执行 get 并替换为该 get 的结果来进行加密。要解密,对decryptionMap 执行相同的操作。

于 2012-10-06T21:18:16.490 回答