Improvements

This commit is contained in:
Thomas 2021-03-02 19:16:01 +01:00
parent c0b8c564df
commit 1e9bbf4d5c
3 changed files with 34 additions and 11 deletions

View File

@ -220,7 +220,6 @@ public abstract class BaseMainActivity extends BaseActivity
Intent intent = getIntent();
PackageManager pm = getPackageManager();
ECDH.uncryptMessage(BaseMainActivity.this, "", "Underground@toot.fedilab.app");
try {
if (intent != null && intent.getComponent() != null) {
ActivityInfo ai = pm.getActivityInfo(intent.getComponent(), PackageManager.GET_META_DATA);

View File

@ -23,6 +23,7 @@ import android.util.Log;
import androidx.preference.PreferenceManager;
import org.spongycastle.asn1.ASN1ObjectIdentifier;
import org.spongycastle.asn1.x9.ECNamedCurveTable;
import org.spongycastle.asn1.x9.X9ECParameters;
@ -36,7 +37,9 @@ import org.spongycastle.jce.spec.ECPublicKeySpec;
import org.spongycastle.math.ec.ECCurve;
import org.spongycastle.math.ec.ECPoint;
import java.io.ByteArrayInputStream;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
@ -47,11 +50,13 @@ import java.security.PublicKey;
import java.security.Security;
import java.security.interfaces.ECPublicKey;
import java.security.spec.ECGenParameterSpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.ArrayList;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.KeyAgreement;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
import app.fedilab.android.client.Entities.Account;
import app.fedilab.android.sqlite.AccountDAO;
@ -198,26 +203,37 @@ public class ECDH {
public static String uncryptMessage(Context context, String cyphered, String slug) {
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
String crypted_message_test = sharedpreferences.getString("CRYPTED_MESSAGE_TEST", null);
Log.v(Helper.TAG, "crypted_message_test: " + crypted_message_test);
getInstance();
SQLiteDatabase db = Sqlite.getInstance(context.getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
String[] slugArray = slug.split("@");
Account account = new AccountDAO(context, db).getUniqAccountUsernameInstance(slugArray[0], slugArray[1]);
String uncrypted = crypted_message_test;
byte[] privateKey = getSharedSecret(context, account);
try {
PublicKey dddd = getServerKey(context, account);
Cipher outCipher = Cipher.getInstance("ECIES", "SC");
outCipher.init(Cipher.DECRYPT_MODE, readPrivateKey(privateKey));
CipherInputStream cipherInputStream = new CipherInputStream(
new ByteArrayInputStream(Base64.decode(cyphered, Base64.DEFAULT)), outCipher);
ArrayList<Byte> values = new ArrayList<>();
int nextByte;
while ((nextByte = cipherInputStream.read()) != -1) {
values.add((byte) nextByte);
}
byte[] bytes = new byte[values.size()];
for (int i = 0; i < bytes.length; i++) {
bytes[i] = values.get(i).byteValue();
}
String finalText = new String(bytes, 0, bytes.length, StandardCharsets.UTF_8);
Log.v(Helper.TAG, "---> " + finalText);
return finalText;
} catch (Exception e) {
e.printStackTrace();
}
byte[] ddd = getSharedSecret(context, account);
return "";
}
@ -231,6 +247,11 @@ public class ECDH {
}
public static PrivateKey readPrivateKey(byte[] key) throws Exception {
SecretKeySpec signingKey = new SecretKeySpec(key, "ECIES");
return kf.generatePrivate(signingKey);
}
static synchronized PrivateKey readMyPrivateKey(Context context) throws Exception {
X9ECParameters x9 = ECNamedCurveTable.getByName("prime256v1");
ASN1ObjectIdentifier oid = ECNamedCurveTable.getOID("prime256v1");

View File

@ -24,7 +24,9 @@ import org.unifiedpush.android.connector.MessagingReceiver;
import org.unifiedpush.android.connector.MessagingReceiverHandler;
import app.fedilab.android.activities.BaseMainActivity;
import app.fedilab.android.client.Entities.Account;
import app.fedilab.android.helper.ECDH;
import app.fedilab.android.helper.Helper;
import app.fedilab.android.helper.NotificationsHelper;
import app.fedilab.android.helper.PushNotifications;
@ -51,6 +53,7 @@ class handler implements MessagingReceiverHandler {
new Thread(() -> {
SQLiteDatabase db = Sqlite.getInstance(context.getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
String[] slugArray = slug.split("@");
//ECDH.uncryptMessage(context, s, slug);
Account account = new AccountDAO(context, db).getUniqAccountUsernameInstance(slugArray[0], slugArray[1]);
NotificationsHelper.task(context, account);
}).start();