java實現AES方式加密

來源:互聯網
上載者:User

標籤:AES加密

import javax.crypto.BadPaddingException;import javax.crypto.Cipher;import javax.crypto.IllegalBlockSizeException;import javax.crypto.NoSuchPaddingException;import javax.crypto.spec.IvParameterSpec;import javax.crypto.spec.SecretKeySpec;import org.apache.commons.codec.binary.Base64;import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.JSONObject;import java.io.IOException;import java.io.UnsupportedEncodingException;import java.net.URLDecoder;import java.net.URLEncoder;import java.security.InvalidAlgorithmParameterException;import java.security.InvalidKeyException;import java.security.NoSuchAlgorithmException;public class AESUtil {    private static final String IV_STRING = "sdf4ddfsFD86Vdf2";    private static final String encoding = "UTF-8";    public static String encryptAES(String content, String key)            throws InvalidKeyException, NoSuchAlgorithmException,            NoSuchPaddingException, UnsupportedEncodingException,            InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {        byte[] byteContent = content.getBytes(encoding);        // 注意,為了能與 iOS 統一        // 這裡的 key 不可以使用 KeyGenerator、SecureRandom、SecretKey 產生        byte[] enCodeFormat = key.getBytes(encoding);        SecretKeySpec secretKeySpec = new SecretKeySpec(enCodeFormat, "AES");        byte[] initParam = IV_STRING.getBytes(encoding);        IvParameterSpec ivParameterSpec = new IvParameterSpec(initParam);        // 指定加密的演算法、工作模式和填充方式        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);        byte[] encryptedBytes = cipher.doFinal(byteContent);        // 同樣對加密後資料進行 base64 編碼        String base64 = new Base64().encodeToString(encryptedBytes);        //進行url編碼 去掉= ? &        return URLEncoder.encode(base64,encoding);    }    public static String decryptAES(String content, String key)            throws InvalidKeyException, NoSuchAlgorithmException,            NoSuchPaddingException, InvalidAlgorithmParameterException,            IllegalBlockSizeException, BadPaddingException, IOException {        //URL解碼        content = URLDecoder.decode(content,encoding);        // base64 解碼        byte[] encryptedBytes = Base64.decodeBase64(content);        byte[] enCodeFormat = key.getBytes(encoding);        SecretKeySpec secretKey = new SecretKeySpec(enCodeFormat, "AES");        byte[] initParam = IV_STRING.getBytes(encoding);        IvParameterSpec ivParameterSpec = new IvParameterSpec(initParam);        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");        cipher.init(Cipher.DECRYPT_MODE, secretKey, ivParameterSpec);        byte[] result = cipher.doFinal(encryptedBytes);        return new String(result, encoding);    }        public static void main(String[] args) throws Exception {    JSONObject json = new JSONObject();    json.put("custNum", "111111");    /*json.put("name", "張三");    json.put("cityCode", "100001");    json.put("cityName", "北京市");    json.put("mobileNo", "15651876590");*/        String content = json.toJSONString();        System.out.println("加密前:" + content);        String key = "djadiKJdj49dFJLd";        System.out.println("加密金鑰和解密密鑰:" + key);        String encrypt = encryptAES(content, key);        System.out.println("加密後:" + encrypt);        String decrypt = decryptAES(encrypt , key);        System.out.println("解密後:" + decrypt);    }}


java實現AES方式加密

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.