【问题标题】:ROT-N (or ROT-X) function in JavaJava中的ROT-N(或ROT-X)函数
【发布时间】:2017-10-02 16:39:54
【问题描述】:

目前,我正在编写一个程序,该程序使用 Java 在给定的字符串上执行 ROT-1 直到并包括 ROT-25。在我研究之初,我发现this代码:

public class Rot13 { 

public static void main(String[] args) {
    String s = args[0];
    for (int i = 0; i < s.length(); i++) {
        char c = s.charAt(i);
        if       (c >= 'a' && c <= 'm') c += 13;
        else if  (c >= 'A' && c <= 'M') c += 13;
        else if  (c >= 'n' && c <= 'z') c -= 13;
        else if  (c >= 'N' && c <= 'Z') c -= 13;
        StdOut.print(c);
    }
    StdOut.println();
}
}

经过一些故障排除后,我得到了这个:

private static void rotALL(String input) {
//Loop 25 times, starting with ROT-1 and ending at ROT-25 (every possibliity besides the original input)
    for (int i = 1; i < 26; i++) {
        int rot = 26 - i;
        System.out.print("ROT" + rot + ": ");    
        for (int charIndex = 0; charIndex < input.length(); charIndex++) {
            char c = input.charAt(charIndex);

            int inta = 97; //a in the ASCII table
            int intaWithRot = inta + rot;
            int intA = 65; //A in the ASCII table
            int intAWithRot = intA + rot;

            int intaWithRotPlusOne = intaWithRot + 1;
            int intaWithRotPlusi = intaWithRot + i;
            int intAWithRotPlusOne = intAWithRot + 1;
            int intAWithRotPlusi = intAWithRot + i;

            if (c >= inta && c <= intaWithRot) {
                c += rot;
           } else if (c >= intA && c <= intAWithRot) {
                c += rot;
            } else if (c >= intaWithRotPlusOne && c <= intaWithRotPlusi) {
                c -= rot;
            } else if (c >= intAWithRotPlusOne && c <= intAWithRotPlusi) {
                c -= rot;
            }
            System.out.print(c);
        }
        System.out.println();
    }

现在我要解决问题了:

  1. 当我使用 ROT-13 输入“grfg qngn”,即“测试数据”时,ROT-13 的输出是“ROT13: test d{t{”、“{”和“a "在ASCII表中相距26位,但我不知道为什么会出现这个错误,当“e”等字母正确显示时。

  2. 如何更改此算法,使其通过 ROT-1 到 ROT-25 循环?我认为这应该可以解决问题,但我错过了一些东西。

提前致谢和亲切的问候!

【问题讨论】:

  • 原代码有什么问题?
  • 它只适用于 ROT-13,而我想从 ROT-1 循环到 ROT-25。
  • 好的,我想你会发现模数运算符“%”会让你的事情变得更容易。
  • 我以前使用过模运算符,但我不知道如何在这个函数中有效地使用它。你能再解释一下吗?

标签: java encryption decode substitution


【解决方案1】:

有数百万种方法可以解决这个问题,作为学习者,您应该探索所有方法。我对使用模“%”运算符的评论可以通过这个小方法来说明:

private static char rotateLower(char c, int rot) {
    int baseBand = c - 'a';
    int modified = (baseBand + rot) % 26;
    return (char) (modified + 'a');
}

【讨论】:

  • 我确实了解函数的输出以及使用它的原因。但是,我不明白我会在我的程序中在哪里使用此代码的输出?编辑:此外,我认为我还需要一个带有“A”而不是“a”的 rotateUpper。
  • 是的,您还需要rotateUpper。或者,您可以有一个更通用的方法,称为rotateRange,它将范围的开头作为参数。在您的程序中,如果您正在检查的当前字符在较低范围内,则 rotateLower;如果它在上限范围内,则rotateUpper;如果它不在两个范围内,则保持不变。
  • 但是我怎样才能得到两个字符而不是一个呢?如果我使用if (c &gt;= rotateLower(c, rot) &amp;&amp; c &lt;= rotateLower(c, rot)) { c += rot;,那么它会提供两个相同的字符。
  • @clanc:您将不得不更多地考虑这个问题,因为我故意没有提供完整的解决方案。
  • 我已经为此工作了将近一整天,并且非常卡住。如果我使用char inta = rotateLower(c, rot);,我会得到'f' 作为我的第一个字符。我的另一个边界char intaWithRot = (char) (inta + rot); 是 '(',在 ASCII 图表上是 dec40,而 'f' 是 dec102。这里有些东西,但我对什么一无所知。
【解决方案2】:
/**
 * Returns a list with Strings which are rotated ROT-n. n = 26 - listIndex
 *
 * Source: http://www.rot-n.com/?page_id=4 
 *
 * @param input the string to mutate
 * @param numeric include numeric values
 * @return a list with mutated strings
 */
private static List<String> rotN(String input, boolean numeric) {
    List<String> output = new ArrayList<>();
    for (int n = 0; n < 26; n++) {
        String result = "";
        int length = input.length();

        for (int i = 0; i < length; i++) {
            char ascii = input.charAt(i);
            char rotated = ascii;
            //Capital letters are 60 to 90
            if (ascii > 64 && ascii < 91) {
                rotated = (char) (rotated + n);
                if (rotated > 90) {
                    rotated += -90 + 64;
                }
                if (rotated < 65) {
                    rotated += -64 + 90;
                }
            } else if (ascii > 96 && ascii < 123) { //Lowercase letters are between 97 and 122
                rotated = (char) (rotated + n);
                if (rotated > 122) {
                    rotated += -122 + 96;
                }
                if (rotated < 97) {
                    rotated += -96 + 122;
                }
            }
            //Numeric values are between 48 to 57 
            if (numeric && ascii > 47 && ascii < 58) {
                rotated = (char) (rotated + n);
                if (rotated > 47) {
                    rotated += -57 + 47;
                }
                if (rotated < 58) {
                    rotated += -47 + 57;
                }
            }
            result += (char) rotated;
        }
        output.add(result);
    }
    return output;
}

这是我找到的解决方案,它正在工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-12
    • 2018-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多