java基本加密程序

时间:2012-10-06 21:08:09

标签: java

我正在尝试制作一个基本加密程序,使用随机数加密整个字母表,加密用户提交的短语,然后将其解密回原始短语,但我发现它很难。任何人都可以帮我指出我的错误!它不应该将两个字母编码为同一个字母,即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);
}
}

3 个答案:

答案 0 :(得分:2)

您应该考虑使用Map来存储字符/编码对。哦,要创建这些随机对,你可以将你的角色添加到List并使用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

当然,你可以使用大写字母和不使用相同的想法。

答案 1 :(得分: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是小写字母)。对于解密,您会找到与您的输入字符匹配的字母,并添加&#39; a&#39;到索引。

答案 2 :(得分: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的每个字符进行加密,对加密映射执行get操作,并替换为get的结果。要解密,请使用decryptionMap执行相同的操作。