1

我一直有一个凯撒密码和维吉尼亚密码的项目。我已经想出了凯撒密码,但我正在努力使用维吉尼亚密码。

我的加密代码分为两部分:encrypt()encrypt1()encrypt()方法提取每个字符并将其传递到encrypt1()基本上进行所有加密的地方。我在这里做了代码,但它没有给我任何好的结果。

例如,如果 new VigenereCipher("SLIME").encrypt("GREEN")where"SLIME"是密钥密码,并且"GREEN"是应加密为的字符串YcMQR。但我的代码给出了?&|{.

考虑到字母表对象涵盖了所有内容 ="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz 1234567890!@#$%^&*()_+-=[]{}\\|;:'\",./?<>"

有人可以帮我吗?

public class VigenereCipher extends SymmetricCipher {

    protected String password;
    protected int passwordPos;

    public VigenereCipher(String password, Alphabet alphabet)
    {
        super(alphabet);
        this.password = password;
    }
    public VigenereCipher(String password)
    {
        super(Alphabet.DEFAULT);
        this.password = password    ;
    }

    public String getPassword()
    {
        return this.password;
    }



    public String encrypt(String s)
    {
        passwordPos = 0;
        String encrypted = "";
        for(int i = 0; i < s.length(); i++)
        {
            char c = s.charAt(i);
            encrypted += encrypt1(c);

        }


        return encrypted;
    }

    protected char encrypt1(char c)
    {
        //Alphabet temp = new Alphabet(s);
        int index = 0;
        char result = 0;

        index = alphabet.indexOf(c); //Found index of a character 

        if(index != -1)
        {
            int keyIndex = alphabet.get(passwordPos++ % password.length());
            result = alphabet.get((keyIndex + index) % alphabet.length());
        }
        else 
            throw new NotInAlphabetException(c, alphabet);
        return result;
    }

    public String decrypt(String c)
    {
        return c;
    }

    protected char decrypt1(char c)
    {
        return c;
    }

    public String toString()
    {
        return "Vigenere Cipher (password =\'"+this.password+"\')"; 
    }



}


public abstract class SymmetricCipher extends Cipher {

    protected Alphabet alphabet;

    public SymmetricCipher (Alphabet alphabet)
    {
        this.alphabet = alphabet;
    }

    public int wrapInt(int i)
    {
        int index = 0;
        if (i >= alphabet.length())
            index = Math.abs(i) % alphabet.length();
        else if (i < 0)
        {
            int temp = Math.abs(i) % alphabet.length();
            index = alphabet.length() - temp;
        }
        else 
            index = i;

        return index;
    }

    public int rotate(int index, int shift)
    {
        int result = 0;

        if (shift > 0)
        {
            result = (index + shift) % alphabet.length();
        }

        else if (shift < 0)
        {
            if(index < Math.abs(shift))
            {
                int temp = Math.abs(index + shift);
                result = alphabet.length() - temp;

            }
            else 
                result = index + shift ;
        }   


        return result;
    }

    public Alphabet getAlphabet()
    {
        return this.alphabet;
    }

    public String encrypt(String s) 
    {
        String string = "";
        char c = 0;
        char encrypted = 0;
        for (int i = 0; i < s.length(); i++)
        {
            c = s.charAt(i);
            encrypted = encrypt1(c);
            string += encrypted;
        }
        return string;


    }

    public String decrypt(String s) throws NotInAlphabetException
    {
        String string = "";
        char c = 0;
        char encrypted = 0;
        for (int i = 0; i < s.length(); i++)
        {
            c = s.charAt(i);
            encrypted = encrypt1(c);
            string += encrypted;
        }
        return string;


    }

    protected abstract char encrypt1(char c);

    protected abstract char decrypt1(char c);


}



public class Alphabet {


    private String symbols;
    public static final Alphabet DEFAULT = new Alphabet("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz 1234567890!@#$%^&*()_+-=[]{}\\|;:'\",./?<>");

    public Alphabet(String symbols)
    {
        this.symbols = symbols;
    }

    public int indexOf(char c) 
    {
        Alphabet temp = new Alphabet(symbols);
        for(int i = 0; i < symbols.length(); i++)
        {
            if(c == symbols.charAt(i))
                return symbols.indexOf(c) ;
        }

        throw new NotInAlphabetException (c, temp); 
    }

    public char get(int i) 
    {
        Alphabet temp = new Alphabet(symbols);
        char c = 0;
        if (i > this.symbols.length())
            throw new NotInAlphabetException (c, temp);
        else 
            return symbols.charAt(i);
    }

    public int length()
    {
        return symbols.length();
    }

    public String getSymbols()
    {
        return symbols;
    }

    public String toString()
    {
        return "Alphabet("+this.symbols+")";
    }

    public boolean equals(Object other)
    {
        if(other instanceof Alphabet)
        {
            Alphabet temp = (Alphabet) other;
            return this.symbols.equals(temp.symbols);

        }
        else 
            return false;
    }
}
4

1 回答 1

1

问题出在encrypt1函数的这一行:

int keyIndex = alphabet.get(passwordPos++ % password.length());

在这里,您尝试在密码中查找密钥,但实际上是从字母表中获取的。您想要的是从密码中找到相关字符(当您到达末尾时循环),然后找出该字符在您的字母表中的索引,这样您就可以获取平面文本并将其移动( index) 字母表中的许多字符。

为此,您可以使用以下代码:

char pwChar = password.charAt(passwordPos++ % password.length());
int keyIndex = alphabet.indexOf(pwChar);

当我更改这一行并使用SLIME密钥和GREEN明文运行您的代码时,我得到了结果YcMQR,这就是您所说的您所期望的。

于 2015-03-01T07:30:18.087 回答