Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Can anyone help me with decrypting an XML file?

Options
  • 24-09-2010 12:02pm
    #1
    Registered Users Posts: 18,272 ✭✭✭✭


    I am trying to to download and decrypt an encrypted XML file.

    I have implemented the download part and have tested with an unencrypted file and it works fine.

    However I now need to be able to download an XML file that has been encrypted using AES and the key "XXXX"

    So I am only concerned with decryption as the encryption on the XML file is already done.

    Here is my code so far:
    public NodeList getXMLDoc(){
            URL url;
            NodeList nl = null;
    
            try{
                String xmlFeed = context.getString(R.string.xml_feed);
                try {
                    url = new URL(xmlFeed);
                    URLConnection urlConnection;
                    urlConnection = url.openConnection();   
                    HttpURLConnection httpConnection = (HttpURLConnection) urlConnection;   
                    int responseCode = httpConnection.getResponseCode();
    
                    if(responseCode == HttpURLConnection.HTTP_OK){
    
                        String bytes = toHex("XXXX");
                        SecretKeySpec skeySpec = new SecretKeySpec(toByte(bytes), "AES");
                        try {
                            c.init(Cipher.DECRYPT_MODE, skeySpec);
                            //c.doFinal();
                        } catch (InvalidKeyException e) {
                            e.printStackTrace();
                        }
                        InputStream in = httpConnection.getInputStream();
                        CipherInputStream cis = new CipherInputStream(in, c);
                        DocumentBuilderFactory dbf;
                        dbf = DocumentBuilderFactory.newInstance();
                        DocumentBuilder db = dbf.newDocumentBuilder();
    
                        Document dom = db.parse(cis);
    
                        Element docEle = dom.getDocumentElement();
    
                        nl = docEle.getElementsByTagName(TAG_CHAR);
    
                        }       
                }
                catch (MalformedURLException e) {
    
                    e.printStackTrace();
                }
                catch (IOException e) {
    
                    e.printStackTrace();
                } catch (ParserConfigurationException e) {
    
                    e.printStackTrace();
                } catch (SAXException e) {
    
                    e.printStackTrace();
                }
                }
                finally{
    
                }
            return nl;
        }
    
    At the minute I am trying to decrypt the whole file using CipherInputStream is this the correct approach?

    My code above gives me the following exception:
    WARN/System.err(5274): java.io.IOException: last block incomplete in decryption
    

    Is this a setup error or what might be causing this error?

    Are there any tutorials on how to decrypt an XML file in Android/Java?

    Am I going in the right direction as to how to decrypt the file or is my code completely wrong?


Comments

  • Registered Users Posts: 18,272 ✭✭✭✭Atomic Pineapple


    Updated Code:
    public NodeList getRoutingDoc(){
            URL url;
            NodeList nl = null;
    
            try{
                String xmlFeed = context.getString(R.string.xml_feed) + IMEI + ".xml";
                try {
                    url = new URL(xmlFeed);
                    URLConnection urlConnection;
                    urlConnection = url.openConnection();    
                    HttpURLConnection httpConnection = (HttpURLConnection) urlConnection;    
                    int responseCode = httpConnection.getResponseCode();
    
                    if(responseCode == HttpURLConnection.HTTP_OK){
                        String bytes = toHex("XXXX");
                        Key skeySpec = new SecretKeySpec(toByte(bytes), "AES");
    
                        InputStream in = httpConnection.getInputStream();
                        System.out.println(toByte(bytes));
                        Cipher c = Cipher.getInstance("AES/CFB8/NoPadding");
                        c.init(Cipher.DECRYPT_MODE, skeySpec, new IvParameterSpec(toByte(bytes)));
                        CipherInputStream cis = new CipherInputStream(in, c);
                        cis.read(new byte[16]);
                        BufferedReader br = new BufferedReader(new InputStreamReader(cis));
                        System.out.println("Got message");
                        System.out.println(br.readLine());
    
                        DocumentBuilderFactory dbf;
                        dbf = DocumentBuilderFactory.newInstance();
                        DocumentBuilder db = dbf.newDocumentBuilder();
    
                        Document dom = db.parse(in);
                        //cis.close();
                        Element docEle = dom.getDocumentElement();
    
                        nl = docEle.getElementsByTagName(TAG_CHAR);
    
                        }        
                }
                catch (MalformedURLException e) {
    
                    e.printStackTrace();
                }
                catch (IOException e) {
    
                    e.printStackTrace();
                } catch (ParserConfigurationException e) {
    
                    e.printStackTrace();
                } catch (SAXException e) {
    
                    e.printStackTrace();
                } catch (InvalidKeyException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (InvalidAlgorithmParameterException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (NoSuchAlgorithmException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (NoSuchPaddingException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                }
                finally{
    
                }
            return nl;
        }
    



    I have updated my code to the latest version I have.

    I am now using the CipherInputStream properly I think and getting the file in but the System.out.println gives me the following output:
    INFO/System.out(7880): &#239;&#191;&#189;=k&#239;&#191;&#189;KV&#239;&#191;&#189;a&#239;&#191;&#189;&#239;&#191;&#189;_|F&#239;&#191;&#189;&#239;&#191;&#189;(# -&#239;&#191;&#189;&#209;.&#239;&#191;&#189;&#239;&#191;&#189;u&#239;&#191;&#189;&#239;&#191;&#189;&#239;&#191;&#189;n&#239;&#191;&#189;|&#239;&#191;&#189;&#239;&#191;&#189;~&#239;&#191;&#189;&#239;&#191;&#189;&#239;&#191;&#189;&#239;&#191;&#189;9&#239;&#191;&#189;<&#195;*&#239;&#191;&#189;|T&#239;&#191;&#189;&#239;&#191;&#189;sUW&#199;&#8240;&#239;&#191;&#189;9&#239;&#191;&#189;qeo&#239;&#191;&#189;M%&#239;&#191;&#189;t&#239;&#191;&#189;V&#239;&#191;&#189;V&#239;&#191;&#189;3Q"&#239;&#191;&#189;    T&#239;&#191;&#189;Yq?&#239;&#191;&#189;&#239;&#191;&#189;E&#239;&#191;&#189;&#239;&#191;&#189;&#239;&#191;&#189;&#239;&#191;&#189;H%fo&#239;&#191;&#189;&#239;&#191;&#189;&#239;&#191;&#189;M&#239;&#191;&#189;un&#239;&#191;&#189;&#239;&#191;&#189;-&#239;&#191;&#189;&#239;&#191;&#189;&#239;&#191;&#189;&#200;&#8220;&#239;&#191;&#189;&#239;&#191;&#189;&#239;&#191;&#189;&#239;&#191;&#189;&#239;&#191;&#189;&#239;&#191;&#189;<d&#239;&#191;&#189;&#239;&#191;&#189;{&#239;&#191;&#189;&#239;&#191;&#189;&#239;&#191;&#189;{&#239;&#191;&#189;!&#239;&#191;&#189;[&&#239;&#191;&#189;&#239;&#191;&#189;%&#239;&#191;&#189;&#239;&#191;&#189;M&#239;&#191;&#189;.&#239;&#191;&#189;jq+&#239;&#191;&#189;&#239;&#191;&#189;,&#239;&#191;&#189;b&#239;&#191;&#189;&#239;&#191;&#189;&#239;&#191;&#189;&#239;&#191;&#189;&#239;&#191;&#189; &#239;&#191;&#189;&#239;&#191;&#189;~&#239;&#191;&#189;&#239;&#191;&#189;&#239;&#191;&#189;&#239;&#191;&#189;)&#239;&#191;&#189;&#239;&#191;&#189;*&#220;&#168;)&#239;&#191;&#189;&#239;&#191;&#189;>&#239;&#191;&#189;&#239;&#191;&#189;i&#239;&#191;&#189;&#239;&#191;&#189;&#239;&#191;&#189;b&#239;&#191;&#189;_"&#239;&#191;&#189;&#239;&#191;&#189;F)&#239;&#191;&#189;`"&#239;&#191;&#189;
    

    So it looks as if its no decrypting it at all or not decrypting it properly??

    Can anyone see what I am doing wrong?


  • Registered Users Posts: 2,781 ✭✭✭amen


    not a java head but are you trying to decrypt the file as you get stream/download it ?

    if so download and save first and the decrypt it.


  • Registered Users Posts: 18,272 ✭✭✭✭Atomic Pineapple


    amen wrote: »
    not a java head but are you trying to decrypt the file as you get stream/download it ?

    if so download and save first and the decrypt it.

    Thanks, I changed it so now I download the file first.

    Then run the following:
    Cipher c = Cipher.getInstance("AES/CFB8/NoPadding");
     c.init(Cipher.DECRYPT_MODE, skeySpec, new IvParameterSpec(toByte(bytes)));
    CipherInputStream cis = new CipherInputStream(new FileInputStream(enc_File), c);
     cis.read(new byte[16]);
    BufferedReader br = new BufferedReader(new InputStreamReader(cis));
     Log.d("XXXX", br.readLine());
    

    But it still doesn't seem to be decoding the file, my log prints out the same sort of junk as before:
    &#239;&#191;&#189;=k&#239;&#191;&#189;KV&#239;&#191;&#189;a&#239;&#191;&#189;&#239;&#191;&#189;_|F&#239;&#191;&#189;&#239;&#191;&#189;(# -&#239;&#191;&#189;&#209;.&#239;&#191;&#189;&#239;&#191;&#189;u&#239;&#191;&#189;&#239;&#191;&#189;&#239;&#191;&#189;n&#239;&#191;&#189;|&#239;&#191;&#189;&#239;&#191;&#189;~&#239;&#191;&#189;&#239;&#191;&#189;&#239;&#191;&#189;&#239;&#191;&#189;9&#239;&#191;&#189;<&#195;*&#239;&#191;&#189;|T&#239;&#191;&#189;&#239;&#191;&#189;sUW&#199;&#8240;&#239;&#191;&#189;9&#239;&#191;&#189;qeo&#239;&#191;&#189;M%&#239;&#191;&#189;t&#239;&#191;&#189;V&#239;&#191;&#189;V&#239;&#191;&#189;3Q"&#239;&#191;&#189;    T&#239;&#191;&#189;Yq?&#239;&#191;&#189;&#239;&#191;&#189;E&#239;&#191;&#189;&#239;&#191;&#189;&#239;&#191;&#189;&#239;&#191;&#189;H%fo&#239;&#191;&#189;&#239;&#191;&#189;&#239;&#191;&#189;M&#239;&#191;&#189;un&#239;&#191;&#189;&#239;&#191;&#189;-&#239;&#191;&#189;&#239;&#191;&#189;&#239;&#191;&#189;&#200;&#8220;&#239;&#191;&#189;&#239;&#191;&#189;&#239;&#191;&#189;&#239;&#191;&#189;&#239;&#191;&#189;&#239;&#191;&#189;<d&#239;&#191;&#189;&#239;&#191;&#189;{&#239;&#191;&#189;&#239;&#191;&#189;&#239;&#191;&#189;{&#239;&#191;&#189;!&#239;&#191;&#189;[&&#239;&#191;&#189;&#239;&#191;&#189;%&#239;&#191;&#189;&#239;&#191;&#189;M&#239;&#191;&#189;.&#239;&#191;&#189;jq+&#239;&#191;&#189;&#239;&#191;&#189;,&#239;&#191;&#189;b&#239;&#191;&#189;&#239;&#191;&#189;&#239;&#191;&#189;&#239;&#191;&#189;&#239;&#191;&#189; &#239;&#191;&#189;&#239;&#191;&#189;~&#239;&#191;&#189;&#239;&#191;&#189;&#239;&#191;&#189;&#239;&#191;&#189;)&#239;&#191;&#189;&#239;&#191;&#189;*&#220;&#168;)&#239;&#191;&#189;&#239;&#191;&#189;>&#239;&#191;&#189;&#239;&#191;&#189;i&#239;&#191;&#189;&#239;&#191;&#189;&#239;&#191;&#189;b&#239;&#191;&#189;_"&#239;&#191;&#189;&#239;&#191;&#189;F)&#239;&#191;&#189;`"&#239;&#191;&#189;
    


  • Registered Users Posts: 18,272 ✭✭✭✭Atomic Pineapple


    Another update:

    I have changed the code to test encryption first and then decryption so the code is now as follows:
    Cipher c = Cipher.getInstance("AES/CFB8/NoPadding");
    	    				
    ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(keyFile));
    oos.writeObject(skeySpec);
    	    				
    c.init(Cipher.ENCRYPT_MODE, skeySpec);
    CipherOutputStream cos = new CipherOutputStream(new FileOutputStream(testFile), c);
    PrintWriter pw = new PrintWriter(new OutputStreamWriter(cos));
    pw.println("Stand and unfold yourself");
    pw.close();
    oos.writeObject(c.getIV());
    oos.close(); 
    
    c.init(Cipher.DECRYPT_MODE, skeySpec, new IvParameterSpec(toByte(bytes)));
    CipherInputStream cis = new CipherInputStream(new FileInputStream(testFile), c);
    Log.d("XXXX", br.readLine());
    

    This log now prints out the following:
    DEBUG/(13642): &#239;&#191;&#189;l&#239;&#191;&#189;&#197;&#8364;&#239;&#191;&#189;&#239;&#191;&#189;&#239;&#191;&#189;&#213;*(&#239;&#191;&#189;&#239;&#191;&#189;&#239;&#191;&#189; yourself
    

    So it looks like its decoding the last part of it but not the rest?

    Any ideas?


  • Registered Users Posts: 7,410 ✭✭✭jmcc


    How is the file being saved? Could there be an issue with how the webserver serves the file (binary rather than text)? What is apparently decrypted is sufficiently non-random enough to look like an xml file that's been improperly encoded initially. The word 'yourself' is also 8 characters. Is there a chance that you have a short read situation where only some of the input data is being read and processed? I don't know Java well enough to debug the code but look at how the initial data is encrypted (padding etc).

    Regards...jmcc


  • Advertisement
Advertisement