aboutsummaryrefslogtreecommitdiffstats
path: root/src/org/thialfihar/android/apg/ApgService.java
blob: 6110fe7cd6dc0aa1d74d717752c7e9b81f00e7e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package org.thialfihar.android.apg;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

public class ApgService extends Service {
    static String TAG = "ApgService";

    @Override
    public IBinder onBind(Intent intent) {
        Log.d(TAG, "bound");
        return mBinder;
    }

    private final IApgService.Stub mBinder = new IApgService.Stub() {

        public String encrypt_with_passphrase(String msg, String passphrase) {
            Preferences mPreferences = Preferences
                    .getPreferences(getApplicationContext());
            InputStream inStream = new ByteArrayInputStream(msg.getBytes());
            InputData in = new InputData(inStream, 9999);
            OutputStream out = new ByteArrayOutputStream();
            long enc_keys[] = {};

            Apg.initialize(getApplicationContext());
            try {
                Apg.encrypt(
                        getApplicationContext(),
                        in,
                        out,
                        true, // armored
                        enc_keys, // encryption keys
                        0, // signature key
                        null, // signature passphrase
                        null, // progress
                        mPreferences.getDefaultEncryptionAlgorithm(),
                        mPreferences.getDefaultHashAlgorithm(),
                        Id.choice.compression.none, false, // mPreferences.getForceV3Signatures(),
                        passphrase // passPhrase
                        );
            } catch (Exception e) {
                Log.d(TAG, "Exception in encrypt");
                e.printStackTrace();
                return null;
            }
            Log.d(TAG, "Encrypted");
            return out.toString();
        }

        public String decrypt_with_passphrase(String encrypted_msg,
                String passphrase) {
            InputStream inStream = new ByteArrayInputStream(encrypted_msg
                    .getBytes());
            InputData in = new InputData(inStream, 9999); // XXX what size in
                                                          // second parameter?
            OutputStream out = new ByteArrayOutputStream();
            try {
                Apg.decrypt(getApplicationContext(), in, out, passphrase, null, // progress
                        true // symmetric
                        );
            } catch (Exception e) {
                Log.d(TAG, "Exception in decrypt");
                e.printStackTrace();
                return null;
            }

            return out.toString();
        }
    };
}