javax.crypto.BadPaddingException: Given final block not properly padded

场景:AES解密时 抱javax.crypto.BadPaddingException: Given final block not properly padded

学习地址:http://www.iteye.com/problems/35327

原因:SecureRandom 实现完全隨操作系统本身的內部狀態,除非調用方在調用 getInstance 方法之後又調用了 setSeed 方法;该实现在 windows 上每次生成的 key 都相同,但是在 solaris 或部分 linux 系统上则不同。

解决方法:

package com.common.util.rsa;

import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;

/**
 * 对称加密--AES
 * @author liuguangping
 *
 */
public class AESUtil {
    
    //密码:长度为8的整数倍
    private static String KEY = "LGP01234"; 
    
    /**
     * 加密方法
     * @param str
     * @return
     */
    public static String encrypt(String str){
        byte[] encode = null;
        EncryParam param = init();
        Cipher cipher = param.getCipher();
        //初始化Cipher格式
        try {
            cipher.init(Cipher.ENCRYPT_MODE, param.getKey());
            encode = cipher.doFinal(str.getBytes("UTF-8"));
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } 
        System.out.println(encode.length);
        return Base64Util.encrypt(encode);
    }
    
    /**
     * 解密
     * @param str
     * @return
     */
    public static String decrypt(String str){
        String decode = "";
        EncryParam param = init();
        try {
            Cipher cipher = param.getCipher();
            //初始化Cipher格式
            cipher.init(Cipher.DECRYPT_MODE, param.getKey());
            byte[] message = Base64Util.decryptByte(str);
            System.out.println(message.length);
            byte[] bytes = cipher.doFinal(message);
//            byte[] bytes =cipher.doFinal(message);
            decode = new String(bytes);
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        }
        return decode;
    }
    
    /**
     * 公共方法,用于初始化Cipher加密、解密器
     * SecureRandom:Windows中每次生成的一样,Linux上面每次不一样,需要setSeed
     * @return
     */
    private static EncryParam init(){
        EncryParam param = new EncryParam();
        try {
            //初始化密码生成工具
            KeyGenerator generator = KeyGenerator.getInstance("AES");
            //初始化工具格式--linux中的实现,Windows通用
            SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
            random.setSeed(KEY.getBytes());
            //windows中生成
//            SecureRandom randomWindows = SecureRandom.getInstance(KEY);
            generator.init(128, random);
            //生成密钥
            SecretKey key = generator.generateKey();
            //创建密码器
            Cipher cipher = Cipher.getInstance("AES");
//            cipher.init(Cipher.ENCRYPT_MODE, key);
            //添加参数
            param.setCipher(cipher);
            param.setKey(key);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } 
        return param;
    }
    
    public static void main(String[] args) {
        String me = encrypt("刘广平");
        System.out.println(me);
        System.out.println(decrypt(me));
    }
}

 

posted @ 2017-02-08 19:33  刘广平  阅读(1001)  评论(0)    收藏  举报