简体   繁体   中英

javax.crypto.BadPaddingException

This is how I have created my AES cipher , but I still keep getting this error of BadPaddingException in doFinal() block of decryption

// 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);

Decrypt method code

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;
        }
    }

Encrypt Method

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;
        }
    }

Your code works quite well for me:

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();
        }
    }
}

This prints out Hello World

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM