The article directories

1. Tool classes

  1. Md5 encryption tool class
  2. Base64 encryption tool class
  3. Bcrypt tools

Second, encryption test

  1. MD5 Encryption Test
  2. Base64 encryption test
  3. SHA Encryption Test
  4. BCrypt encryption test

1. Tool classes

1. Md5 encryption tool class
Encryption package Tester. Util; import java.security.MessageDigest; public class MD5Utils { private static final String hexDigIts[] = {"0"."1"."2"."3"."4"."5"."6"."Seven"."8"."9"."a"."b"."c"."d"."e"."f"}; /** * MD5 encryption * @param Origin character * @param charsetName encoding * @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;
        returnhexDigIts[d1] + hexDigIts[d2]; }}Copy the code
2. Base64 encryption tool class
Encryption package Tester. Util; import org.apache.commons.codec.binary.Base64; import java.io.UnsupportedEncodingException; Public class Base64Util {// String encoding private static Final String UTF_8 ="UTF-8"; /** * Encrypted string * @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) {
        }
        returnnull; } /** * Decrypt the encrypted string * @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("I am Chinese"));
        String enStr = Base64Util.encodeData("I am Chinese"); System.out.println(Base64Util.decodeData(enStr)); }}Copy the code
3. Bcrypt utility class
Encryption package Tester. Util; import java.util.HashMap; import java.util.Map; import org.apache.commons.lang3.StringUtils; import org.mindrot.bcrypt.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);
		returnbcryptResult; }}Copy the code

Second, encryption test

1. Perform the MD5 encryption test
Encryption package Tester; The import encryption Tester. Util. MD5Utils; Public class MD5Test {public static void main(String[] args) {String String ="I am a word."; String byteArrayToHexString = MD5Utils.byteArrayToHexString(string.getBytes()); System.out.println(byteArrayToHexString); //e68891e698afe4b880e58fa5e8af9d } }Copy the code
2. Base64 encryption test
Encryption package Tester; import java.util.Base64; The import encryption Tester. Util. Base64Util; Public class Bast64Tester {public static void main(String[] args) {String String ="I am a string"; String encodeData = Base64Util.encodeData(string); DecodeData = Base64Util. DecodeData (encodeData); / / decryption System. Out. Println (encodeData); //5oiR5piv5LiA5Liq5a2X56ym5Liy System.out.println(decodeData); I am a string}}Copy the code
3. SHA encryption test
Encryption package Tester; import org.apache.commons.codec.digest.Sha2Crypt; /** * SHA encryption */ public class ShaTest {public static void main(String[] args) {String String ="I am a word."; String sha256Crypt = Sha2Crypt.sha256Crypt(string.getBytes()); System.out.println(sha256Crypt); //A $5$AFoQTeyt$TiqmobvcQXjXaAQMYosAAO4KI8LfigZMGHzq.Dlp4NC

	}
}
Copy the code
4. BCrypt encryption test
Encryption package Tester; import java.security.SecureRandom; import java.util.Map; import java.util.UUID; The import encryption Tester. Util. BcryptCipher; The import encryption Tester. Util. MD5Utils; Public class BCrypt ttest {public static void main(String[] args) {String String ="I am a word.";
		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	
	}
}
Copy the code