嵌套异常是javax.crypto.BadPaddingException:给定最终块未正确填充

时间:2017-02-10 16:59:27

标签: java encryption jar javax

我正在尝试按如下方式加密和解密数据,但收到错误。加密时会抛出错误,因为嵌套异常是javax.crypto.BadPaddingException:给定最终块未正确填充。我是java Cryptography的新手。请告诉我怎么做?

CMD出错:

每当我尝试打开一个可执行的JAR文件时,它会加载一段时间然后给我一个错误说:

org.springframework.security.util.EncryptionUtils$EncryptionException: Given final block not properly padded; nested exception is javax.crypto.BadPaddingException: Given final block not properly padded
        at org.springframework.security.util.EncryptionUtils.cipher(EncryptionUtils.java:90) ~[script-1.0.jar:na]
        at org.springframework.security.util.EncryptionUtils.decrypt(EncryptionUtils.java:131) ~[script-1.0.jar:na]
        at EncryptDecryptUtil.decrypt(EncryptDecryptUtil.java:10) ~[script-1.0.jar:na]
        at WebDriverBootstrap.fetchAnswer(WebDriverBootstrap.java:56) [script-1.0.jar:na]
        at WebDriverBootstrap.main(WebDriverBootstrap.java:38) [script-1.0.jar:na]

Caused by: javax.crypto.BadPaddingException: Given final block not properly padded
        at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:989) ~[sunjce_provider.jar:1.8.0_112]
        at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:845) ~[sunjce_provider.jar:1.8.0_112]
        at com.sun.crypto.provider.DESedeCipher.engineDoFinal(DESedeCipher.java:294) ~[sunjce_provider.jar:1.8.0_112]
        at javax.crypto.Cipher.doFinal(Cipher.java:2165) ~[na:1.8.0_121]
        at org.springframework.security.util.EncryptionUtils.cipher(EncryptionUtils.java:88) ~[script-1.0.jar:na]
        ... 4 common frames omitted

代码:

package org.springframework.security.util;

import java.io.UnsupportedEncodingException;
import java.security.spec.KeySpec;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import org.apache.commons.codec.binary.Base64;
import org.springframework.security.SpringSecurityException;
import org.springframework.util.Assert;

public final class EncryptionUtils
{
  public static byte[] stringToByteArray(String input)
  {
    Assert.hasLength(input, "Input required");
    try
    {
      return input.getBytes("UTF-8");
    }
    catch (UnsupportedEncodingException fallbackToDefault) {}
    return input.getBytes();
  }

  public static String byteArrayToString(byte[] byteArray)
  {
    Assert.notNull(byteArray, "ByteArray required");
    Assert.isTrue(byteArray.length > 0, "ByteArray cannot be empty");
    try
    {
      return new String(byteArray, "UTF8");
    }
    catch (UnsupportedEncodingException e) {}
    return new String(byteArray);
  }

  private static byte[] cipher(String key, byte[] passedBytes, int cipherMode)
    throws EncryptionUtils.EncryptionException
  {
    try
    {
      KeySpec keySpec = new DESedeKeySpec(stringToByteArray(key));
      SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
      Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
      SecretKey secretKey = keyFactory.generateSecret(keySpec);
      cipher.init(cipherMode, secretKey);
      return cipher.doFinal(passedBytes);
    }
    catch (Exception e)
    {
      throw new EncryptionException(e.getMessage(), e);
    }
  }

  public static String encrypt(String key, String inputString)
    throws EncryptionUtils.EncryptionException
  {
    isValidKey(key);
    byte[] cipherText = cipher(key, stringToByteArray(inputString), 1);
    return byteArrayToString(Base64.encodeBase64(cipherText));
  }

  public static byte[] encrypt(String key, byte[] inputBytes)
    throws EncryptionUtils.EncryptionException
  {
    isValidKey(key);
    return Base64.encodeBase64(cipher(key, inputBytes, 1));
  }

  public static String decrypt(String key, String inputString)
    throws EncryptionUtils.EncryptionException
  {
    Assert.hasText(key, "A key is required to attempt decryption");
    byte[] cipherText = cipher(key, Base64.decodeBase64(stringToByteArray(inputString)), 2);
    return byteArrayToString(cipherText);
  }

  public static byte[] decrypt(String key, byte[] inputBytes)
    throws EncryptionUtils.EncryptionException
  {
    Assert.hasText(key, "A key is required to attempt decryption");
    return cipher(key, Base64.decodeBase64(inputBytes), 2);
  }

  private static void isValidKey(String key)
  {
    Assert.hasText(key, "A key to perform the encryption is required");
    Assert.isTrue(key.length() >= 24, "Key must be at least 24 characters long");
  }

  public static class EncryptionException
    extends SpringSecurityException
  {
    private static final long serialVersionUID = 1L;

    public EncryptionException(String message, Throwable t)
    {
      super(t);
    }

    public EncryptionException(String message)
    {
      super();
    }
  }
}
  

java:90行:抛出新的EncryptionException(e.getMessage(),e);   java:56行:Assert.hasLength(输入,"需要输入");

0 个答案:

没有答案