淺析Java 常用的 4 種加密方式(MD5+Base64+SHA+BCrypt)
一、工具類(lèi)
md5加密工具類(lèi)
public class MD5Utils {
private static final String hexDigIts[] = {"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"};
/**
* MD5加密
* @param origin 字符
* @param charsetname 編碼
* @return
*/
public static String MD5Encode(String origin, String charsetname){
String resultString = null;
try{
resultString = new String(origin);
MessageDigest md = MessageDigest.getInstance("MD5");
if(null == charsetname || "".equals(charsetname)){
resultString = byteArrayToHexString(md.digest(resultString.getBytes()));
}else{
resultString = byteArrayToHexString(md.digest(resultString.getBytes(charsetname)));
}
}catch (Exception e){
}
return resultString;
}
public static String byteArrayToHexString(byte b[]){
StringBuffer resultSb = new StringBuffer();
for(int i = 0; i < b.length; i++){
resultSb.append(byteToHexString(b[i]));
}
return resultSb.toString();
}
public static String byteToHexString(byte b){
int n = b;
if(n < 0){
n += 256;
}
int d1 = n / 16;
int d2 = n % 16;
return hexDigIts[d1] + hexDigIts[d2];
}
}
base64加密工具類(lèi)
public class Base64Util {
// 字符串編碼
private static final String UTF_8 = "UTF-8";
/**
* 加密字符串
* @param inputData
* @return
*/
public static String decodeData(String inputData) {
try {
if (null == inputData) {
return null;
}
return new String(Base64.decodeBase64(inputData.getBytes(UTF_8)), UTF_8);
} catch (UnsupportedEncodingException e) {
}
return null;
}
/**
* 解密加密后的字符串
* @param inputData
* @return
*/
public static String encodeData(String inputData) {
try {
if (null == inputData) {
return null;
}
return new String(Base64.encodeBase64(inputData.getBytes(UTF_8)), UTF_8);
} catch (UnsupportedEncodingException e) {
}
return null;
}
public static void main(String[] args) {
System.out.println(Base64Util.encodeData("我是中文"));
String enStr = Base64Util.encodeData("我是中文");
System.out.println(Base64Util.decodeData(enStr));
}
}
Bcrypt工具類(lèi)
public class BcryptCipher {
// generate salt seed
private static final int SALT_SEED = 12;
// the head fo salt
private static final String SALT_STARTSWITH = "$2a$12";
public static final String SALT_KEY = "salt";
public static final String CIPHER_KEY = "cipher";
/**
* Bcrypt encryption algorithm method
* @param encryptSource
* need to encrypt the string
* @return Map , two values in Map , salt and cipher
*/
public static Map<String, String> Bcrypt(final String encryptSource) {
String salt = BCrypt.gensalt(SALT_SEED);
Map<String, String> bcryptResult = Bcrypt(salt, encryptSource);
return bcryptResult;
}
/**
*
* @param salt encrypt salt, Must conform to the rules
* @param encryptSource
* @return
*/
public static Map<String, String> Bcrypt(final String salt, final String encryptSource) {
if (StringUtils.isBlank(encryptSource)) {
throw new RuntimeException("Bcrypt encrypt input params can not be empty");
}
if (StringUtils.isBlank(salt) || salt.length() != 29) {
throw new RuntimeException("Salt can't be empty and length must be to 29");
}
if (!salt.startsWith(SALT_STARTSWITH)) {
throw new RuntimeException("Invalid salt version, salt version is $2a$12");
}
String cipher = BCrypt.hashpw(encryptSource, salt);
Map<String, String> bcryptResult = new HashMap<String, String>();
bcryptResult.put(SALT_KEY, salt);
bcryptResult.put(CIPHER_KEY, cipher);
return bcryptResult;
}
}
二、加密測(cè)試
MD5加密測(cè)試
/**
* MD5加密
*/
public class MD5Test {
public static void main(String[] args) {
String string = "你好 世界";
String byteArrayToHexString = MD5Utils.byteArrayToHexString(string.getBytes());
System.out.println(byteArrayToHexString);//e68891e698afe4b880e58fa5e8af9d
}
}
base64加密測(cè)試
/**
* base64加密
*/
public class Bast64Tester {
public static void main(String[] args) {
String string = "你好 世界";
String encodeData = Base64Util.encodeData(string); //加密
String decodeData = Base64Util.decodeData(encodeData); //解密
System.out.println(encodeData);//5oiR5piv5LiA5Liq5a2X56ym5Liy
System.out.println(decodeData);//你好 世界
}
}
SHA加密測(cè)試
/**
* SHA加密
*/
public class ShaTest {
public static void main(String[] args) {
String string = "你好 世界";
String sha256Crypt = Sha2Crypt.sha256Crypt(string.getBytes());
System.out.println(sha256Crypt);//$5$AFoQTeyt$TiqmobvcQXjXaAQMYosAAO4KI8LfigZMGHzq.Dlp4NC
}
}
BCrypt加密測(cè)試
/**
* BCrypt加密
*/
public class BCryptTest {
public static void main(String[] args) {
String string = "你好世界";
Map<String, String> bcrypt = BcryptCipher.Bcrypt(string);
System.out.println(bcrypt.keySet()); //[cipher, salt]
System.out.println(bcrypt.get("cipher")); //$2a$12$ylb92Z84gqlrSfzIztlCV.dK0xNbw.pOv3UwXXA76llOsNRTJsE/.
System.out.println(bcrypt.get("salt")); //$2a$12$ylb92Z84gqlrSfzIztlCV.
Map<String, String> bcrypt2 = BcryptCipher.Bcrypt(bcrypt.get("salt"),string);
System.out.println(bcrypt2.get("SALT_KEY")); //null
System.out.println(bcrypt2.get("CIPHER_KEY")); //null
}
}
總結(jié)
以上所述是小編給大家介紹的淺析Java 常用的 4 種加密方式(MD5+Base64+SHA+BCrypt),希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺(jué)得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
相關(guān)文章
SpringCloud微服務(wù)熔斷器Hystrix使用詳解
這篇文章主要介紹了Spring Cloud Hyxtrix的基本使用,它是Spring Cloud中集成的一個(gè)組件,在整個(gè)生態(tài)中主要為我們提供服務(wù)隔離,服務(wù)熔斷,服務(wù)降級(jí)功能,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07
解讀@RequestBody與post請(qǐng)求的關(guān)系
這篇文章主要介紹了解讀@RequestBody與post請(qǐng)求的關(guān)系,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12
springboot如何使用logback-spring配置日志格式,并分環(huán)境配置
這篇文章主要介紹了springboot如何使用logback-spring配置日志格式,并分環(huán)境配置的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
java數(shù)據(jù)結(jié)構(gòu)與算法之冒泡排序詳解
這篇文章主要介紹了java數(shù)據(jù)結(jié)構(gòu)與算法之冒泡排序,結(jié)合實(shí)例形式詳細(xì)分析了java冒泡排序的原理、實(shí)現(xiàn)技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-05-05

