我有一个关于MD5的问题,以及我如何设置任何数字的字符串,以便创建如下:
private static final String VALID_MD5 = "DF3AEBC649F9E3B674EEB790A4DA224E";
这是设置的,以便字符串编号实际上是6357.我如何设置它以取任何我想要的4位数字。以下是模拟安全系统的完整代码供参考。 (请注意我明白MD5并不完全安全。):
//import message digest, one way hash functions that take data and output fixed hash value
//note this technically is not secure and can like most things be cracked with brute force but as this is a sample and will not be put into real life use it is an easy way to make a "mock" secure system
import java.security.MessageDigest;
//import scanner for user to enter own numbers
import java.util.Scanner;
//new class named pin
public class pin
{
public static void main( String[] args )
{
if( Login.login() )
{
//purposely left blank class, this is where the code executed would go after successfully activating the correct pin
}
}
}
// a new separate class specific to login
class Login
{
//private instant variables
private static final Scanner ONE = new Scanner( System.in );//create a new scanner and call it in
private static final String VALID_MD5 = "DF3AEBC649F9E3B674EEB790A4DA224E"; //refers to pin 6357, pin string passed to MD5 methods returns array of 16 seemingly random bytes, for convenience converted to a 32 character hex string for storage and comparison
//use boolean for true or false, etc
public static boolean login()
{
System.out.print( "Enter pin: " );//enter pin using util scanner
String pin = ONE.nextLine();//go to next line
if( isValid( pin ) && VALID_MD5.equals( md5HexString( pin ) ) )//if the string is valid(refer to below in boolean) and Valid_MD% equals the hexstring(pin)do the following statements
{
//print out and return true if pin matches
System.out.println( "Login successful." );
return true;
}
//print out and return false if pin does not match
System.out.println( "Login failed, invalid pin." );
return false;
}
//return if String s is valid under the condition of being 4 digits long using boolena for true or false
private static boolean isValid( String s )
{
return s.matches( "\\d{4}" ); // the pin entered through scanner must 4 digits long
}
//String md5HexString is String s
private static String md5HexString( String s )
{
return toHexString( md5( s ) );// return s to HexString
}
//bytes
private static byte[] md5( String s )
{
try
{
MessageDigest md = MessageDigest.getInstance( "MD5" );//get instance of md, md5
byte[] buf = s.getBytes();//get bytes
md.update( buf, 0, buf.length );
return md.digest();//return
}
catch( Exception ex )//use catch exception ex instead of just catch exception, allows access to exception class(error cause) instance for try
{
return new byte[16];
}
}
private static String toHexString( byte[] byteArray )
{
final String HEX_CHARS = "0123456789ABCDEF"; //16
byte[] result = new byte[byteArray.length << 1];
int len = byteArray.length; //variable for Array length
for( int i = 0 ; i < len ; i++ )//for i is 0 and i is less than byte array length add one
{
byte b = byteArray[i];//next byte from array
int lo4 = b & 0x0F;//l0 4 bits, 0-15
int hi4 = ( b & 0xF0 ) >> 4;//hi 4 bits, 0-15
//fill with two ASCII characters, one hi 4 bits other for low 4 bits, hi nibble in hexadecimal and low nibble, nibbles are half a byte or also a hex digit, used to represent a single hexadecimal digit
result[i * 2] = (byte)HEX_CHARS.charAt( hi4 );
result[i * 2 + 1] = (byte)HEX_CHARS.charAt( lo4 );
}
return new String( result );//convert the result into a string
}
}
答案 0 :(得分:1)
MD5总和来自字符串输入$ echo -n 6357 | md5sum
df3aebc649f9e3b674eeb790a4da224e -
:
1234
如果要检查用户是否输入了不同的四位数字,请计算此数字的MD5总和并更改您的代码。如果您想检查输入的1234
,请预先计算$ echo -n 1234 | md5sum
81dc9bdb52d04dc20036dbd8313ed055 -
private static final String VALID_MD5 = "81dc9bdb52d04dc20036dbd8313ed055";
并更改您的代码:
{{1}}
有关MD5的信息可从many source获得。