SHA256
------------------java自带实现方式---------------
package com.xiayu.demo;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class SHA256 {
public static String getSHA256StrJava(String data){
MessageDigest messageDigest;
String encodeStr = "";
try {
messageDigest = MessageDigest.getInstance("SHA-256");
messageDigest.update(data.getBytes("UTF-8"));
encodeStr = byte2Hex(messageDigest.digest());
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return encodeStr;
}
private static String byte2Hex(byte[] bytes){
StringBuffer stringBuffer = new StringBuffer();
String temp = null;
for (int i=0;i
temp = Integer.toHexString(bytes[i] & 0xFF);
if (temp.length()==1){
stringBuffer.append("0");
}
stringBuffer.append(temp);
}
return stringBuffer.toString();
}
}
------------------java自带实现方式---------------
apache工具类
commons-codec
commons-codec
public static String getSHA256Data(String data){
if (data == null || data.length() == 0){
return null;
}
MessageDigest messageDigest;
String encdeStr;
try {
messageDigest = MessageDigest.getInstance("SHA-256");
byte[] hash = messageDigest.digest(data.getBytes("UTF-8"));
encdeStr = Hex.encodeHexString(hash);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return encdeStr;
}
4.对称加密
AES加解密工具类
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
public class AESUtil {
public static String encrypt(String key,String content){
try {
KeyGenerator keygen=KeyGenerator.getInstance("AES");
keygen.init(128, new SecureRandom(key.getBytes()));
SecretKey original_key=k