简体   繁体   中英

decryption issue in java

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM