Current location - Quotes Website - Signature design - Java md5 encryption index page code
Java md5 encryption index page code
/**?

*? Converts the specified byte array into a 16 string?

*? @param? b?

*? @ Return?

*/?

Public? Static electricity String? byteToHexString(byte[]? b)? {

StringBuffer? hexString? =? New? string buffer();

For what? (int? Me? =? 0; ? Me? & lt? B. length; ? i++)? {

String? Black magic? =? Integer.toHexString(b[i]? & amp? 0x ff);

What if? (hex.length()? ==? 1)? {

Black magic? =? '0'? +? Hexadecimal;

}

hex string . append(hex . toupper case());

}

Return? hex string . tostring();

}

/**?

*? Get the encryption password in 16?

*? @param? Password?

*? @ Return?

*? @throws? NoSuchAlgorithmException?

*? @throws? UnsupportedEncodingException?

*/?

Public? Static electricity String? GetEncryptedPwd (string? Password)

Throwing? NoSuchAlgorithmException,? UnsupportedEncodingException? {

//Declare an encrypted password array variable

byte[]? pwd? =? null

//random number generator

SecureRandom? Random? =? New? SecureRandom();

//Declare a salt array variable

byte[]? Salt? =? New? Bytes [salt _ length];

//Put the random number into the salt variable.

random . nextbytes(salt);

//Declare the message digest object

MessageDigest? md? =? null

//Create message digest

md? =? message digest . getinstance(" MD5 ");

//Pass salt data into the message digest object.

MD . update(salt);

//Pass the password data to the message digest object.

MD . update(password . getbytes(" UTF-8 "));

//Gets the byte array of message digest.

byte[]? Digestion? =? MD . digest();

//Because the salt is to be stored in the byte array of the password, the byte length of the salt is increased.

pwd? =? New? byte[digest.length? +? SALT _ LENGTH];

//Copy the bytes of the salt to the first 12 byte of the generated encrypted password byte array, so as to take out the salt when verifying the password.

System.arraycopy(salt,0,? pwd,? 0,? Salt _ length);

//From the 13 byte, copy the message digest to the bytes of the encrypted password byte array.

System.arraycopy(digest,0,? pwd,? Salt _ length,? digest . length);

//Convert the encrypted password in byte array format to the password in 16 hexadecimal string format.

Return? byteToHexString(pwd);

}

Public? Static electricity Invalid? main(String[]? args)? {//test method

Try it? {

system . out . println(getEncryptedPwd(" 123456 "));

? }? Catch? (NoSuchAlgorithmException? e)? {

? //? Todo? Automatically generated? Catch? block

? e . printstacktrace();

}? Catch? (UnsupportedEncodingException? e)? {

//? Todo? Automatically generated? Catch? block

e . printstacktrace();

}

} You can call getEncryptedPwd in jsp pages? way