Android에서 텍스트 파일을 저장 /로드하는 중 놀고 있습니다. 정상적으로 작동합니다. 다음 단계는 AES로 암호화하고 해독하는 것입니다.자바에서 암호 해독 문제
writetofile 메서드 내에서 encrypt() 메서드를 호출 할 수 있습니다.이 메서드는 제대로 작동합니다. readfromfile 메서드를 호출하면 암호 텍스트가 검색되는 것을 볼 수 있습니다.
하지만 암호 해독은 작동하지 않습니다. 몇 군데에서 stringBuffer.toString() 및 StringBuffer의 append() 메서드 내에서 simplecrypo를 호출했지만 응용 프로그램이 충돌합니다.
그래서 누구도 파일 내의 문자열을 해독해야하는 위치를 알고 있습니까? decrypt class,하지만 난 문제 먹으 렴 것 같아요 -
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();
}
}
}
는 그리고 암호화 클래스는 여기에 공개적으로 사용할 수 있습니다.
감사합니다.
첫 번째 사항 : 읽기/쓰기 용으로 파일을 열고 _text_를 쓸 때 인코딩을 지정하십시오. 비슷하게,'.getBytes()'에 인코딩을 지정하십시오. – fge
가능한 [ColdFusion 9 및 Java (Android)에서 다른 결과를 생성하는 암호화/암호 해독을위한 Java SimpleCrypto 클래스] (http://stackoverflow.com/questions/11418336/java-simplecrypto-class-for-encryption-decryption-producing) - 다른 결과) –