MD5,任意一串数字如何转换成字节

MD5, how can any string of numbers be converted to bytes

我有一个关于 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
}
}

MD5 和由字符串输入生成 6357:

$ echo -n 6357 | md5sum
df3aebc649f9e3b674eeb790a4da224e  -

如果您想检查用户是否输入了不同的四位数字,请计算该数字的 MD5 和并更改您的代码。如果要检查 1234 是否已输入,请预先计算 1234

的 MD5 和
$ echo -n 1234 | md5sum
81dc9bdb52d04dc20036dbd8313ed055  -

并更改您的代码:

private static final String VALID_MD5 = "81dc9bdb52d04dc20036dbd8313ed055";

有关 MD5 的信息可从 many source 获得。