字符串构建中的逻辑错误

时间:2015-11-18 21:27:03

标签: java for-loop

我有一个程序,我写的是应该加密和解密一组预定的字符串。提供了主类,我必须编写依赖项,但是我的对象没有被正确调用,从而产生了大量的编译时错误。两个依赖项都可以自行编译,所以我必须在类之间的接口中犯了错误。它有相当多的代码,所以这里是一个pastebin链接:

http://pastebin.com/i2V51TJm

编辑:现在我遇到了重大的逻辑错误。我知道它在加密器中,也可能是解密器,但我之前已经使用过解密器,我在这里做的只是将X从固定值改为难度。

编辑2:现在已经修复了randChar,现在我只需要弄清楚我的en / decrypt循环中有什么问题。

这是我的新错误/输出:

==============================
Simulating difficulty level 0:
Encrypting String: kitty ...
Encrypted as: xvrdsoyttik
Decrypting String: xvrdsoyttik ...
Decrypted as: kkkkkkkkkkkk
Fail!
==============================
Simulating difficulty level 1:
Encrypting String: test string ...
Encrypted as: gqpyqfdqqwvcqnirts tset
Decrypting String: gqpyqfdqqwvcqnirts tset ...
Decrypted as: test strinqcvwqqdfqypqg
Fail!
==============================
Simulating difficulty level 2:
Encrypting String: Agent: James Bond ...
Encrypted as: dnjomBe bsveimqaiJa :tnegA
Decrypting String: dnjomBe bsveimqaiJa :tnegA ...
Decrypted as: Aet James Bon
Fail!
==============================

加密器:

/** 
    encryptString encrypts the string, as well as stores the original for later use
    @param str The string to be encrypted
    @return encrypted The encrypted string
    */
    public String encryptString(String str)
    {
        int length = str.length();
        origStr = str;
        int x = diff;
        StringBuilder codeRev = new StringBuilder(str).reverse();

         for (int pos = 0; pos <= length; pos++)
         {
            if (x <= length + diff) //removed 
            {
                codeRev.insert(x, randomLetter());
            }               
            x = x + diff + 1;
         }
         encryptedStr = codeRev.toString();
         return(codeRev).toString();

    }

解密:

/**
decryptString decrypts the code based on using the difficulty as the increment.
@param code The encrypted string
@return decrypted The decrypted string
*/
    public String decryptString(String code)
    {
        int length = code.length();
        int x = 0; //to 1
        encryptedStr = code;

        String codeRev = new StringBuilder(code).reverse().toString();

        StringBuilder decrypted = new StringBuilder();

         for (int pos = 0; pos <= length; pos++)
         {
            if (x < length) //removed <=
            {
                decrypted.append(codeRev.charAt(x));
            }               
            x = x + diff;
         }
         decryptedStr = decrypted.toString();
         return(decrypted).toString();
    }

随机字符制作者:

/**
    Generates a random letter
    @return letter The random letter
    */
    public char randomLetter()
    {
        Random ranChar = new Random();
        char cr = (char)(ranChar.nextInt(26)+ 'a');
        return cr;
    }

1 个答案:

答案 0 :(得分:2)

错误非常简单 - getDecryptedStr()中没有Decryptor方法,getEncryptedStr()中没有Encryptor方法

一般来说 - 如果你遇到这些错误,看起来你没有使用任何IDE。您应该开始使用ideaeclipse之一。您将立即看到此类编译错误