2010-11-10 16 views
2

J'essaye d'exécuter un programme simple de cryptage/décryptage. Je reçois une exception de remplissage. Il doit y avoir quelque chose de caché que je ne suis pas au courant. J'ai fondamentalement crypté une chaîne écris-la dans un fichier, la relis et la décrypte. Le tableau crypté d'origine a été déchiffré sans problème. J'ai comparé le tableau crypté d'origine avec le tableau lu à partir du fichier, ils étaient identiques à ce que je peux voir. Le tampon du fichier ne fonctionne pas, il doit donc y avoir quelque chose de différent. Je ne sais pas quoi faire.javax.crypto.BadPaddingException: erreur

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

Sortie:

java sample 123456789
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) 

Répondre

5

Le problème est que le tampon d'octets avec une taille de 64, que vous lisez le fichier dans, est trop grand. Changer à 32.

Ou utiliser la longueur du fichier comme ceci:

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

Ok, ça marche. Merci. – tadpole