import
java.io.UnsupportedEncodingException;
import
java.security.MessageDigest;
import
java.security.NoSuchAlgorithmException;
public
class
MainActivity {
public static boolean verifyPassword( String
input
) {
/
/
这里修改下传入值
if
(
input
.length() !
=
4
) {
return
false;
}
byte[] v
=
encodePassword(
input
);
byte[] p
=
"09042ec2c2c08c4cbece042681caf1d13984f24a"
.getBytes();
if
(v.length !
=
p.length) {
return
false;
}
for
(
int
i
=
0
; i < v.length; i
+
+
) {
if
(v[i] !
=
p[i]) {
return
false;
}
}
return
true;
}
private static byte[] encodePassword(String
input
) {
byte[] SALT
=
{
95
,
35
,
83
,
73
,
75
,
35
,
95
};
try
{
StringBuilder sb
=
new StringBuilder();
sb.append((char) SALT[
0
]);
sb.append((char) SALT[
1
]);
for
(
int
i
=
0
; i <
input
.length(); i
+
+
) {
sb.append((char)
input
.getBytes(
"iso-8859-1"
)[i]);
sb.append((char) SALT[i
+
2
]);
}
sb.append((char) SALT[
6
]);
byte[] bArr
=
new byte[
0
];
return
SHA1(sb.toString()).getBytes(
"iso-8859-1"
);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return
null;
}
}
private static String convertToHex(byte[] data) {
StringBuffer buf
=
new StringBuffer();
for
(
int
i
=
0
; i < data.length; i
+
+
) {
int
halfbyte
=
(data[i] >>>
4
) &
15
;
int
two_halfs
=
0
;
while
(true) {
if
(halfbyte <
0
|| halfbyte >
9
) {
buf.append((char) ((halfbyte
-
10
)
+
97
));
}
else
{
buf.append((char) (halfbyte
+
48
));
}
halfbyte
=
data[i] &
15
;
int
two_halfs2
=
two_halfs
+
1
;
if
(two_halfs >
=
1
) {
break
;
}
two_halfs
=
two_halfs2;
}
}
return
buf.toString();
}
private static String SHA1(String text) {
try
{
MessageDigest md
=
MessageDigest.getInstance(
"SHA-1"
);
byte[] bArr
=
new byte[
40
];
md.update(text.getBytes(
"iso-8859-1"
),
0
, text.length());
return
convertToHex(md.digest());
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e2) {
e2.printStackTrace();
}
return
null;
}
/
/
以上均为粘贴代码
public static void main(String[] strArr) {
int
i;
for
(i
=
999
;i<
10000
; i
+
+
) {
String sn
=
String.valueOf(i);
if
(verifyPassword(sn)){
System.out.println(
"success:"
+
i);
break
;
}
else
{
System.out.println(
"fail:"
+
i);
}
}
}
}