Java encryption algorithm--MD5 encryption and hash hashing with secret key encryption algorithm source code _java

Source: Internet
Author: User
Tags md5 md5 encryption stringbuffer

Java encryption algorithm--MD5 encryption and hash hashing with secret key encryption algorithm source code

Recently learned encryption algorithm knowledge, using MD5 encryption, Baidu a lot of online data, not very detailed, here on how to implement the MD5 encryption and hashing hash with secret key encryption algorithm, we can see.

Implementation code:

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;  /** * Using 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"; 
/*** * MD5 generate 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 <) hexvalue.append ("0"); 
Hexvalue.append (Integer.tohexstring (Val)); 

return hexvalue.tostring (); 

/*** * MD5 encryption generates a 32-bit MD5 code */public static String stringMD5 (String inStr) {return string2md5 (STRING2MD5 (INSTR)); 
/** * Encryption and decryption algorithm */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 (a); 

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 (); byte array to hexadecimal number for (int i = 0; i < str.length i++) {String Shahex = integer.tohexstring (Str[i] & 0xFF); if (sh
Ahex.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) {//To
Do auto-generated catch block E.printstacktrace ();

Return "";}}
Test main function public static void main (String args[]) throws Exception {//hash hash with secret key encrypt String TT = convertSHA1 ("123456");

SYSTEM.OUT.PRINTLN (TT); 
MD5 Encrypt string s = new String ("123456"); 
System.out.println ("Original:" + s); 
System.out.println ("MD5 after:" + STRING2MD5 (s));
System.out.println ("MD5 after encryption:" + stringMD5 (s)); } 
}

Thank you for reading, I hope to help you, thank you for your support for this site!

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.