我需要用Java编写算法来验证密码字段。 在所有基本控件之间(最小长度,lumhezza massima,字母数字...),我必须检查密码是否不是由键盘上的顺序字符组成的(例如 QWERTY,YTREWQ,ASDFGH 等)。 密码的最小长度为8个字符。密码在4位数字后无效 我无法在数据库中使用“禁止”字符串,关于现有算法的任何提示,或者是否有图书馆已经在进行这种检查,来创建“字典表”?
答案 0 :(得分:0)
嗯...我想到一件事。
您可以尝试构建一个字母矩阵:
-----------------------
| q w e r t y ...
| a s d f g h ... etc.
| z x c v b n ...
-----------------------
给每个字母一个索引。例如,“ q”具有[1,1],“ w”具有[2,1],“ v”具有[4,3],依此类推。
现在,给定一个字符串,找到每个字母之间的距离并求和。
示例:
给定字符串“ sdgh”:
s-d, distance is 1
d-g, distance is 2
g-h, distance is 1
Total distance is: 4
现在您的比率为4/4。 (四个字母,总距离为四个)。
您的安全要求可以吗?回答这个问题,您就完成了。
答案 1 :(得分:0)
好的,这是我的解决方案,对于水平检查似乎效果很好。如果距离> 4,则表示密码无效。有任何改进的提示吗?我知道它有点硬编码,但目前我可以通过它。
@Test
public void runCheckDigit() {
checkdigit("cnqwerty13");
}
public void checkdigit(String password) {
int x = 4;
int y = 10;
String[][] keyboard = new String[x][y];
// first row
keyboard[0][0] = "1";
keyboard[0][1] = "2";
keyboard[0][2] = "3";
keyboard[0][3] = "4";
keyboard[0][4] = "5";
keyboard[0][5] = "6";
keyboard[0][6] = "7";
keyboard[0][7] = "8";
keyboard[0][8] = "9";
keyboard[0][9] = "0";
// second row
keyboard[1][0] = "q";
keyboard[1][1] = "w";
keyboard[1][2] = "e";
keyboard[1][3] = "r";
keyboard[1][4] = "t";
keyboard[1][5] = "y";
keyboard[1][6] = "u";
keyboard[1][7] = "i";
keyboard[1][8] = "o";
keyboard[1][9] = "p";
// TERZA RIGA
keyboard[2][0] = "a";
keyboard[2][1] = "s";
keyboard[2][2] = "d";
keyboard[2][3] = "f";
keyboard[2][4] = "g";
keyboard[2][5] = "h";
keyboard[2][6] = "j";
keyboard[2][7] = "k";
keyboard[2][8] = "l";
keyboard[2][9] = "ò";
// third row
keyboard[3][0] = "z";
keyboard[3][1] = "x";
keyboard[3][2] = "c";
keyboard[3][3] = "v";
keyboard[3][4] = "b";
keyboard[3][5] = "n";
keyboard[3][6] = "m";
keyboard[3][7] = ",";
keyboard[3][8] = ".";
keyboard[3][9] = "-";
printMatrix(keyboard, x, y);
List<Coordinata> cList = new ArrayList<Coordinata>();
for (int i = 0, n = password.length(); i < n; i++) {
char s = password.toLowerCase().charAt(i);
cList.add(getCoordinate(s, keyboard));
}
int distanza = 0;
for (int i = 0; i < cList.size()-1; i++) {
distanza = distanza + distanceElement(cList.get(i), cList.get(i + 1));
}
System.out.println("distanza : " + distanza);
}
private static Coordinata getCoordinate(char s, String[][] keyboard) {
Coordinata c = null;
for (int i = 0; i < keyboard.length; ++i) {
for (int j = 0; j < keyboard[0].length; ++j) {
if (keyboard[i][j].equals(Character.toString(s))) {
// Found the correct i,j
return c = new Coordinata(i, j, Character.toString(s));
}
}
}
return c;
}
private static int distanceElement(Coordinata c1, Coordinata c2) {
int distance = 0;
distance = Math.abs(c2.getX() - c1.getX()) + Math.abs(c2.getY() - c1.getY());
System.out.println("Distance: " + c1.getLettera() + "->" + c2.getLettera() + " = " + distance);
return distance;
}
private static void printMatrix(String[][] matrix, int matrixRow, int matrixCol) {
System.out.println("Matrix is : ");
for (int i = 0; i < matrixRow; i++) {
for (int j = 0; j < matrixCol; j++) {
System.out.print(matrix[i][j] + "\t");
}
System.out.println();
}
}