javax.crypto.BadPaddingException : error

标签 java encryption

我正在尝试运行一个简单的加密/解密程序。我收到填充异常。一定有什么隐情是我不知道的。我基本上加密了一个字符串,将其写入文件,读回并解密。原始加密数组被毫无问题地解密。我将原始加密数组与从文件中读回的数组进行了比较,从我所看到的来看它们是相同的。文件中的缓冲区不起作用,因此肯定存在一些差异。我不知道该怎么办。

import java.security.*;  
import java.security.spec.InvalidKeySpecException;  
import javax.crypto.Cipher;  
import javax.crypto.spec.SecretKeySpec;  

import java.io.*;  

public class sample  
{  
   private static String _algo = "AES";  
   private static byte[] _key = new byte[16];  

   public static byte[] encrypt (String val) throws Exception  
   {  
      Key key = new SecretKeySpec (_key, _algo);  
      Cipher c = Cipher.getInstance (_algo);  

      c.init (Cipher.ENCRYPT_MODE, key);  

      byte[] encode = c.doFinal (val.getBytes());  

      return encode;  
   }  

   public static String decrypt (byte[] val) throws Exception    
   {  
      Key key = new SecretKeySpec (_key, _algo);  
      Cipher c = Cipher.getInstance (_algo);  

      c.init (Cipher.DECRYPT_MODE, key);  

      byte[] decode = c.doFinal (val);  

      String decodeStr = new String (decode);  

      return decodeStr;  
   }  

   public static void main (String[] args) throws Exception  
   {  
      String str = "Good bye cruel world";  

      //  
      // get password from command line  
      //  
      _key = args[0].getBytes();  

      byte[] encodeArray = sample.encrypt (str);  

      //  
      // write encrypted array to file  
      //  
      FileOutputStream os = new FileOutputStream ("data");  
      os.write (encodeArray);  
      os.close();  

      //  
      // decode and print out string  
      //  
      String decodeStr = sample.decrypt (encodeArray);  
      System.out.println ("decodeStr = " + decodeStr);  

      //  
      // read back encrypted string  
      byte[] buffer = new byte[64];  
      FileInputStream is = new FileInputStream ("data");  
      is.read (buffer);  
      is.close();  

      decodeStr = sample.decrypt (buffer);  
      System.out.println ("decodeStr = " + decodeStr);  
   }  
}  

输出:

java sample 1234567890123456  
decodeStr = Good bye cruel world  
Exception in thread "main" javax.crypto.BadPaddingException: Given final block not   properly padded  
        at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..)  
        at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..)  
        at com.sun.crypto.provider.AESCipher.engineDoFinal(DashoA13*..)  
        at javax.crypto.Cipher.doFinal(DashoA13*..)  
        at sample.decrypt(sample.java:32)  
        at sample.main(sample.java:70)  

最佳答案

问题是您正在将文件读入的大小为 64 的字节缓冲区太大。将其更改为 32。

或者使用文件的长度,如下所示:

byte[] buffer = new byte[(int)new File("data").length()];

关于javax.crypto.BadPaddingException : error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4146324/

相关文章:

javascript - AES CBC : JavaScript/CryptoJS Encrypt -> Golang Decrypt

encryption - 多重 DES_CBC_NOPAD 加密的 doFinal 错误

python - 我如何(成功地)从命令行 openSSL 解码编码密码?

c# - 从密码构建私有(private) CngKey(ECDH_P384)

java - ListFragment 适配器的通用 View 持有者类型

java - 继承: How child class interact with its parent?

java - 通过互联网与 Android 手机和 PC 进行 2 种方式通信

java - 使用 gson/jackson java API 解析 json 文件

c++ - 序列化公钥以通过网络发送

Java内存模型-有人能解释一下吗?