Preparations for backup feature

This commit is contained in:
2021-06-13 21:38:39 +02:00
parent 75b67ea2f0
commit c37f51bff8
7 changed files with 331 additions and 5 deletions
@@ -0,0 +1,111 @@
package de.joel.zoomhelper;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import androidx.annotation.RequiresApi;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
public class BackupController {
@RequiresApi(api = Build.VERSION_CODES.O)
public void backup(Activity activity) {
Encrypter encrypter = new Encrypter();
SharedPreferences mPrefs = activity.getPreferences(Context.MODE_PRIVATE);
String value = mPrefs.getString("Meetings", "");
File destination = new File(activity.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath(), "backup.tmp");
try {
writeStringToFile(value, destination.getAbsolutePath());
} catch (IOException e) {
e.printStackTrace();
}
//TODO: Eingabefeld für Passwort
try {
encrypter.encrypt(destination.getAbsolutePath(), "test");
} catch (BadPaddingException | IllegalBlockSizeException | IOException | NoSuchAlgorithmException | InvalidKeySpecException | InvalidAlgorithmParameterException | InvalidKeyException | NoSuchPaddingException e) {
e.printStackTrace();
}
//noinspection ResultOfMethodCallIgnored
destination.delete();
destination = new File(activity.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath(), "backup.zoomhelper");
shareFile(destination, activity);
}
public void restore(Activity activity) {
new FileChooser(activity).openFile();
}
@SuppressLint("ApplySharedPref")
public void restore(Activity activity, InputStream inp) {
Encrypter encrypter = new Encrypter();
SharedPreferences mPrefs = activity.getPreferences(Context.MODE_PRIVATE);
String decrypted = null;
//TODO: Passwort einlesen aus Eingabefeld
try {
decrypted = encrypter.decrypt(inp, "test");
} catch (IOException | InvalidAlgorithmParameterException | InvalidKeyException | NoSuchPaddingException | NoSuchAlgorithmException | InvalidKeySpecException | IllegalBlockSizeException | BadPaddingException e) {
e.printStackTrace();
}
//TODO: Hinweis, dass Daten mit den Daten aus dem Backup überschrieben werden...
SharedPreferences.Editor prefsEditor = mPrefs.edit();
prefsEditor.putString("Meetings",decrypted);
prefsEditor.commit();
prefsEditor.putString("lastMeeting", "0");
//TODO: Fehlermeldung bei falschem Passwort
}
public void writeStringToFile(String str, String fileName)
throws IOException {
FileOutputStream outputStream = new FileOutputStream(fileName);
byte[] strToBytes = str.getBytes();
outputStream.write(strToBytes);
outputStream.close();
}
public void shareFile(File file, Activity activity){
Intent intentShareFile = new Intent(Intent.ACTION_SEND);
if(file.exists()) {
intentShareFile.setType("application/octet-stream");
intentShareFile.putExtra(Intent.EXTRA_STREAM, Uri.parse(file.getAbsolutePath()));
intentShareFile.putExtra(Intent.EXTRA_SUBJECT,
"Sharing File...");
intentShareFile.putExtra(Intent.EXTRA_TEXT, "Sharing File...");
activity.startActivity(Intent.createChooser(intentShareFile, "Share File"));
}
}
}
@@ -0,0 +1,154 @@
package de.joel.zoomhelper;
import android.os.Build;
import android.os.Environment;
import androidx.annotation.RequiresApi;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.security.SecureRandom;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKeyFactory;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.spec.KeySpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.IvParameterSpec;
public class Encrypter {
@RequiresApi(api = Build.VERSION_CODES.O)
public void encrypt(String inputFile, String password) throws BadPaddingException, IllegalBlockSizeException, IOException, NoSuchAlgorithmException, InvalidKeySpecException, InvalidAlgorithmParameterException, InvalidKeyException, NoSuchPaddingException {
SecureRandom srandom = null;
try {
srandom = SecureRandom.getInstanceStrong();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
byte[] salt = new byte[8];
assert srandom != null;
srandom.nextBytes(salt);
SecretKeyFactory factory =
SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
KeySpec spec = new PBEKeySpec(password.toCharArray(), salt, 10000, 128);
SecretKey tmp = factory.generateSecret(spec);
SecretKeySpec skey = new SecretKeySpec(tmp.getEncoded(), "AES");
byte[] iv = new byte[128 / 8];
srandom.nextBytes(iv);
IvParameterSpec ivspec = new IvParameterSpec(iv);
FileOutputStream out = null;
out = new FileOutputStream(inputFile + ".enc");
out.write(salt);
out.write(iv);
Cipher ci = null;
ci = Cipher.getInstance("AES/CBC/PKCS5Padding");
ci.init(Cipher.ENCRYPT_MODE, skey, ivspec);
try (FileInputStream in = new FileInputStream(inputFile)) {
processFile(ci, in, out);
}
}
static private void processFile(Cipher ci, InputStream in, OutputStream out)
throws javax.crypto.IllegalBlockSizeException,
javax.crypto.BadPaddingException,
java.io.IOException {
byte[] ibuf = new byte[1024];
int len;
while ((len = in.read(ibuf)) != -1) {
byte[] obuf = ci.update(ibuf, 0, len);
if (obuf != null) out.write(obuf);
}
byte[] obuf = ci.doFinal();
if (obuf != null) out.write(obuf);
}
@SuppressWarnings("ResultOfMethodCallIgnored")
public String decrypt(String inputFile, String password) throws IOException, InvalidAlgorithmParameterException, InvalidKeyException, NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeySpecException, IllegalBlockSizeException, BadPaddingException {
FileInputStream in = new FileInputStream(inputFile);
byte[] salt = new byte[8], iv = new byte[128 / 8];
in.read(salt);
in.read(iv);
SecretKeyFactory factory =
SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
KeySpec spec = new PBEKeySpec(password.toCharArray(), salt, 10000, 128);
SecretKey tmp = factory.generateSecret(spec);
SecretKeySpec skey = new SecretKeySpec(tmp.getEncoded(), "AES");
Cipher ci = Cipher.getInstance("AES/CBC/PKCS5Padding");
ci.init(Cipher.DECRYPT_MODE, skey, new IvParameterSpec(iv));
OutputStream out = new OutputStream() {
private final StringBuilder string = new StringBuilder();
@Override
public void write(int b) throws IOException {
this.string.append((char) b );
}
//Netbeans IDE automatically overrides this toString()
public String toString() {
return this.string.toString();
}
};
processFile(ci, in, out);
return out.toString();
}
public String decrypt(InputStream in, String password) throws IOException, InvalidAlgorithmParameterException, InvalidKeyException, NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeySpecException, IllegalBlockSizeException, BadPaddingException {
byte[] salt = new byte[8], iv = new byte[128 / 8];
in.read(salt);
in.read(iv);
SecretKeyFactory factory =
SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
KeySpec spec = new PBEKeySpec(password.toCharArray(), salt, 10000, 128);
SecretKey tmp = factory.generateSecret(spec);
SecretKeySpec skey = new SecretKeySpec(tmp.getEncoded(), "AES");
Cipher ci = Cipher.getInstance("AES/CBC/PKCS5Padding");
ci.init(Cipher.DECRYPT_MODE, skey, new IvParameterSpec(iv));
OutputStream out = new OutputStream() {
private final StringBuilder string = new StringBuilder();
@Override
public void write(int b) throws IOException {
this.string.append((char) b );
}
//Netbeans IDE automatically overrides this toString()
public String toString() {
return this.string.toString();
}
};
processFile(ci, in, out);
return out.toString();
}
}
@@ -5,7 +5,11 @@ import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.EditText;
@@ -14,6 +18,7 @@ import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.Toast;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.pm.ShortcutInfoCompat;
@@ -25,6 +30,8 @@ import com.github.javiersantos.appupdater.enums.UpdateFrom;
import com.google.android.material.snackbar.Snackbar;
import com.google.android.material.textfield.TextInputLayout;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
@@ -140,6 +147,56 @@ public class MainActivity extends AppCompatActivity {
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public boolean onOptionsItemSelected(MenuItem item) {
final int item_backup = R.id.menu_backup;
final int item_restore = R.id.menu_restore;
final int item_settings = R.id.menu_settings;
final int item_info = R.id.menu_info;
final BackupController backupController = new BackupController();
switch (item.getItemId()) {
case item_backup:
backupController.backup(this);
break;
case item_restore:
backupController.restore(this);
break;
default:
break;
}
return true;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Uri fileUri = null;
String filePath = null;
if (requestCode == 2) {
fileUri = data.getData();
filePath = fileUri.getPath();
}
try {
new BackupController().restore(this, getContentResolver().openInputStream(fileUri));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// new BackupController().restore(this, file.getAbsolutePath());
}
private void importFromClipboard() {
final LinearLayout layoutBegin = findViewById(R.id.layoutBegin);
final ClipboardManager clipboard = (ClipboardManager) getApplicationContext().getSystemService(CLIPBOARD_SERVICE);
@@ -154,7 +211,7 @@ public class MainActivity extends AppCompatActivity {
if (clipboard.hasPrimaryClip() && clipboard.getPrimaryClipDescription().hasMimeType(MIMETYPE_TEXT_PLAIN)) {
final String text = clipboard.getPrimaryClip().getItemAt(0).getText().toString();
if ( meetingImportController.handleSendText(text, true)) {
if (meetingImportController.handleSendText(text, true)) {
snackbar.show();
}
@@ -240,7 +297,7 @@ public class MainActivity extends AppCompatActivity {
@SuppressLint("ClickableViewAccessibility")
public void fillDropdownMeetingName() {
final ArrayList<Meeting> meetings = meetingsController.getMeetings();
final ArrayList<Meeting> meetings = meetingsController.getMeetings();
String[] meetingNames = new String[meetings.size()];
for (int i = 0; i < meetings.size(); i++) {
@@ -119,6 +119,7 @@ public class MeetingsController {
return meetingList;
}
//TODO: Nach Remove bleibt Meeting noch stehen
public void removeMeeting(int meeting) {
MainActivity act = (MainActivity)context;
ArrayList<Meeting> meetings = getMeetings();