0

我昨天发布了这个类似的帖子,以为我已经完成了,但是当我运行测试用例 4/20decrypt1()时,Caesar class. 在 中,如果我运行以下测试用例(失败)Caesar class,我试图找出我的方法代码有什么问题:decrypt1()

@Test (timeout=2000)
public void test_Caesar_15(){
    assertEquals('Q',new CaesarCipher(100) .decrypt1('X'));
}

答案应该是81当我得到的只是101。另一种情况是这样的:

@Test (timeout=2000)
public void test_Caesar_21(){
    assertEquals("catfood",new CaesarCipher(17, lowerSpace).decrypt("trjweeu"));
}

当输出应该是catfood但我得到这样的smth ca fvvd。smb 可以帮助我使用 decrypt1() 方法/指导我修复代码吗?假设 Alphabet 对象包括在 中显示的每个Alphabet class字符DEFAULT。我在这里包含了三个互连器类,因此可以更轻松地跟踪decrypt1()方法中的错误。

public class CaesarCipher extends SymmetricCipher {

    protected int shift;

    public CaesarCipher(int shift, Alphabet alphabet)
    {
        super(alphabet);
        this.shift = shift;

    }

    public CaesarCipher(int shift)
    {

        super(Alphabet.DEFAULT);
        this.shift = shift;
    }

    public String encrypt(String s)
    {
        String encrypted = super.encrypt(s);
        return encrypted;


    }

    public char encrypt1(char c) throws NotInAlphabetException
    {

        int index = 0;
        int rotateIndex = 0;
        char shiftedChar = 0;

        index = alphabet.indexOf(c);
        if((index + shift) > alphabet.length())
        {
            rotateIndex = rotate(index, shift);
            shiftedChar = alphabet.get(rotateIndex);
        }
        else if((index + shift) < 0)
        {
            rotateIndex = rotate(index, shift);
            shiftedChar = alphabet.get(rotateIndex);
        }
        else
        {
            index += shift;
            shiftedChar = alphabet.get(index);
        }

        return shiftedChar;


    }

    public String decrypt(String s)
    {
        String decrypted = super.decrypt(s);
        return decrypted;
    }
    public char decrypt1(char c)
    {
        int index = 0;
        int rotateIndex = 0;
        char shiftedChar = 0;

        index = alphabet.indexOf(c);

        if((index - shift) > alphabet.length())
        {
            rotateIndex = rotate(index, shift);
            shiftedChar = alphabet.get(rotateIndex);
        }
        else if((index - shift) < 0)
        {

            rotateIndex = rotate(index, shift);
            shiftedChar = alphabet.get(rotateIndex);
        }
        else
        {
            index = index - shift;
            shiftedChar = alphabet.get(index);
        }

        return shiftedChar;
    }

    public String toString()
    {
        return "Caesar Cipher (shift="+shift+")";
    }

}


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 decrypted = 0;
        for (int i = 0; i < s.length(); i++)
        {
            c = s.charAt(i);
            decrypted = decrypt1(c);
            string += decrypted;
        }
        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

0

我重构了您的一些代码以将关注点分开。例如,decrypt1() 和 encrypt1() 方法打开了索引 -/+ 移位的差/和。但是这段代码逻辑上属于rotate()。考虑到这一点,decrypt1() 和 encrypt1() 成为明确的函数:

@Override
public char encrypt1(char c) throws NotInAlphabetException {

    int index = alphabet.indexOf(c);
    int rotateIndex = rotate(index, shift);
    char shiftedChar = alphabet.get(rotateIndex);

    return shiftedChar;

}

@Override
public char decrypt1(char c) {

    int index = alphabet.indexOf(c);
    int rotateIndex = rotate(index, -shift);
    char shiftedChar = alphabet.get(rotateIndex);

    return shiftedChar;
}

这显然是凯撒密码的代码:加密将字符从 indexC = indexP + shift 转移,解密将其取回 indexP = indexC - shift。这将转移的所有重量转移到旋转中。

最重要的是,现在轮换支持负转移。请注意,按字母表大小的移位与根本没有移位相同,因此我们可以继续将字母表大小添加到移位中,直到移位为正。您已经考虑了 shift 将索引带到字母表之外的其他特殊情况。以下轮换解决了这两个问题:

public int rotate(int index, int shift) {

    while (shift < 0) {
        shift += alphabet.length();
    }

    return (index + shift) % alphabet.length();
}

假设较低的空间是

Alphabet lowerSpace = new Alphabet("abcdefghijklmnopqrstuvwxyz ");

以上功能解决了您所显示的问题。我应该指出,您不需要在 CaesarCipher 中覆盖 encrypt() 和 decrypt(),因为您只是不必要地将显式的动态调度层添加回超类。

于 2015-03-01T20:55:08.627 回答