亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

一文讓你輕松了解JAVA開發中的四種加密方法

發布時間:2020-08-02 23:50:00 來源:網絡 閱讀:1595 作者:編程Crazy 欄目:編程語言

文章目錄

一、工具類

  1. md5加密工具類
  2. base64加密工具類
  3. Bcrypt工具類

二、加密測試

  1. MD5加密測試
  2. base64加密測試
  3. SHA加密測試
  4. BCrypt加密測試

一、工具類

1. md5加密工具類
package 加密Tester.util;
import java.security.MessageDigest;

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

}
2. base64加密工具類
package 加密Tester.util;
import org.apache.commons.codec.binary.Base64;
import java.io.UnsupportedEncodingException;

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));
    }
}
3. Bcrypt工具類
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);
        return bcryptResult;
    }

}

二、加密測試

1. MD5加密測試
package 加密Tester;

import 加密Tester.util.MD5Utils;

/**
 * MD5加密
 */
public class MD5Test {
    public static void main(String[] args) {
        String string = "我是一句話";
        String byteArrayToHexString = MD5Utils.byteArrayToHexString(string.getBytes());
        System.out.println(byteArrayToHexString);//e68891e698afe4b880e58fa5e8af9d

    }
}
2. base64加密測試
package 加密Tester;

import java.util.Base64;

import 加密Tester.util.Base64Util;

/**
 * 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);//我是一個字符串

    }

}
3. SHA加密測試
package 加密Tester;

import org.apache.commons.codec.digest.Sha2Crypt;

/**
 * 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

    }
}
4. BCrypt加密測試
package 加密Tester;

import java.security.SecureRandom;
import java.util.Map;
import java.util.UUID;

import 加密Tester.util.BcryptCipher;
import 加密Tester.util.MD5Utils;

/**
 * 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   
    }
}
向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

长兴县| 吴桥县| 南京市| 潮安县| 社旗县| 宝山区| 泸西县| 龙海市| 蒙自县| 普兰店市| 柳州市| 桦川县| 桑日县| 门头沟区| 蒙城县| 文登市| 潮州市| 江北区| 长葛市| 沂水县| 仙游县| 原平市| 关岭| 汕尾市| 新乡县| 长兴县| 微博| 鹤庆县| 舞钢市| 东丰县| 旬阳县| 武宁县| 娄烦县| 武宣县| 开原市| 安西县| 淄博市| 习水县| 巴里| 梅州市| 靖州|