网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

浅析Java常用的4种加密方式(MD5+Base64+SHA+BCrypt)

一、工具类

为清苑等地区用户提供了全套网页设计制作服务,及清苑网站建设行业解决方案。主营业务为网站建设、做网站、清苑网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!

 md5加密工具类 

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加密工具类

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工具类

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 Bcrypt(final String encryptSource) {
 String salt = BCrypt.gensalt(SALT_SEED);
 Map bcryptResult = Bcrypt(salt, encryptSource);
 return bcryptResult;
 }
 /**
 *
 * @param salt encrypt salt, Must conform to the rules
 * @param encryptSource
 * @return
 */
 public static Map 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 bcryptResult = new HashMap();
 bcryptResult.put(SALT_KEY, salt);
 bcryptResult.put(CIPHER_KEY, cipher);
 return bcryptResult;
 }
 
}

二、加密测试

MD5加密测试 

/**
 * MD5加密
 */
public class MD5Test {
 public static void main(String[] args) {
 String string = "你好 世界";
 String byteArrayToHexString = MD5Utils.byteArrayToHexString(string.getBytes());
 System.out.println(byteArrayToHexString);//e68891e698afe4b880e58fa5e8af9d
 
 }
}

base64加密测试 

/**
 * 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加密测试 

/**
 * 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加密测试

/**
 * BCrypt加密
 */
public class BCryptTest {
 
 public static void main(String[] args) {
 
 String string = "你好世界";
 Map 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 bcrypt2 = BcryptCipher.Bcrypt(bcrypt.get("salt"),string);
 System.out.println(bcrypt2.get("SALT_KEY")); //null
 System.out.println(bcrypt2.get("CIPHER_KEY")); //null
 }
}

总结

以上所述是小编给大家介绍的浅析Java 常用的 4 种加密方式(MD5+Base64+SHA+BCrypt),希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对创新互联网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!


网站标题:浅析Java常用的4种加密方式(MD5+Base64+SHA+BCrypt)
URL网址:http://bjjierui.cn/article/jidhss.html

其他资讯