JAVA密碼編譯演算法系列-AesCBC

來源:互聯網
上載者:User

標籤:private   解密   class   pre   static   cep   equals   crypto   ascii   

package ***;import javax.crypto.Cipher;import javax.crypto.spec.IvParameterSpec;import javax.crypto.spec.SecretKeySpec;import sun.misc.BASE64Decoder;import sun.misc.BASE64Encoder;/*** AES 是一種可逆密碼編譯演算法,對使用者的敏感資訊加密處理* 對未經處理資料進行AES加密後,在進行Base64編碼轉化;* 正確*/public class AesCBC {/*已確認* 加密用的Key 可以用26個字母和數字組成* 此處使用AES-128-CBC加密模式,key需要為16位。*/    private static String sKey="1234567890123456";    private static String ivParameter="1234567890123456";    private static AesCBC instance=null;    //private static     private AesCBC(){    }    public static AesCBC getInstance(){        if (instance==null)            instance= new AesCBC();        return instance;    }    // 加密    public String encrypt(String sSrc, String encodingFormat, String sKey, String ivParameter) throws Exception {        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");        byte[] raw = sKey.getBytes();        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");        IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());//使用CBC模式,需要一個向量iv,可增加密碼編譯演算法的強度        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);        byte[] encrypted = cipher.doFinal(sSrc.getBytes(encodingFormat));        return new BASE64Encoder().encode(encrypted);//此處使用BASE64做轉碼。}    // 解密    public String decrypt(String sSrc, String encodingFormat, String sKey, String ivParameter) throws Exception {        try {            byte[] raw = sKey.getBytes("ASCII");            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");            IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());            cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);            byte[] encrypted1 = new BASE64Decoder().decodeBuffer(sSrc);//先用base64解密            byte[] original = cipher.doFinal(encrypted1);            String originalString = new String(original,encodingFormat);            return originalString;        } catch (Exception ex) {            return null;        }}    public static void main(String[] args) throws Exception {        // 需要加密的字串        String cSrc = "123456";        System.out.println("加密前的字串是:"+cSrc);        // 加密        String enString = AesCBC.getInstance().encrypt(cSrc,"utf-8",sKey,ivParameter);        System.out.println("加密後的字串是:"+ enString);                System.out.println("1jdzWuniG6UMtoa3T6uNLA==".equals(enString));                // 解密        String DeString = AesCBC.getInstance().decrypt(enString,"utf-8",sKey,ivParameter);        System.out.println("解密後的字串是:" + DeString);    }}

 

JAVA密碼編譯演算法系列-AesCBC

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.