javax.crypto.BadPaddingException:错误 [英] javax.crypto.BadPaddingException: error

查看:169
本文介绍了javax.crypto.BadPaddingException:错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试运行一个简单的加密/解密程序。我得到一个填充异常。必须有隐藏的东西,我不知道。我基本上加密了将一个字符串写入一个文件,读回来并对其进行解密。原始的加密数组解密没有问题。我将原始的加密数组与从文件读回的数组进行了比较,与我可以看到的相同。文件的缓冲区不起作用,所以必须有区别。我不知道该怎么做。

  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异常
{
Key key = new SecretKeySpec(_key,_algo);
密码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异常
{
Key key = new SecretKeySpec(_key,_algo);
密码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异常
{
String str =再见残酷的世界;

//
//从命令行获取密码
//
_key = args [0] .getBytes();

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

//
//将加密数组写入文件
//
FileOutputStream os = new FileOutputStream(data);
os.write(encodeArray);
os.close();

//
//解码并打印出字符串
//
String decodeStr = sample.decrypt(encodeArray);
System.out.println(decodeStr =+ decodeStr);

//
//读回加密字符串
byte [] buffer = new byte [64];
FileInputStream is = new FileInputStream(data);
is.read(buffer);
is.close();

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

输出: p>

  java示例1234567890123456 
decodeStr =再见残酷世界
线程main中的异常javax.crypto.BadPaddingException :给最终块没有正确填充
在com.sun.crypto.provider.SunJCE_f.b(DashoA13 * ..)
在com.sun.crypto.provider.SunJCE_f.b(DashoA13 * .. )
在com.sun.crypto.provider.AESCipher.engineDoFinal(DashoA13 * ..)
在javax.crypto.Cipher.doFinal(DashoA13 * ..)
在sample.decrypt( sample.java:32)
在sample.main(sample.java:70)


解决方案

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



或使用文件的长度如下:

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


I am trying to run a simple encryption/decryption program. I am getting a padding exception. There must be something hidden that I am not aware. I basically encrypted a string write it to a file, read it back, and decrypted it. The original encrypted array was decrypted without a problem. I compared the original encrypted array with the array read back from the file, they were identical from what I can see. The buffer from the file does not work, so there must be something difference. I don't know what to do.

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

Output:

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)  

解决方案

The problem is that the byte buffer with a size of 64, which you are reading the file into, is too big. Change it to 32.

Or use the length of the file like this:

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

这篇关于javax.crypto.BadPaddingException:错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆