BASE64Decoder替代類Base64

        之前base64加密用的是sun公司的sun.misc.BASE64Encoder/BASE64Decoder,由於後面版本更新,sun公司被oracle公司收購,加密類BASE64Encoder被org.apache.commons.codec.binary.Base64替代了。

      兩者加密數據的結果其實是相同的

import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sun.misc.BASE64Encoder;

import com.alibaba.fastjson.JSONObject;

public class TestEncode {
    private static final Logger logger = LoggerFactory.getLogger(TestEncode.class); 
    public static void main(String[] args) throws Exception {
        JSONObject condition = new JSONObject();
        condition.put("payAmount", "1");
        condition.put("merchantCode", "XM01");
        condition.put("transDesc", "選擇支付渠道");
        condition.put("bizNo", "dadad3333");
        condition.put("bizType", "76");
        condition.put("idType", "5");
        condition.put("accountName", "衛涵菡");
        condition.put("serialNo", "2");
        condition.put("idNo", "131124197907242963");
        String encryptKey = "abc";
        String result1 = aesEncrypt1(condition.toJSONString(),encryptKey);
        String result2 = aesEncrypt2(condition.toJSONString(),encryptKey);
        logger.info(result1);
        logger.info(result2);
    }
    public static String aesEncrypt1(String content, String encryptKey) throws Exception {
        logger.info("加密前的數據content={}",content);
        return base64Encode1(aesEncryptToBytes(content, encryptKey));
    }
    
    public static String aesEncrypt2(String content, String encryptKey) throws Exception {
        logger.info("加密前的數據content={}",content);
        return base64Encode2(aesEncryptToBytes(content, encryptKey));
    }


    public static String base64Encode1(byte[] bytes) {
        return new String(new Base64().encode(bytes));
        
    }
    
    public static String base64Encode2(byte[] bytes) {
        return new BASE64Encoder().encode(bytes);   
    }
    
    
    public static byte[] aesEncryptToBytes(String content, String encryptKey) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
        KeyGenerator kgen = KeyGenerator.getInstance("AES");
        SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
        random.setSeed(encryptKey.getBytes());
        kgen.init(128, random);

        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(1, new SecretKeySpec(kgen.generateKey().getEncoded(), "AES"));

        return cipher.doFinal(content.getBytes("utf-8"));
    }
    

}









發佈了170 篇原創文章 · 獲贊 158 · 訪問量 40萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章