如果我有特定的密钥长度(例如4)和给定的密文,我该如何使用str.matches("^[a-zA-Z0-9\\-#\\.\\!\\;\\,\\(\\)\\/%&\\s]*$"))
查找密钥以获取明文。
到目前为止,我有一个用于生成固定大小的字符串的类:
class Get {
char[] alphabet; // stores all possible letters as a character array
int keyLength; // how long the key is
int x = 0; // internal count of the number of strings generated
int max; // maximum number of strings to be generated
/*
* keyLength: length of the key strings to be generated
*/
Guess(int keyLength) {
alphabet = "abcdefghijklmnopqrstuvwxyz".toCharArray(); // use English's
this.keyLength = keyLength; // save key length
this.max = (int) Math.pow(alphabet.length, keyLength) - 1; // maximum number of guesses
}
/*
* Generate a new string of keyLength long
* Returns null if all strings have been generated
*/
String gen() {
String ret = null;
if(x < max) {
char[] c = new char[keyLength];
int[] y = toDigits(x);
for(int i = 0; i<keyLength; i++) {
c[i] = alphabet[y[i]];
}
ret = new String(c);
x = x + 1;
}
return ret;
}
/*
* Convert a number x into digits using the size of the alphabet as base
*/
int[] toDigits(int x) {
int[] ret = new int[keyLength];
for(int i = 1; i <= keyLength; i++) {
ret[keyLength - i] = x % alphabet.length;
x = x / alphabet.length;
}
return ret;
}
我想学习如何编写一种方法,该方法将遍历所有可能的密钥,尝试使它们查看密文是否为纯文本,以及是否打印出消息和找到的密钥