首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >javax.crypto.BadPaddingException:错误

javax.crypto.BadPaddingException:错误
EN

Stack Overflow用户
提问于 2010-11-10 15:53:10
回答 1查看 17K关注 0票数 2

我正在尝试运行一个简单的加密/解密程序。我得到了一个填充异常。一定有我不知道的隐藏的东西。我基本上加密了一个字符串,把它写到一个文件中,读回来,然后解密它。原始加密数组已被解密,没有任何问题。我将原始加密数组与从文件中读取的数组进行了比较,它们与我所看到的完全相同。文件中的缓冲区不能工作,所以一定有一些不同之处。该怎么办呢。

代码语言:javascript
复制
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);  
   }  
}  

输出:

代码语言:javascript
复制
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)  
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-11-10 16:30:02

问题是,您要将文件读入的大小为64的字节缓冲区太大了。改到32。

或者像这样使用文件的长度:

代码语言:javascript
复制
byte[] buffer = new byte[(int)new File("data").length()];
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4146324

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档