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

 更新时间:2019年10月22日 15:22:05   作者:失控的的狗蛋~  
这篇文章主要介绍了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<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;
 }
 
}

二、加密测试

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<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
 }
}

总结

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

相关文章

  • 浅谈fastjson的常用使用方法

    浅谈fastjson的常用使用方法

    下面小编就为大家带来一篇浅谈fastjson的常用使用方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-08-08
  • Java开发编程到底是用idea好还是eclipse好

    Java开发编程到底是用idea好还是eclipse好

    这篇文章主要介绍了Java开发编程到底是用idea好还是eclipse好,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-08-08
  • java并发之AtomicInteger源码分析

    java并发之AtomicInteger源码分析

    AtomicInteger是java并发包下面提供的原子类,主要操作的是int类型的整型,通过调用底层Unsafe的CAS等方法实现原子操作。下面小编和大家一起学习一下
    2019-05-05
  • SpringBoot之整合MyBatis实现CRUD方式

    SpringBoot之整合MyBatis实现CRUD方式

    这篇文章主要介绍了SpringBoot之整合MyBatis实现CRUD方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-08-08
  • JAVA为什么要使用封装及如何封装经典实例

    JAVA为什么要使用封装及如何封装经典实例

    这篇文章主要给大家介绍了关于JAVA为什么要使用封装及如何封装的相关资料,封装就是将属性私有化,提供公有的方法访问私有属性,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2023-10-10
  • IDEA中JetBrains Mono字体的正确安装姿势

    IDEA中JetBrains Mono字体的正确安装姿势

    在 JetBrains Mono 的设计阶段,它就充分考虑到了长时间工作可能导致的眼睛疲劳问题,比如字母的大小和形状、空间量、自然等宽平衡、不必要的细节、连字、以及难以区分的符号等,从而最终设计出了这么一款字体
    2021-06-06
  • Java实现浪漫流星表白的示例代码

    Java实现浪漫流星表白的示例代码

    本文将利用Java语言实现浪漫流星表白,可以实现这些功能:播放音乐、自定义流星数量、飞行速度、光晕大小、流星大小,自定义表白话语,感兴趣的可以学习一下
    2022-05-05
  • SpringBoot基于Redis实现短信登录的操作

    SpringBoot基于Redis实现短信登录的操作

    验证码登录是非常常见的一种登录方式,能够简化用户登录的过程,本文主要介绍了SpringBoot基于Redis实现短信登录的操作,具有一定的参考价值,感兴趣的可以了解一下
    2023-12-12
  • Eclipse将Maven项目打成jar包的方法

    Eclipse将Maven项目打成jar包的方法

    这篇文章主要介绍了Eclipse将Maven项目打成jar包的方法,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2007-09-09
  • SpringBoot 实现动态添加定时任务功能

    SpringBoot 实现动态添加定时任务功能

    这篇文章主要介绍了SpringBoot 动态添加定时任务,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-02-02

最新评论