2009-11-19 8 views
-4

Ce code montre ArrayIndexOutOfBoundsException: à la ligne char [] password = args [0] .toCharArray(); . Que faire ?ArrayIndexOutOfBoundsException

package org.temp2.cod1; 
import java.security.*; 
import java.security.spec.InvalidKeySpecException; 
import java.security.spec.InvalidParameterSpecException; 
import java.security.spec.KeySpec; 

import javax.crypto.*; 
import javax.crypto.spec.*; 
import java.io.*; 

public class Code2 { 

public static void main(String[] args) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException, InvalidKeySpecException, InvalidParameterSpecException { 

    char[] password = args[0].toCharArray(); 
    byte[] salt = new byte[8]; 
    for (int i = 0; i < 8; ++i) { 
     salt[i] = (byte) Integer.parseInt(args[1].substring(i * 2, i * 2 + 2), 16); 
    } 


    SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); 
    KeySpec spec = new PBEKeySpec(password, salt, 1024, 256); 
    SecretKey tmp = factory.generateSecret(spec); 
    SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES"); 

    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); 
    cipher.init(Cipher.ENCRYPT_MODE, secret); 
    AlgorithmParameters params = cipher.getParameters(); 
    byte[] iv = params.getParameterSpec(IvParameterSpec.class).getIV(); 
    byte[] ciphertext = cipher.doFinal("Hello, World!".getBytes("UTF-8")); 


    } 
} 
+0

vous devriez publier votre trace de pile, et déterminer où c'est arrivé. Alors peut être vous pouvez dire immédiatement où il a mal tourné. Sinon, essayez de parcourir le code dans le débogage. – Chii

+3

-1 montrez quelques efforts, enveloppez-le dans une tentative d'attraper et regardez la trace de la pile, mettez printlns partout et essayez de découvrir quelle commandexactly provoque le problème, et vous pouvez ensuite déboguer. – user44242

+1

Et pour l'amour des dieux, n'écrivez pas votre propre code crypto sauf si vous êtes un expert. –

Répondre

4

Vérifiez si vous transmettez réellement 2 arguments à la méthode principale ou utilisez args [0] pour accéder au premier. Et vérifiez, si le second paramètre a une longueur minimale de 16 caractères.

+0

Il utilise 'args [0]'. +1 sur la vérification du nombre d'arguments, mais – ChssPly76

+0

Plus tard, il utilise args [1], aussi ... – tanascius

1

Depuis que vous avez demandé quoi faire; Vérifiez la pile et regardez quelle ligne lève cette exception. Et vérifier si vous fournissez assez d'arguments

0

Ce code lance la même exception aussi longtemps que vous l'appelez sans args ...

public class Code2 { 
    public static void main(String[] args) { 
    string firstArgument = args[0]; 
    } 
} 

args est un tableau de chaînes et contient les arguments passés à votre programme. Avec args [0] vous essayez d'accéder au premier argument mais il n'y a pas un tel premier argument ...

Questions connexes