java encryption algorithm MD5 encryption and hash hash with secret key encryption algorithm source code
java encryption algorithm —MD5 encryption and hash hash with secret key encryption algorithm source code
Recently learned the knowledge of encryption algorithm, the use of MD5 encryption, baidu 1 under the Internet information is a lot of, not very detailed, here sorted out how to use MD5 encryption and hash hash with the secret key encryption algorithm, you can look at the next.
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 overweight generate 32 position 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 generate 32 position 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();
// The byte array is converted to 106 Into the system The 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 with secret key encryption
String tt = convertSHA1("123456");
System.out.println(tt);
//MD5 encryption
String s = new String("123456");
System.out.println(" The original: " + s);
System.out.println("MD5 After: " + string2MD5(s));
System.out.println("MD5 After the encryption: " + stringMD5(s));
}
}
Thank you for reading, I hope to help you, thank you for your support of this site!