Java加密/解密分配:抵消字符

时间:2015-02-06 20:18:17

标签: java

我坚持使用我的一类代码。我的教授鼓励在这样的论坛上提问,特别是因为它给了他较少的问题:),所以我想我会问你所有人的帮助。

我的任务的目的是通过移动或解除字符来加密和解密输入字符串,而不是用户告诉它的次数。我的代码如下。

出于某种原因,我在解密加密文本时遇到错误,并且在运行我的代码时错误只发生在6或更多的数字上,因此如果使用教授的示例并加密" subterfuge& #34;抵消6个字符来制作" yahzkxlamk"然后尝试解密文本以再次偏移6个字符以进行" subterfuge",它给了我一个错误。错误是

java.lang.StringIndexOutOfBoundsException: String index out of range: -6

当我使用相同的输入字符串运行代码时," subterfuge",但是偏移量为5或更小,它可以工作。该错误据说发生在下面代码的第65行,它表示

sb.append(alphabet.charAt(offset));

在最后Decrypt()语句中的else方法的末尾。

import javax.swing.*;

public class Encryptor {


private String plainText;
private int shift;
public String cipherText;


public Encryptor() {
    plainText = null;
    shift = 0;
}

public static void main(String[] args) {


    //encryption block
    Encryptor e = new Encryptor();
    String strCipherText = e.Encrypt();
    System.out.println("encrypted text");
    System.out.println(strCipherText);

    //decrypt block
    Encryptor d = new Encryptor();

    //cipher text becomes the input text to the Decrypt method
    d.cipherText = strCipherText;

    String strPlainText = d.Decrypt();
    System.out.println("decrypted text");
    System.out.println(strPlainText);

    System.exit(0);


}//end of main method

public String Decrypt()
{

            plainText = cipherText;

            shift = Integer.parseInt(JOptionPane.showInputDialog("enter offset"));

            int offset=0;
            int newOffset=0;
            String alphabet ="abcdefghijklmnopqrstuvwxyz";
            StringBuffer sb = new StringBuffer();
            int index = plainText.length();
            for(int i=0;i<index;i++)
            {
                String temp = "" + plainText.charAt(i);

                offset = alphabet.indexOf(temp);
                offset -= shift;
                if(offset > 25)
                {
                    newOffset = offset % 26;
                    sb.append(alphabet.charAt(newOffset));
                }
                else
                {
                    sb.append(alphabet.charAt(offset));
                }
            }//end of for loop
            return sb.toString();// return encrypted string
}

public String Encrypt()
{

    plainText = ((String)JOptionPane.showInputDialog("enter words " + "to encrypt")).toLowerCase().trim();


    shift = Integer.parseInt(JOptionPane.showInputDialog("enter offset"));

    int offset=0;
    int newOffset=0;
    String alphabet = "abcdefghijklmnopqrstuvwxyz";
    StringBuffer sb = new StringBuffer();
    int index = plainText.length();
    for(int i=0;i<index;i++)
    {
        String temp = "" + plainText.charAt(i);

        offset = alphabet.indexOf(temp);
        offset += shift;
        if(offset > 25)
        {
            newOffset = offset % 26;
            sb.append(alphabet.charAt(newOffset));
        }
        else
        {
            sb.append(alphabet.charAt(offset));
        }
    }//end of for loop
    return sb.toString();// return encrypted string
}

}

1 个答案:

答案 0 :(得分:4)

这是你的问题:

offset = alphabet.indexOf(temp);
offset -= shift;
if(offset > 25)
{
    newOffset = offset % 26;
    sb.append(alphabet.charAt(newOffset));
}
else
{
    sb.append(alphabet.charAt(offset));//< New offset is less than 0
}

你想要的只是一个积极的mod函数。所以,只需在进行模块化部门后添加:

while(newOffset < 0)
    newOffset += 26;

我倾向于做的就是为此做一个函数:

/* Positive modular division. */
public static int pmod(int num, int mod)
{
    num %= mod;
    if(num < 0) num += mod;
    return num;
}