Reputation: 784
following image is what written in file,
this is the code to write on a file. SecurityUtils class used here is to encode string to AES/CBC.
public static boolean reWriteToFile(String str1,
String str2, String str3, String str4) {
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
SecurityUtils securityUtils = new SecurityUtils();
String st = securityUtils.getEncryptedToken(str1) + "\n" + securityUtils.getEncryptedToken(str2) + "\n" + securityUtils.getEncryptedToken(str3) + "\n" + securityUtils.getEncryptedToken(str4);
Logger.i("STRING_TO_WRITE", st);
File externalStorageDir = Environment.getExternalStorageDirectory();
File myFile = new File(externalStorageDir, APP_CONFIG_FILE_NAME);
try {
FileOutputStream fOut1 = new FileOutputStream(myFile);
OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut1);
myOutWriter.append(st);
myOutWriter.close();
fOut1.close();
} catch (Exception e) {
e.printStackTrace();
}
return true;
} else {
return false;
}
}
this is the code to read from file:
public static boolean readFromFile() {
SecurityUtils securityUtils = new SecurityUtils();
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
File externalStorageDir = Environment.getExternalStorageDirectory();
File myFile = new File(externalStorageDir, APP_CONFIG_FILE_NAME);
if (myFile.exists()) {
try {
BufferedReader br = new BufferedReader(
new InputStreamReader(new FileInputStream(myFile)));
String data;
StringBuffer sb = new StringBuffer();
while ((data = br.readLine()) != null) {
sb.append(data + ",");
}
String str = sb.toString().replace(",,", ",");
String[] both = str.split(",");
Log.d("3rd string", both[3]);
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
return false;
} else
return false;
}
But in the log of both[3] I get only 5th line as shown in image where I required 5th to 9th lines while 1st line is a string with no newline character 3rd also a string with no newline character 5th-9th also a string with no newline character 11th also a string with no newline character. But between each different strings there is a new line character
these all are AES/CBC encoded strings not the whole part.
Upvotes: 2
Views: 482
Reputation: 666
Solution of your problem is pretty easy - you shouldn't write to file any strings, who are only results of encryption. Every time, when you want to encrypt some string, result of encryption should be encoded by Base64. Use below code to encode your encrytion result.
public String base64Encoding(String input) throws UnsupportedEncodingException {
String base64 = "";
byte[] hex = input.getBytes("utf8");
base64 = Base64.encodeToString(hex, Base64.NO_WRAP);
return base64;
}
Your code should look like this
String st = base64Encoding(securityUtils.getEncryptedToken(str1)) + "\n" + base64Encoding(securityUtils.getEncryptedToken(str2)) + "\n" + base64Encoding(securityUtils.getEncryptedToken(str3)) + "\n" + base64Encoding(securityUtils.getEncryptedToken(str4));
Paste it in your try catch block, and all should be fine.
And when you want to read your file, and decrypt your string, you should first decode base64, then decrypt aes. Heres example code
public String base64ToText(String input) throws UnsupportedEncodingException {
String text = "";
byte[] data = Base64.decode(input, Base64.NO_WRAP);
text = new String(data, "utf8");
return text;
}
Upvotes: 1