Java中常见加密算法
3个加密算法
1. MD5Utils
1 package com.jzx.encrypted_utils; 2 3 import java.security.MessageDigest; 4 5 public class MD5Utils { 6 7 private static final String hexDigIts[] = {"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"}; 8 //之所以不是char数组是为了防止拼接的时候进行加法运算 9 /** 10 * MD5加密 11 * @param origin 原始字符串 12 * @param charsetname 编码 13 * @return 14 */ 15 public static String MD5Encode(String origin, String charsetname){ 16 String resultString = null; 17 try{ 18 resultString = new String(origin); 19 MessageDigest md = MessageDigest.getInstance("MD5"); 20 if(null == charsetname || "".equals(charsetname)){ //字符集为空 21 resultString = byteArrayToHexString(md.digest(resultString.getBytes())); 22 }else{ //字符集非空 23 resultString = byteArrayToHexString(md.digest(resultString.getBytes(charsetname))); 24 } 25 }catch (Exception e){ 26 } 27 return resultString; 28 } 29 30 31 public static String byteArrayToHexString(byte b[]){ 32 StringBuffer resultSb = new StringBuffer(); 33 for(int i = 0; i < b.length; i++){ 34 resultSb.append(byteToHexString(b[i])); //将每个字符都转化为十六进制数 35 } 36 return resultSb.toString(); 37 } 38 39 public static String byteToHexString(byte b){ 40 int n = b; //转换成对应的ASCII值 41 if(n < 0){ 42 n += 256; 43 } 44 int d1 = n / 16; //转换成对应十六进制数"d1d2" 45 int d2 = n % 16; 46 return hexDigIts[d1] + hexDigIts[d2]; 47 //索引正好对应相应的单字符串(0对应字符"0"),若是char数组的话会进行加法运算 48 } 49 50 }
2. Base64
1 package com.jzx.encrypted_utils; 2 3 import org.apache.commons.codec.binary.Base64; 4 import java.io.UnsupportedEncodingException; 5 6 public class Base64Util { 7 8 // 字符串编码 9 private static final String UTF_8 = "UTF-8"; 10 11 /** 12 * 加密字符串 13 * @param inputData 14 * @return 15 */ 16 public static String encodeData(String inputData) { 17 try { 18 if (null == inputData) { 19 return null; 20 } 21 return new String(Base64.decodeBase64(inputData.getBytes(UTF_8)), UTF_8); 22 } catch (UnsupportedEncodingException e) { 23 } 24 return null; 25 } 26 27 /** 28 * 解密加密后的字符串 29 * @param inputData 30 * @return 31 */ 32 public static String decodeData(String inputData) { 33 try { 34 if (null == inputData) { 35 return null; 36 } 37 return new String(Base64.encodeBase64(inputData.getBytes(UTF_8)), UTF_8); 38 } catch (UnsupportedEncodingException e) { 39 } 40 return null; 41 } 42 }
3. BcryptCipher
1 package com.jzx.encrypted_utils; 2 3 import java.util.HashMap; 4 import java.util.Map; 5 6 import org.apache.commons.lang3.StringUtils; 7 import org.mindrot.bcrypt.BCrypt; 8 9 public class BcryptCipher { 10 // generate salt seed 11 private static final int SALT_SEED = 12; 12 // the head fo salt 13 private static final String SALT_STARTSWITH = "$2a$12"; 14 15 public static final String SALT_KEY = "salt"; 16 17 public static final String CIPHER_KEY = "cipher"; 18 19 /** 20 * Bcrypt encryption algorithm method 21 * @param encryptSource 22 * need to encrypt the string 23 * @return Map , two values in Map , salt and cipher 24 */ 25 public static Map<String, String> Bcrypt(final String encryptSource) { 26 String salt = BCrypt.gensalt(SALT_SEED); 27 Map<String, String> bcryptResult = Bcrypt(salt, encryptSource); 28 return bcryptResult; 29 } 30 /** 31 * 32 * @param salt encrypt salt, Must conform to the rules 33 * @param encryptSource 34 * @return 35 */ 36 public static Map<String, String> Bcrypt(final String salt, final String encryptSource) { 37 if (StringUtils.isBlank(encryptSource)) { 38 throw new RuntimeException("Bcrypt encrypt input params can not be empty"); 39 } 40 41 if (StringUtils.isBlank(salt) || salt.length() != 29) { 42 throw new RuntimeException("Salt can\'t be empty and length must be to 29"); 43 } 44 if (!salt.startsWith(SALT_STARTSWITH)) { 45 throw new RuntimeException("Invalid salt version, salt version is $2a$12"); 46 } 47 48 String cipher = BCrypt.hashpw(encryptSource, salt); 49 Map<String, String> bcryptResult = new HashMap<String, String>(); 50 bcryptResult.put(SALT_KEY, salt); 51 bcryptResult.put(CIPHER_KEY, cipher); 52 return bcryptResult; 53 } 54 55 }
测试:
1 public static void main(String[] args) { 2 3 String string = "我是一句话"; 4 Map<String, String> bcrypt = BcryptCipher.Bcrypt(string); 5 System.out.println(bcrypt.keySet()); //[cipher, salt] 6 7 System.out.println(bcrypt.get("cipher")); //获取cipher值 8 //$2a$12$ylb92Z84gqlrSfzIztlCV.dK0xNbw.pOv3UwXXA76llOsNRTJsE 9 System.out.println(bcrypt.get("salt")); //获取salt值 10 //$2a$12$ylb92Z84gqlrSfzIztlCV. 11 12 Map<String, String> bcrypt2 = BcryptCipher.Bcrypt(bcrypt.get("salt"),string); 13 System.out.println(bcrypt2.get("SALT_KEY")); //null 14 System.out.println(bcrypt2.get("CIPHER_KEY")); //null 15 }
版权声明:本文为jiazhongxin原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。