2016-06-23 2 views
0

내 프로그램에 이상한 점이 있습니다. 일부 문자열을 암호화 한 다음 암호를 해독하려고합니다. 나는 암호 해독의 기능을 테스트하기 위해 시험 클래스를 만들었다. 상황이 암호화를 위해 잘 작동하지만, 해독하려고 할 때만, 때로는 문자열 중간에 이상한 문자가 있습니다. 여기 내 코드입니다 :Java Cipher 해독에 이상한 문자가 있습니다.

public class Prova { 

public static void main(String[] args){ 
    String s = "with the lights out is less dangerous here we are now entertain us"; 
    s = cripta(s); 
    System.out.println(s); 
    s = decripta(s); 
    System.out.println(s); 
} 

public static String cripta(String s){ 
    System.out.println("lunghezza stringa:"+s.length()); 
    byte[] input = s.getBytes(); 
    byte[] output; 
    byte[] keyBytes = hexStringToByteArray("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"); 
    byte[] ivBytes = hexStringToByteArray("AAAAAAAAAAAAAAAA"); 
    String out = ""; 


    SecretKeySpec key = new SecretKeySpec(keyBytes, "DESede"); 
    IvParameterSpec ivSpec = new IvParameterSpec(ivBytes); 
    try { 
     Cipher cp = Cipher.getInstance("DESede/ECB/PKCS5Padding"); 
     cp.init(Cipher.ENCRYPT_MODE, key); 
     byte[] criptati = new byte[cp.getOutputSize(input.length)]; 
     int enc_len = cp.update(input, 0, input.length, criptati, 0); 
     enc_len += cp.doFinal(criptati, enc_len); 
     out = new String(criptati); 
    } catch (NoSuchAlgorithmException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (NoSuchPaddingException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (InvalidKeyException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (ShortBufferException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IllegalBlockSizeException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (BadPaddingException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    return out; 
} 

public static String decripta(String s){ 

    byte[] input = s.getBytes(); 
    byte[] output; 
    byte[] keyBytes = hexStringToByteArray("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"); 
    byte[] ivBytes = hexStringToByteArray("AAAAAAAAAAAAAAAA"); 
    String out = ""; 

    try { 
     SecretKeySpec key = new SecretKeySpec(keyBytes, "DESede"); 
     IvParameterSpec ivSpec = new IvParameterSpec(ivBytes); 
     Cipher cp = Cipher.getInstance("DESede/ECB/NoPadding"); 
     cp.init(Cipher.DECRYPT_MODE, key); 
     byte[] decrypt = new byte[cp.getOutputSize(input.length)]; 

     int dec_len = cp.update(input, 0, input.length, decrypt, 0); 
     System.out.println(dec_len); 
     dec_len += cp.doFinal(decrypt, dec_len); 
     System.out.println(dec_len); 
     out = new String(decrypt); 
    } catch (NoSuchAlgorithmException | NoSuchPaddingException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (InvalidKeyException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (ShortBufferException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IllegalBlockSizeException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (BadPaddingException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    return out; 
} 

private static byte[] hexStringToByteArray(String s) { 
    // TODO Auto-generated method stub 
    int len = s.length(); 
    byte[] data = new byte[len/2]; 
    for (int i = 0; i < len; i += 2) { 
     data[i/2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) 
          + Character.digit(s.charAt(i+1), 16)); 
    } 
    return data; 

} 

}

내가받을 출력 "이 소등이 덜 dangÝâ ¥ 8, -xÖÆZŽÏö = AEE 지금 우리를 즐겁게"입니다

+1

는'String' 바이너리 데이터의 컨테이너가 아닙니다. – EJP

답변

0

문제가 있다는 것입니다 암호화 된 바이트는 단순히 문자열로 변환 될 때 이상한 동작을 유발하는 일부 값을 포함 할 수 있습니다. decripta에서

out = new String(Base64.getEncoder().encode(criptati)); 

: 그런데

byte[] input = Base64.getDecoder().decode(s.getBytes()); 

: encripta에서

가 : 당신은 왜 cp.update()을 사용합니까 그래서 그런 base64로 인코딩을 사용해야합니까? 왜 그냥 cp.doFinal() 그런 :

Cipher cp = Cipher.getInstance("DESede/ECB/PKCS5Padding"); 
cp.init(Cipher.ENCRYPT_MODE, key); 
byte[] criptati = cp.doFinal(input); 
out = new String(Base64.getEncoder().encode(criptati)); 

그리고 decripta에 대한 동일하지? 제거 (등 TODO-의견, 사용되지 않는 코드) 모든 혼란과 함께 예를 들어 작업

편집

:

public static void main(String[] args) { 
     String s = "with the lights out is less dangerous here we are now entertain us"; 
     s = cripta(s); 
     System.out.println(s); 
     s = decripta(s); 
     System.out.println(s); 
    } 

    public static String cripta(String s) { 
     System.out.println("lunghezza stringa:" + s.length()); 
     byte[] input = s.getBytes(); 
     byte[] keyBytes = hexStringToByteArray("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"); 
     String out = ""; 

     SecretKeySpec key = new SecretKeySpec(keyBytes, "DESede"); 
     try { 
      Cipher cp = Cipher.getInstance("DESede/ECB/PKCS5Padding"); 
      cp.init(Cipher.ENCRYPT_MODE, key); 
      byte[] criptati = cp.doFinal(input); 
      out = new String(Base64.getEncoder().encode(criptati)); 
     } catch (NoSuchAlgorithmException e) { 
      e.printStackTrace(); 
     } catch (NoSuchPaddingException e) { 
      e.printStackTrace(); 
     } catch (InvalidKeyException e) { 
      e.printStackTrace(); 
     } catch (IllegalBlockSizeException e) { 
      e.printStackTrace(); 
     } catch (BadPaddingException e) { 
      e.printStackTrace(); 
     } 

     return out; 
    } 

    public static String decripta(String s) { 

     byte[] input = Base64.getDecoder().decode(s.getBytes()); 
     byte[] keyBytes = hexStringToByteArray("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"); 
     String out = ""; 

     try { 
      SecretKeySpec key = new SecretKeySpec(keyBytes, "DESede"); 
      Cipher cp = Cipher.getInstance("DESede/ECB/NoPadding"); 
      cp.init(Cipher.DECRYPT_MODE, key); 
      byte[] decrypt = cp.doFinal(input); 
      out = new String(decrypt); 
     } catch (NoSuchAlgorithmException | NoSuchPaddingException e) { 
      e.printStackTrace(); 
     } catch (InvalidKeyException e) { 
      e.printStackTrace(); 
     } catch (IllegalBlockSizeException e) { 
      e.printStackTrace(); 
     } catch (BadPaddingException e) { 
      e.printStackTrace(); 
     } 
     return out; 
    } 
+0

이제이 오류가 발생합니다 : 스레드 "main"예외 java.lang.IllegalArgumentException : 잘못된 base64 문자 20 –

+0

그것은 작동합니다, 나는 당신의 솔루션을 사용하여 실수를했습니다, 지금은 wrok 것, 도와 주셔서 대단히 감사합니다! –

관련 문제