From bb4c255314e0beb1713050cc231f317894997f53 Mon Sep 17 00:00:00 2001 From: Christian Schneppe Date: Mon, 8 Aug 2016 22:04:57 +0200 Subject: reworked backup service * automatically save database encrypted to local storage at 4 am each day * run backup import in new thread --- .../pixart/messenger/utils/EncryptDecryptFile.java | 73 ++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 src/main/java/de/pixart/messenger/utils/EncryptDecryptFile.java (limited to 'src/main/java/de/pixart/messenger/utils/EncryptDecryptFile.java') 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(); + } +} -- cgit v1.2.3