Java encryption algorithm-MD5 encryption and Hash hash encryption algorithm source code, encryption algorithm-md5

Source: Internet
Author: User

Java encryption algorithm-MD5 encryption and Hash hash encryption algorithm source code, encryption algorithm-md5

Package com. ompa. common. utils;

Import java. security. MessageDigest;
Import java. security. NoSuchAlgorithmException;

Import javax. crypto. Mac;
Import javax. crypto. SecretKey;
Import javax. crypto. spec. SecretKeySpec;

/**
* MD5 Encryption
*
* @ Author zhangcd
* @ Date 2016-4-29
*/
Public class EncryptUtil {

Private static final String MAC_NAME = "HmacSHA1 ";
Private static final String ENCODING = "UTF-8 ";
Private static final String key = "iloveyou ";

/***
* Use MD5 to generate a 32-bit md5 code
*/
Public static String string2MD5 (String inStr ){
MessageDigest md5 = null;
Try {
Md5 = MessageDigest. getInstance ("MD5 ");
} Catch (Exception e ){
System. out. println (e. toString ());
E. printStackTrace ();
Return "";
}
Char [] charArray = inStr. toCharArray ();
Byte [] byteArray = new byte [charArray. length];

For (int I = 0; I <charArray. length; I ++)
ByteArray [I] = (byte) charArray [I];
Byte [] md5Bytes = md5.digest (byteArray );
StringBuffer hexValue = new StringBuffer ();
For (int I = 0; I <md5Bytes. length; I ++ ){
Int val = (int) md5Bytes [I]) & 0xff;
If (val <16)
HexValue. append ("0 ");
HexValue. append (Integer. toHexString (val ));
}
Return hexValue. toString ();

}

/***
* MD5 encryption generates 32-bit md5 code
*/
Public static String stringMD5 (String inStr ){
Return string2MD5 (string2MD5 (inStr ));

}

/**
* Encryption and decryption algorithms
*/
Public static String convertMD5 (String inStr ){

Char [] a = inStr. toCharArray ();
For (int I = 0; I <a. length; I ++ ){
A [I] = (char) (a [I] ^ 'T ');
}
String s = new String ();
Return s;

}

/**
* HMAC-SHA1
* @ Param encryptText
* @ Param encryptKey
* @ Return
* @ Throws Exception
*/
Public static String HmacSHA1Encrypt (String encryptText, String encryptKey) throws Exception
{
Byte [] data = encryptKey. getBytes (ENCODING );
SecretKey secretKey = new SecretKeySpec (data, MAC_NAME );
Mac mac = Mac. getInstance (MAC_NAME );
Mac. init (secretKey );

Byte [] text = encryptText. getBytes (ENCODING );
Byte [] str = mac. doFinal (text );
// Create Hex String
StringBuffer hexString = new StringBuffer ();
// Convert byte array to hexadecimal number
For (int I = 0; I <str. length; I ++ ){
String shaHex = Integer. toHexString (str [I] & 0xFF );
If (shaHex. length () <2 ){
HexString. append (0 );
}
HexString. append (shaHex );
}
Return hexString. toString ();
}

Public static String convertSHA1 (String instr ){
Try {
Return HmacSHA1Encrypt (instr, key );
} Catch (Exception e ){
// TODO Auto-generated catch block
E. printStackTrace ();
Return "";
}
}

// Test the main function
Public static void main (String args []) throws Exception {
// Hash hash encryption with a key
String tt = convertSHA1 ("123456 ");
System. out. println (tt );

// MD5 Encryption
String s = new String ("123456 ");
System. out. println ("Original:" + s );
System. out. println ("after MD5:" + string2MD5 (s ));
System. out. println ("after MD5 encryption:" + stringMD5 (s ));
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.