javax.crypto.BadPaddingException

时间:2011-04-06 17:55:20

标签: java aes

这就是我创建AES密码的方法,但我仍然在doFinal()解密块中得到BadPaddingException错误

// Get the key generator            
   KeyGenerator kg = KeyGenerator.getInstance("AES");
   kg.init(128);
   SecretKey sk = kg.generateKey();
   byte[] iv = new byte[]{0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f};
   AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv);

   ecipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
   dcipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

   ecipher.init(Cipher.ENCRYPT_MODE, sk,paramSpec);
   dcipher.init(Cipher.DECRYPT_MODE, sk,paramSpec);

解密方法代码

public String decr(String str) {
        try {
            byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(str);
            byte[] utf8 = dcipher.doFinal(dec);
            return new String(utf8, "UTF8");
        } catch (Exception ex) {
            return null;
        }
    }

加密方法

public String encr(String str) {
        try {
            byte[] utf8 = str.getBytes("UTF8");
            byte[] enc = ecipher.doFinal(utf8);
            return new sun.misc.BASE64Encoder().encode(enc);
        } catch (Exception ex) {
            return null;
        }
    }

1 个答案:

答案 0 :(得分:4)

您的代码对我来说效果很好:

package com.sandbox;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.spec.AlgorithmParameterSpec;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;

public class EncryptionTest
{
    private final Cipher ecipher;
    private final Cipher dcipher;

    public EncryptionTest() throws Exception
    {
        KeyGenerator kg = KeyGenerator.getInstance("AES");
        kg.init(128);
        SecretKey sk = kg.generateKey();
        byte[] iv = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f };
        AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv);

        ecipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        dcipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

        ecipher.init(Cipher.ENCRYPT_MODE, sk, paramSpec);
        dcipher.init(Cipher.DECRYPT_MODE, sk, paramSpec);
    }

    public String encrypt(String str) throws IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException
    {
        byte[] utf8 = str.getBytes("UTF8");
        byte[] enc = ecipher.doFinal(utf8);
        return new sun.misc.BASE64Encoder().encode(enc);
    }

    public String decrypt(String str) throws IOException, IllegalBlockSizeException, BadPaddingException
    {
        byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(str);
        byte[] utf8 = dcipher.doFinal(dec);
        return new String(utf8, "UTF8");
    }

    public static void main(String[] args)
    {
        try
        {
            EncryptionTest sandbox = new EncryptionTest();
            System.out.println(sandbox.decrypt(sandbox.encrypt("Hello world")));
        }
        catch (Exception e)
        {
          e.printStackTrace();
        }
    }
}

打印出Hello World