0

初心者のプログラミング能力への挑戦として、単純なブルートフォースパスワードをコーディングできるかどうかを確認するのは楽しいだろうと思いました。そこで、文字列の長さの値を指定して、使用できるすべての英数字の順列を生成するアプリケーションの作成を開始しました。しかし、私は完全なプログラミング初心者なので、問題を抱えています。

まず、java.lang.Mathをインポートしたにもかかわらず、シンボルが見つからないというエラーが発生します:pow。完全なjava.lang.Math.pow();で書き出すことで、これを修正することができました。代わりに関数を使用する場合、それが機能するのにインポートが機能しない理由は私を超えています。

次に、入力した長さに関係なく、入力した後、ランタイムエラーが発生します。

aaException in thread "main" java.lang.ArrayIndexOutOfBoundsException: 98
at combination.main(combination.java:53)

これは、53行目で次のことを示唆しています。

current[j] = alphanum[((int)current[j])+1];

current[]またはalphanum[]のいずれかでインデックス98にアクセスしようとしていますか?私が見る限り、これは起こってはならないことです...

私はこの開発にかなり困惑しています。とにかく、これが私のコードです:

//48-57 65-90 97-122

import java.util.Scanner;
import java.lang.Math;

public class combination {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        //Alphanum will be an array of chars: the lowercase letters of the alphabet, the uppercase, and the numbers 0-9.
        char[] alphanum = new char[62];

        //Set indexes 0-25 as lowercase a-z, and indexes 26-51 as uppercase A-Z, using ascii conversion.
        for (int i=0; i<26; i++) {
            alphanum[i] = (char)(i+97);
            alphanum[i+26] = (char)(i+65);
        }

        //Set indexes 51-61 as 0-9.
        for (int i=0; i<10; i++) {
            alphanum[i+52] = (char)(i+48);
        }

        //Take in variable for length.
        System.out.print("Enter length: ");
        int length = in.nextInt();

        //Current will be an array of chars: it will hold the current permutation being generated.
        char[] current = new char[length];

        //Set all indexes in current to "a" by default, and print this string as the first permutation.
        for (int i=0; i<length; i++) {
            current[i] = alphanum[0];
            System.out.print(current[i]);
        }

        //power will be a temporary double, used to calculate the number of iterations needed, as the pow function works with doubles.
        double power = (java.lang.Math.pow(62.00, ((double)length)));

        //Convert power to an integer, iterations, and subtract 1 because one iteration was already printed previously.
        int iterations = ((int)power)-1;

        /*The loop works like this. The rightmost char is checked, and if it is the maximum value of the idex
        it is reverted to idex 0 again and the index value of the char to the left of it is increased by 1,
        if it is not the maximum then it is just increased by 1. This is iterated the right number of times such
        that every alphanumeric permutation of that length has been returned.*/
        for (int i=0; i<iterations; i++) {
            for (int j=(length-1); j>=0; j--) {
                if ((j!=0) && (((int)current[j])==122)) {
                    current[j] = alphanum[0];
                    current[j-1] = alphanum[((int)current[j-1])+1];
                } else if (j!=0) {
                    current[j] = alphanum[((int)current[j])+1];
                } else {
                    System.out.println("This has looped too many times. Something is wrong.");
                }
            }

            //At the end of each iteration, print the string.
            for (int l=0; l<length; l++) {
                System.out.print(current[l]);
            }
        }
    }
}

私はあなたが提供できるどんな助けや洞察にも本当に感謝するでしょう。^ _ ^

4

2 に答える 2

4

配列alphanumのサイズは62で、の意味((int)current[j-1])+1は98(> 62)です。

char'a'のint値は97です。

于 2011-12-10T19:33:50.340 に答える
1

current[]またはalphanum[]のいずれかでインデックス98にアクセスしようとしていますか?私が見る限り、これは起こってはならないことです...

インデックスが現在の配列の内容から派生している特定のインデックスでalphanumの要素にアクセスしようとするため、これは完全に可能です。これらの配列の内容をさまざまな手順で印刷することをお勧めします。そうすれば、コードが思ったとおりに動作しない場所をすぐに見つけることができます。

current[j] = alphanum[((int)current[j])+1];

ここでアクセスしようとします

int index = ((int)current[j])+1;
current[j] = alphanum[index];

インデックスは98のようです

于 2011-12-10T19:32:37.153 に答える