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 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;
}
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?
EDIT:
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): �=k�KV�a��_|F��(# -�Ñ��u���n�|��~����9�<Ã�|T��sUWÇ�9�qeo�M%�t�V�V�3Q"� T�Yq?��E����H%fo���M�un��-���È������<d��{���{�!�[&��%��M�.�jq+��,�b����� ��~����)��*ܨ)��>��i���b�_"��F)�`"�
So it looks as if its no decrypting it at all or not decrypting it properly??
Can anyone see what I am doing wrong?
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): �l�Ť���Õ*(��� yourself
So it looks like its decoding the last part of it but not the rest?
Any ideas?