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!