Another update:
I have changed the code to test encryption first and then decryption so the
code is now as follows:
[CODE]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());[/CODE]
This log now prints out the following:
[CODE]DEBUG/(13642): �l�Ť���Õ*(��� yourself[/CODE]
So it looks like its decoding the last part of it but not the rest?
Any ideas?
On Fri, Sep 24, 2010 at 2:23 PM, Donal Rafferty <[email protected]> wrote:
>
> 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): � =k�K V� a ��_|F��(# -ï¿½Ñ ï¿½
> �u ���n�| �� ~��� �9�< �|T��sU
> Wlj�9�qeo� M%�t�V�V�3Q" � T�Yq?��E��� �H%f
> o���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?
>
>
>
> On Fri, Sep 24, 2010 at 10:44 AM, [email protected] <[email protected]>wrote:
>
>> 0 down vote favorite
>>
>>
>> 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?
>>
>
>
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en