0

I am playing around with saving/loading a text file in Android, works fine. Next step is to encrypt and decrypt with AES.

I can call the encrypt() method within the writetofile method,a dn that works fine. If i call the readfromfile method, I can see the ciphertext is retrieved, great.

But decryption isnt working for me - I have called simplecrypo in a few places - on stringBuffer.toString(), and within the StringBuffer's append() method - but both crash the application.

So does anybody know where I am supposed to decrypt the string within the file?

package com.example.filesdemo;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

    private EditText etInput;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        etInput = (EditText) findViewById(R.id.etInput);

    }

    public void writeToFile(View v) throws Exception {
        try {
            String inputStr = etInput.getText().toString();
            //encrypt the string - works!
            String encrypted = SimpleCrypto.encrypt("testkey", inputStr);


            FileOutputStream fos = openFileOutput("myfile.txt", MODE_PRIVATE);
            fos.write(encrypted.getBytes());
            fos.flush();
            fos.close();
            Toast.makeText(this, "File saved!", Toast.LENGTH_SHORT).show();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void readFromFile(View v) throws Exception{
        try {
            FileInputStream fis = openFileInput("myfile.txt");
            StringBuffer stringBuffer = new StringBuffer();
            BufferedReader bReader = new BufferedReader(new InputStreamReader(
                    fis));
            String strLine = null;

            while ((strLine = bReader.readLine()) != null) {
                stringBuffer.append(strLine + "\n");

            }
            bReader.close();
            fis.close();
            Toast.makeText(this, "File content: \n" +stringBuffer.toString(),
                    Toast.LENGTH_SHORT).show();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

And the encryption class is publically available here - decrypt class, but i dont think thats the issue.

Thanks!

  • 2
    First things first: specify the encoding when you open a file for read/write and you want to write _text_. Similarly, specify an encoding in `.getBytes()`. – fge Dec 23 '14 at 11:01
  • 2
    possible duplicate of [Java SimpleCrypto Class for encryption / decryption producing different results in Coldfusion 9 and Java (Android)](http://stackoverflow.com/questions/11418336/java-simplecrypto-class-for-encryption-decryption-producing-different-results) – Maarten Bodewes Dec 23 '14 at 12:01

1 Answers1

0

From the look of things, you are writing the encrypted bytes to the file, and then trying to read them back as text into a Scanner. That won't work, as you have found.

If you want your file to be text, then you need to convert the bytes to Base64 and write as text. On reading the Base64 text, convert back to bytes and decypher. Java 8 has the Base64 class, I an not sure about Android.

If you want the file as raw bytes, then you need to read it as bytes, not as text. Once read as bytes it can be decyphered directly.

rossum
  • 15,344
  • 1
  • 24
  • 38