이 코드는 ArrayIndexOutOfBoundsException : 행에 있습니다. [] password = args [0] .toCharArray(); . 어떻게해야할까요?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"));
}
}
스택 추적을 게시하고 발생한 위치를 찾아야합니다. 그런 다음 그것이 잘못 된 곳을 즉시 알 수 있습니다. 그렇지 않은 경우 디버그에서 코드를 단계별로 실행하십시오. – Chii
-1 약간의 노력을 보여주고 try catch에 랩핑하고 스택 추적을 살펴보고 printlns를 사방에 깔아서 어떤 commandexactly가 문제를 일으키는 지 알아 내려고 디버그 할 수 있습니다. – user44242
그리고 신을 위해서, 전문가가 아니라면 자신의 암호 코드를 작성하지 마십시오. –