aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/pixart/messenger/utils
diff options
context:
space:
mode:
authorChristian Schneppe <christian@pix-art.de>2016-08-08 22:04:57 +0200
committerChristian Schneppe <christian@pix-art.de>2016-08-09 22:34:03 +0200
commitbb4c255314e0beb1713050cc231f317894997f53 (patch)
tree97a413d8b0db8759d8876ac2ee0ce9b56dff5f3e /src/main/java/de/pixart/messenger/utils
parent7dd493bff1ddfb0f12d95a6ed466f42973e09051 (diff)
reworked backup service
* automatically save database encrypted to local storage at 4 am each day * run backup import in new thread
Diffstat (limited to 'src/main/java/de/pixart/messenger/utils')
-rw-r--r--src/main/java/de/pixart/messenger/utils/EncryptDecryptFile.java73
1 files changed, 73 insertions, 0 deletions
diff --git a/src/main/java/de/pixart/messenger/utils/EncryptDecryptFile.java b/src/main/java/de/pixart/messenger/utils/EncryptDecryptFile.java
new file mode 100644
index 000000000..fd9392132
--- /dev/null
+++ b/src/main/java/de/pixart/messenger/utils/EncryptDecryptFile.java
@@ -0,0 +1,73 @@
+package de.pixart.messenger.utils;
+
+import android.util.Log;
+
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.security.InvalidKeyException;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+import java.util.Arrays;
+
+import javax.crypto.Cipher;
+import javax.crypto.CipherInputStream;
+import javax.crypto.CipherOutputStream;
+import javax.crypto.NoSuchPaddingException;
+import javax.crypto.spec.SecretKeySpec;
+
+import de.pixart.messenger.Config;
+
+public class EncryptDecryptFile {
+ private static String cipher_mode = "AES/ECB/PKCS5Padding";
+
+ public static void encrypt(FileInputStream iFile, FileOutputStream oFile, String iKey) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
+ byte[] key = iKey.getBytes("UTF-8");
+ Log.d(Config.LOGTAG, "Cipher key: " + Arrays.toString(key));
+ MessageDigest sha = MessageDigest.getInstance("SHA-1");
+ Log.d(Config.LOGTAG, "Cipher sha: " + sha.toString());
+ key = sha.digest(key);
+ Log.d(Config.LOGTAG, "Cipher sha key: " + Arrays.toString(key));
+ key = Arrays.copyOf(key, 16); // use only first 128 bit
+ Log.d(Config.LOGTAG, "Cipher sha key 16 bytes: " + Arrays.toString(key));
+ SecretKeySpec sks = new SecretKeySpec(key, "AES");
+ Cipher cipher = Cipher.getInstance(cipher_mode);
+ cipher.init(Cipher.ENCRYPT_MODE, sks);
+ Log.d(Config.LOGTAG, "Cipher IV: " + Arrays.toString(cipher.getIV()));
+ CipherOutputStream cos = new CipherOutputStream(oFile, cipher);
+ Log.d(Config.LOGTAG, "Encryption with: " + cos.toString());
+ int b;
+ byte[] d = new byte[8];
+ while ((b = iFile.read(d)) != -1) {
+ cos.write(d, 0, b);
+ }
+ cos.flush();
+ cos.close();
+ iFile.close();
+ }
+
+ public static void decrypt(FileInputStream iFile, FileOutputStream oFile, String iKey) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
+ byte[] key = iKey.getBytes("UTF-8");
+ Log.d(Config.LOGTAG, "Cipher key: " + Arrays.toString(key));
+ MessageDigest sha = MessageDigest.getInstance("SHA-1");
+ Log.d(Config.LOGTAG, "Cipher sha: " + sha.toString());
+ key = sha.digest(key);
+ Log.d(Config.LOGTAG, "Cipher sha key: " + Arrays.toString(key));
+ key = Arrays.copyOf(key, 16); // use only first 128 bit
+ Log.d(Config.LOGTAG, "Cipher sha key 16 bytes: " + Arrays.toString(key));
+ SecretKeySpec sks = new SecretKeySpec(key, "AES");
+ Cipher cipher = Cipher.getInstance(cipher_mode);
+ cipher.init(Cipher.DECRYPT_MODE, sks);
+ Log.d(Config.LOGTAG, "Cipher IV: " + Arrays.toString(cipher.getIV()));
+ CipherInputStream cis = new CipherInputStream(iFile, cipher);
+ Log.d(Config.LOGTAG, "Decryption with: " + cis.toString());
+ int b;
+ byte[] d = new byte[8];
+ while ((b = cis.read(d)) != -1) {
+ oFile.write(d, 0, b);
+ }
+ oFile.flush();
+ oFile.close();
+ cis.close();
+ }
+}