2013-07-07 6 views
1

SD 카드에 저장된 내 앱의 xml 파일을 암호화/해독하려고합니다. using this code. 암호화가 잘 작동하는 동안, 내가 붙어있는 암호 해독 부분입니다.SD 카드 안드로이드에서 XML 파일 암호 해독

암호화 코드 :

private void writeToFile(final String xmlString, final String exportFileName) throws IOException { 
    File dir = new File(Environment.getExternalStorageDirectory(), BData.DATASUBDIRECTORY); 
    if (!dir.exists()) { 
    dir.mkdirs(); 
    } 
    File file = new File(dir, exportFileName); 
    file.createNewFile(); 

    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file)); 
    try { 
     String fileData = AdvancedCrypto.encrypt("myPassword", "mySalt"), xmlString.toString()); 
     bos.write(fileData.getBytes()); 
    } catch (Exception e){ 
    } finally { 
     if (bos != null) { 
      bos.flush(); 
      bos.close(); 
     } 
    } 

} 

암호 해독 코드 : 해독 방법이 라인에서

07-07 21:44:27.755: W/System.err(5608): Caused by: java.lang.StringIndexOutOfBoundsException: length=11; regionStart=0; regionLength=32 
07-07 21:44:27.755: W/System.err(5608):  at java.lang.String.startEndAndLength(String.java:593) 
07-07 21:44:27.755: W/System.err(5608):  at java.lang.String.substring(String.java:1474) 

: :이 오류를 던지고

public void getDataFromXML(Context context, String fileName){ 
     try 
     { 
      XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); 
      factory.setNamespaceAware(true); 
      XmlPullParser _xml = factory.newPullParser(); 

      // get a reference to the file. 
      File file = new File(Environment.getExternalStorageDirectory() 
        + File.separator + fileName); 

      // create an input stream to be read by the stream reader. 
      FileInputStream fis = new FileInputStream(file); 

      BufferedInputStream buf = new BufferedInputStream(fis); 

      int size = (int) file.length(); 
      byte[] contents = new byte[size]; 
      //byte[] data = buf.read(contents); 

      String fileData = AdvancedCrypto.decrypt("myPassword", "mySalt"), contents.toString()); 
      buf.read(fileData.getBytes()); 

      // set the input for the parser using an InputStreamReader 
      _xml.setInput(buf, HTTP.UTF_8); 

      buf.close(); 

      int eventType = _xml.getEventType(); 
      boolean done = false;  

      //..rest of the code 
      } 
    } 

String ivHex = encrypted.substring(0, IV_LENGTH * 2); 

파일을 올바르게 읽는 중입니까?

답변

0

배열은 Java의 toString() 메서드를 재정의하지 않습니다. 바이트 배열에서 toString()을 호출하면 아마도 사용자가 작성한 내용이 생성되지 않습니다.

AdvancedCrypto.decrypt() 메서드는 암호문을 문자열로 간주합니다. 해당 encrypt 메서드는 출력 바이트를 16 진수로 인코딩하는 것으로 보입니다. 코드에 대한 가장 간단한 수정은 contents.toString()new String(contents)으로 변경하는 것입니다. (플랫폼 기본 인코딩에 의존하는 것은 나쁜 습관입니다. 문자열과 바이트 사이를 양방향으로 변환 할 때 UTF-8을 명시 적으로 지정해야합니다.) 그러나 가장 간단한 수정이 항상 최상의 것은 아닙니다. 임의의 암호화 코드를 인터넷에서 복사하여 붙여 넣는 것은 특히 의심 스럽습니다. 암호문을 파일에 저장하는 경우 암호문을 16 진수로 인코딩 할 이유가 전혀 없지만이 AdvancedCrypto 클래스는 암호문을 파일에서 저장합니다. 이런 방식으로 클래스를 사용하면 불필요한 바이트 복사가 발생하므로 데이터가 수백 킬로바이트를 초과하면 성능 문제가 발생할 수 있습니다.

또한 data is only as secure as the key임을 기억하십시오. 애플리케이션에서 키를 하드 코딩하면 애플리케이션에 액세스 할 수있는 누구나 데이터를 해독 할 수 있습니다.

+0

나는이 코드를 quesiton에 링크했다 : http://pocket-for-android.1047292.n5.nabble.com/Encryption-method-and-reading-the-Dropbox-backup-td4344194.html#a4454327 – input

+0

정말로, 나쁘다. 내 편집을 참조하십시오. – ntoskrnl