MD5 Encryption Analysis: JDK API: Get Object API: Encrypted API:
1 Packagecn.utils;2 3 Importjava.security.MessageDigest;4 Importjava.security.NoSuchAlgorithmException;5 6 /**7 * @authorCQY13 MD5 Encryption Tool class8 */9 Public classMd5utils {Ten One /** A * Get MD5 encryption - * - * @parampwd the * strings that need to be encrypted - * @returnstring string after strings are encrypted - */ - Public Staticstring Getpwd (string pwd) { + Try { - //Create a Cryptographic object +MessageDigest digest = messagedigest.getinstance ("MD5"); A at //method of invoking the cryptographic object, the encrypted action has been completed - byte[] bs =digest.digest (Pwd.getbytes ()); - //Next, we will optimize the results after encryption and follow the MySQL optimization approach . - //MySQL's optimization ideas: - //The first step is to convert all the data into positive numbers: -String hexstring = ""; in for(byteb:bs) { - //The first step is to convert all the data into positive numbers: to //Explanation: Why use b&255 + /* - * B: It would have been a byte type of data (1 bytes) 255: is an int type of data (4 bytes) the * Byte type data is operated with data of type int, automatic type promotion to int type eg:b: 1001 1100 (raw data) * * operation: b:0000 0000 0000 0000 0000 0000 1001 1100 255:0000 $ * 0000 0000 0000 0000 0000 1111 1111 results: 0000 0000 0000 0000Panax Notoginseng * 0000 0000 1001 1100 The temp at this time is an integer of type int - */ the inttemp = B & 255; + //The second step is to convert all the data into a 16 binary form A //Note: When converting, note if positive >=0&&<16, then if you use Integer.tohexstring (), it may cause a missing bit number the //Therefore, the temp needs to be judged + if(Temp < && temp >= 0) { - //manually fill in a "0" $hexstring = hexstring + "0" +integer.tohexstring (temp); $}Else { -hexstring = hexstring +integer.tohexstring (temp); - } the } - returnhexstring;Wuyi}Catch(nosuchalgorithmexception e) { the //TODO auto-generated Catch block - e.printstacktrace (); Wu } - return""; About } $ - /** - * @paramargs - */ A Public Static voidMain (string[] args) { +String pwd = md5utils.getpwd ("abc"); the System.out.println (PWD); - } $ the}
Simple MD5 Encryption Class (Md5utils) in Java