AES對稱加密,以及javax.crypto.BadPaddingException: Given final block not properly padded 解決


下面的AES加密算法,加密出來的字符串存在“\n”的情況,這影響http中數據傳遞的特殊字符,解決辦法

1.使用Base64再次加密

2.使用 jce(Java Cryptography Extension)的強加密算法,如果再安全保險,package com.example.springboot.thread;

import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; import java.security.SecureRandom; public class AESUtil { /** * 加密 * 1.構造密鑰生成器 * 2.根據ecnodeRules規則初始化密鑰生成器 * 3.產生密鑰 * 4.創建和初始化密碼器 * 5.內容加密 * 6.返回字符串 */
    public static String AESEncode(String encodeRules, String content) { String aesEncode = null; try { //1.構造密鑰生成器,指定為AES算法,不區分大小寫
            KeyGenerator keygen = KeyGenerator.getInstance("AES"); //2.根據ecnodeRules規則初始化密鑰生成器 //生成一個128位的隨機源,根據傳入的字節數組 //keygen.init(128, new SecureRandom(encodeRules.getBytes()));
            SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG"); secureRandom.setSeed(encodeRules.getBytes()); keygen.init(128, secureRandom); //3.產生原始對稱密鑰
            SecretKey original_key = keygen.generateKey(); //4.獲得原始對稱密鑰的字節數組
            byte[] raw = original_key.getEncoded(); //5.根據字節數組生成AES密鑰
            SecretKey key = new SecretKeySpec(raw, "AES"); //6.根據指定算法AES自成密碼器
            Cipher cipher = Cipher.getInstance("AES"); //7.初始化密碼器,第一個參數為加密(Encrypt_mode)或者解密解密(Decrypt_mode)操作,第二個參數為使用的KEY
 cipher.init(Cipher.ENCRYPT_MODE, key); //8.獲取加密內容的字節數組(這里要設置為utf-8)不然內容中如果有中文和英文混合中文就會解密為亂碼
            byte[] byte_encode = content.getBytes("utf-8"); //9.根據密碼器的初始化方式--加密:將數據加密
            byte[] byte_AES = cipher.doFinal(byte_encode); //10.將加密后的數據轉換為字符串 //這里用Base64Encoder中會找不到包 //解決辦法: //在項目的Build path中先移除JRE System Library,再添加庫JRE System Library,重新編譯后就一切正常了。
            aesEncode = new BASE64Encoder().encode(byte_AES); //11.將字符串返回
        } catch (Exception e) { e.printStackTrace(); } // log.error("AESEncode error,content:"+content); //如果有錯就返加null
        return aesEncode; } /** * 解密 * 解密過程: * 1.同加密1-4步 * 2.將加密后的字符串反紡成byte[]數組 * 3.將加密內容解密 */
    public static String AESDecode(String encodeRules, String content) { String aesDecode = null; try { //1.構造密鑰生成器,指定為AES算法,不區分大小寫
            KeyGenerator keygen = KeyGenerator.getInstance("AES"); //2.根據ecnodeRules規則初始化密鑰生成器 //生成一個128位的隨機源,根據傳入的字節數組
            //keygen.init(128, new SecureRandom(encodeRules.getBytes())); SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG"); secureRandom.setSeed(encodeRules.getBytes()); keygen.init(128, secureRandom); //3.產生原始對稱密鑰
            SecretKey original_key = keygen.generateKey(); //4.獲得原始對稱密鑰的字節數組
            byte[] raw = original_key.getEncoded(); //5.根據字節數組生成AES密鑰
            SecretKey key = new SecretKeySpec(raw, "AES"); //6.根據指定算法AES自成密碼器
            Cipher cipher = Cipher.getInstance("AES"); //7.初始化密碼器,第一個參數為加密(Encrypt_mode)或者解密(Decrypt_mode)操作,第二個參數為使用的KEY
 cipher.init(Cipher.DECRYPT_MODE, key); //8.將加密並編碼后的內容解碼成字節數組
            byte[] byte_content = new BASE64Decoder().decodeBuffer(content); /* * 解密 */
            byte[] byte_decode = cipher.doFinal(byte_content); aesDecode = new String(byte_decode, "utf-8"); } catch (Exception e) { e.printStackTrace(); } //如果有錯就返加null // log.error("AESDncode error,content:"+content);
        return aesDecode; } }

 

javax.crypto.BadPaddingException: Given final block not properly padded錯誤是因為上述紅色部分之前代碼是

keygen.init(128, new SecureRandom(encodeRules.getBytes()));
原因:SecureRandom 實現完全隨操作系統本身的內部狀態,除非調用方在調用 getInstance 方法,然后調用 setSeed 方法;該實現在 windows 上每次生成的 key 都相同,但是在 solaris 或部分 linux 系統上則不同。關於SecureRandom類的詳細介紹,見 http://yangzb.iteye.com/blog/325264

解決辦法:調用getInstance方法

SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG") ;
secureRandom.setSeed(encodeRules.getBytes());
 參考地址:https://blog.csdn.net/seapeak007/article/details/79747309


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM