2016-06-03 33 views
-2

압축 파일의 암호화를 해제하려고합니다. 어떤 직업과 다른 직업은 그렇지 않습니다. ZipInputStream을 닫으려고하면 오류가 발생합니다.암호화되지 않음으로 인한 어려움 : 오류 BadPaddingException : 최종 블록이 올바르게 채워지지 않았다고 가정 할 때

사용자 컴퓨터에 파일을 만들 때 문제가 없었지만이 보안 위험을 피하고자합니다. 마지막으로 파일을 삭제하여 모든 위험 요소를 처리 할 수 ​​있다고 생각했지만 파일이 삭제되지 않은 상황을 목격했습니다. 그러므로 나는이 가능성을 완전히 피하고 싶다. 이 코드를 사용하려면 Using streams to decrypt and unzip to limit memory usage?을 사용하십시오.

나는 패딩에 대해 여러 곳을 읽었지만이 코드를 상속 받았으며 아직 학습 곡선에 있습니다.

final File file = new File(path, fileName); 
Key key = new SecretKeySpec(secret, "AES"); 
final Cipher cipher = Cipher.getInstance("AES"); 
cipher.init(Cipher.DECRYPT_MODE, key); 
try (FileInputStream fis = new FileInputStream(file); CipherInputStream cis = new CipherInputStream(fis, cipher); ZipInputStream zis = new ZipInputStream(new BufferedInputStream(cis))) { 
       ZipEntry entry; 
       while ((entry = zis.getNextEntry()) != null) { 

        List<String> lines; 
        try (ByteArrayOutputStream output = new ByteArrayOutputStream(2048)) { 
         int len; 
         while ((len = zis.read(buffer)) > 0) { 
          output.write(buffer, 0, len); 
         } 
         try (ByteArrayInputStream bais = new ByteArrayInputStream(output.toByteArray()); Reader reader = new InputStreamReader(bais)) { 
          lines = readFile(reader); 
         } 
        } 
        //Do something with lines of the file... 

       } 

      } 

내가 오류가 아래이며,이 마지막 줄 (은 try-와-자원 블록의 끝에서 발생합니다.

java.io.IOException: javax.crypto.BadPaddingException: Given final block not properly padded 
    at javax.crypto.CipherInputStream.close(CipherInputStream.java:321) 
    at java.io.BufferedInputStream.close(BufferedInputStream.java:472) 
    at java.io.PushbackInputStream.close(PushbackInputStream.java:379) 
    at java.util.zip.InflaterInputStream.close(InflaterInputStream.java:227) 
    at java.util.zip.ZipInputStream.close(ZipInputStream.java:265) 

Caused by: javax.crypto.BadPaddingException: Given final block not properly padded 
    at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:811) 
    at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:676) 
    at com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher.java:313) 
    at javax.crypto.Cipher.doFinal(Cipher.java:1970) 
    at javax.crypto.CipherInputStream.close(CipherInputStream.java:314) 

나는 디버깅과 코드를 걸어 봤어요. 이 오류가 발생하는 파일은 암호화되지 않고 압축이 풀려서 제대로 읽혀집니다. 모든 데이터가 읽히고 모든면에서 "행"이 완료되며 ZipInputStream이 닫히지 않습니다. 아이디어가 있습니까?

+1

당신이 이해하지 못하는 보안 코드를 사용하고 있습니다. 인터넷에서 얻었으므로 괜찮을 것입니다. 당신은 하나의 신뢰하는 사람입니다! 코드에 의도적으로 결함이있을 수 있다고 생각한 적이 있습니까? 그것은 실제로 이루어진 것입니다. BTW, 출력 스트림이 제대로 닫히지 않았습니까? – zaph

+0

코드가 불완전합니다 ([mcve]에 대해 읽음). 또한이 문제를 조사하기 위해 파일을 압축하고 암호화하는 코드를 확인해야합니다. – Roman

답변

1

내 문제는 해독 중이지만 tr보다 먼저 ByteArrayOutputStream에 다시 쓰지 않고 지글 지글 소리.

내 코드를 수정하고 작업 :

public void decrypt(final byte[] secret, final File encryptedFile) throws IOException { 
     final ByteArrayOutputStream baos; 
     try { 
      final Cipher cipher = Cipher.getInstance("AES"); 
      cipher.init(Cipher.DECRYPT_MODE, modelEncryptor.getKey()); 
      try (FileInputStream fis = new FileInputStream(encryptedFile); CipherInputStream cis = new CipherInputStream(fis, cipher)) { 
       baos = new ByteArrayOutputStream(); 
       byte[] buff = new byte[8]; 
       int numRead = cis.read(buff); 
       while (numRead != -1) { 
        baos.write(buff, 0, numRead); //I was trying to combine this write with the unzipping write. 
        numRead = cis.read(buff); 
       } 
      } 
     } catch (Exception ex) { 
      throw new IOException("Unable to decrypt " + encryptedFile, ex); 
     } 

     try (ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); ZipInputStream zipIn = new ZipInputStream(new BufferedInputStream(bais))) { 
      ZipEntry entry; 
      while ((entry = zipIn.getNextEntry()) != null) { 
       int BUFFER = 2048; 
       ByteArrayOutputStream fos = new ByteArrayOutputStream(2048); 
       try (BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER)) { 
        int count; 
        byte data[] = new byte[BUFFER]; 
        while ((count = zipIn.read(data, 0, BUFFER)) != -1) { 
         dest.write(data, 0, count); 
        } 
        dest.flush(); 
       } 
       final List<String> lines = Arrays.asList(fos.toString().split("\r\n")); 
       //Do something with lines... 
      } 
     } 
     baos.close(); 
    } 

압축 풀기에서 해독을 분리함으로써이 모든 것이 지금 잘 작동합니다. 언젠가 다른 사람에게 도움이되기를 바랍니다.

P. 나는 암호화면에서 아무런 변화가 없었다.