2013-08-15 9 views
1

다음 방법을 사용하여 암호화 된 문자열의 암호를 해독하려하지만 예외로 인해 고정되어 있습니다.숫자 형식 문자열을 해독하는 중 예외가 발생했습니다.

암호화 된 문자열을 아래 메서드로 보내려고하지만 String을 byte []로 변환하는 동안 Number Format Exception을 가져올 수 없습니다.

내 암호 해독 방법은 다음과 같습니다

public static String decrypt(String seed, String encrypted) throws Exception { 

    byte[] seedByte = seed.getBytes(); 

    System.arraycopy(seedByte, 0, key, 0, ((seedByte.length < 16) ? seedByte.length : 16)); 

    String base64 = new String(Base64.decode(encrypted, 0)); 

    byte[] rawKey = getRawKey(seedByte); 

    byte[] enc = toByte(base64); 

    byte[] result = decrypt(rawKey, enc); 

    return new String(result); 

    } 

내 toByte입니다 (String) 메소드 :

public static byte[] toByte(String hexString) { 

    int len = hexString.length()/2; 

    byte[] result = new byte[len]; 

    for (int i = 0; i < len; i++) 

    result[i] = Integer.valueOf(hexString.substring(2 * i, 2 * i + 2), 16).byteValue(); 

    return result; 

    } 

예외 내가 점점 오전 : 내가 왜

08-15 13:03:04.748: W/System.err(10013): java.lang.NumberFormatException: Invalid int: "@��" 
08-15 13:03:04.748: W/System.err(10013): at java.lang.Integer.invalidInt(Integer.java:138) 
08-15 13:03:04.748: W/System.err(10013): at java.lang.Integer.parse(Integer.java:375) 
08-15 13:03:04.748: W/System.err(10013): at java.lang.Integer.parseInt(Integer.java:366) 
08-15 13:03:04.748: W/System.err(10013): at java.lang.Integer.valueOf(Integer.java:510) 
08-15 13:03:04.748: W/System.err(10013): at com.example.aes.EncodeDecodeAES.toByte(EncodeDecodeAES.java:226) 
08-15 13:03:04.748: W/System.err(10013): at com.example.aes.EncodeDecodeAES.decrypt(EncodeDecodeAES.java:69) 
08-15 13:03:04.748: W/System.err(10013): at com.example.aes.MainActivity$1.run(MainActivity.java:94) 

난 정말 이해 didnt는 이 오류가 발생했습니다.

좋습니다.

result[i] = Integer.valueOf(hexString.substring(2 * i, 2 * i + 2), 16).byteValue(); 

당신은 4 바이트 정수를 고려 (이 방법을 시도 할 수 있습니다 당신이이 개 자리에 hexString을 분할 할 것을 :

+1

여기에 IDE가 없지만 charset 문제 일 수 있습니까? 내가 여기 내 hexString를 사용할 필요가 sun.misc.BASE64Decoder sun.misc.BASE64Encoder – DennisH

+0

나는이 패키지를 디코딩/인코딩하려고 – koti

+0

당신이 나에게 어떤 제안을 do.can 무엇을 몰랐어요 나는 내 hexString을 사용한다 – DennisH

답변

0

나는 여기에 문제가 바이트 배열에 대한 분석은 당신이 여기에서 생각 정수) :

byte[][] result = new byte[len][4]; 
int[] numbers = new int[len]; 
for(int j = 0; j < len ; j++){ 
    numbers[j] = Integer.parseInt(hexString.substring(2 * j, 2 * j + 2); 
} 

for (i = 0; i < len; i++){ 
    result[i][3] = (byte) numbers[i] & 0xFF);//No need for shifting here 
    for(j = 0; j < len-1; j++){ 
    result[i][j] = (byte) ((numbers[i] >> ((-1)*j*8+24)) & 0xFF); 
    } 
} 

구현의 문제는 여러 정수로 문자열을 분할한다는 것입니다 그리고 당신은 다음 byte_array하는, 정수의 [0] 단지 첫 번째 바이트 byte_array에 할당하고 [1] 두 번째 정수의 첫 번째 바이트 길이 4의 전체 바이트 배열이 단일 Integer를 저장하는 데 사용된다는 것을 알고 있어야합니다.

+0

이 – koti

+0

곳해야한다, 제안하십시오 – koti

+0

미안, 나는 대답을 편집했다 - 그것을 조사해라. – g00dy

0

16 진수 문자열에 잘못된 바이트가있는 것 같습니다. 바이트 배열에 넣기 전에 유효성을 검사하십시오. 예 :

if(new Scanner(hexString.substring(2 * i, 2 * i + 2), 16).hasNextInt()) 
     result[i] = Integer.valueOf(hexString.substring(2 * i, 2 * i + 2), 16).byteValue()  
+0

이 if 조건으로 들어가기. 다른 해결책을 제안하십시오. – koti

+0

if() 조건 자체에서 오류가 발생합니다 – koti

+0

정확히 EncodeDecodeAES.java:226 행이 무엇에 해당합니까? –

관련 문제