Java 加密与解密
package com.xu.encrypt.md5;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import javax.crypto.KeyGenerator;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import com.sun.org.apache.xml.internal.security.exceptions.Base64DecodingException;
import com.sun.org.apache.xml.internal.security.utils.Base64;
@SuppressWarnings(value="all")
public class Encrypt {
private static final String KEY_MAC = "HmacMD5";
public static void main(String[] args) {
System.out.println("encodeBase64:"+encodeBase64("a"));
System.out.println("decodeBase64:"+decodeBase64("YQ=="));
System.out.println("encodeMD5:"+encodeMD5("a"));
System.out.println("encodeHMAC:"+encodeHMAC("a"));
System.out.println("encodeSha:"+encodeSha("a"));
System.out.println("md5AndSha:"+md5AndSha("a"));
}
public static String md5AndSha(String inputText) {
return encodeSha(encodeMD5(inputText));
}
public static String encodeSha(String inputText) {
return encode(inputText,"sha-1");
}
public static String encodeHMAC(String inputText){
String result=null;
try {
String key =initMacKey();
result=encryptHMAC(inputText.getBytes(), key);
} catch (Exception e) {e.printStackTrace();}
return result.toString();
}
private static String initMacKey() throws Exception {
KeyGenerator keyGenerator = KeyGenerator.getInstance(KEY_MAC);
SecretKey secretKey = keyGenerator.generateKey();
return new String(secretKey.getEncoded());
}
private static String encryptHMAC(byte[] data, String key) throws Exception {
SecretKey secretKey = new SecretKeySpec(key.getBytes(),KEY_MAC);
Mac mac = Mac.getInstance(secretKey.getAlgorithm());
mac.init(secretKey);
return new String(mac.doFinal(data));
}
private static String encode(String inputText, String algorithmName) {
if (inputText == null || "".equals(inputText.trim())) {
throw new RuntimeException("请输入需要加密的信息!!!");
}
if (algorithmName == null || "".equals(algorithmName.trim())) {
algorithmName = "md5";
}
try {
MessageDigest m = MessageDigest.getInstance(algorithmName);
m.update(inputText.getBytes("UTF8"));
byte s[] = m.digest();
return hex(s);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}
public static String encodeMD5(String inputText){
byte secret[] = null;
try {
MessageDigest digest = MessageDigest.getInstance("md5");
digest.update(inputText.getBytes("UTF8"));
secret= digest.digest();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return hex(secret);
}
private static String hex(byte[] arr) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < arr.length; ++i) {
sb.append(Integer.toHexString((arr[i] & 0xFF) | 0x100).substring(1, 3));
}
return sb.toString();
}
public static String encodeBase64(String inputText){
return Base64.encode(inputText.getBytes());
}
public static String encodeBase64(byte[] inputText){
return Base64.encode(inputText);
}
public static String decodeBase64(String inputText){
String t = null;
try {
t= new String(Base64.decode(inputText));
} catch (Base64DecodingException e) {
e.printStackTrace();
}
return t;
}
}