<commit_before>package net.yasme.android.encryption;

/**
 * Created by <NAME> on 22.07.2014.
 */

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Base64;
import android.util.Log;

import net.yasme.android.entities.Device;
import net.yasme.android.entities.MessageKey;
import net.yasme.android.entities.User;
import net.yasme.android.storage.DatabaseManager;

import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

public class KeyEncryption {

    private String RSAKEY_STORAGE = "rsaKeyStorage"; //Storage for Private and Public Keys from user
    private final String PRIVATEKEY = "privateKey";
    private final String PUBLICKEY = "publicKey";
    private RSAEncryption rsa;
    private DatabaseManager db = DatabaseManager.INSTANCE;

    //Constructor
    public KeyEncryption() {
        this.rsa = new RSAEncryption();
    }

    public void generateRSAKeys(){
        rsa.generateKeyPair();
    }

    //save own RSAKeys in SharedPreferences
    public boolean saveRSAKeys(long deviceId){

        RSAKEY_STORAGE += "_"+deviceId;

        try {

            Context context = DatabaseManager.INSTANCE.getContext();
            SharedPreferences privKeyStorage = context.getSharedPreferences(RSAKEY_STORAGE, Context.MODE_PRIVATE);
            SharedPreferences.Editor keyeditor = privKeyStorage.edit();

            //delete existing keys
            if (privKeyStorage.getString(PRIVATEKEY, "") != ""){
                keyeditor.remove(PRIVATEKEY);
            }
            if (privKeyStorage.getString(PUBLICKEY, "") != ""){
                keyeditor.remove(PUBLICKEY);
            }


            keyeditor.putString(PRIVATEKEY, rsa.getPrivKeyinBase64());
            keyeditor.putString(PUBLICKEY, rsa.getPubKeyinBase64());

            keyeditor.commit();

            Log.d(this.getClass().getSimpleName(), "[???] RSA Keys generated and saved");


            return true;
        } catch (Exception e){
            Log.d(this.getClass().getSimpleName(), "[???] saving rsa keys failed");
            Log.d(this.getClass().getSimpleName(),"[???] "+e.getMessage());
            return false;
        }
    }

    public String getGeneratedPubKeyInBase64(){
        return rsa.getPubKeyinBase64();
    }

    //encrypt
    //recipientDevice in MessageKey is needed
    public MessageKey encrypt(MessageKey messageKey){
        //TODO: static Public Key entfernen
        /*START*/

        String pubKey_base64 = "<KEY>";
        KeyFactory kf = null;
        PublicKey pubKey = null;
        try {
            kf = KeyFactory.getInstance("RSA");
            byte[] publicKeyBytes = Base64.decode(pubKey_base64, Base64.DEFAULT);
            pubKey = kf.generatePublic(new X509EncodedKeySpec(publicKeyBytes));
        } catch (Exception e) {
            e.printStackTrace();
        }

        /*END*/

        //long recipientDevice = messageKey.getRecipientDevice().getId();
        //PublicKey pubKey = getPubKeyFromUser(recipientDevice);

        if (pubKey != null){
            String keyEncrypted = rsa.encrypt(messageKey.getMessageKey(), pubKey);
            messageKey.setKey(keyEncrypted);
            return messageKey;
        }

        return null;
    }

    //decrypt
    public MessageKey decrypt(MessageKey messageKey){

        //TODO: static Public Key entfernen
        /*START*/

        String privKey_base64 = "<KEY>;
        KeyFactory kf = null;
        PrivateKey privKey = null;
        try {
            kf = KeyFactory.getInstance("RSA");
            byte[] privKeyBytes = Base64.decode(privKey_base64, Base64.DEFAULT);
            privKey = kf.generatePrivate(new PKCS8EncodedKeySpec(privKeyBytes));
        } catch (Exception e) {
            e.printStackTrace();
        }

        /*END*/

        //long selfDeviceId = messageKey.getRecipientDevice().getId();
        //PrivateKey privKey = getPrivateRSAKeyFromStorage(selfDeviceId);

        if (privKey != null) {
            String key = rsa.decrypt(messageKey.getMessageKey(), privKey);
            messageKey.setKey(key);
            return messageKey;
        }

        return null
    }

    //sign
    //creatorDevice in MessageKey is needed
    public MessageKey sign(MessageKey messageKey){
        //TODO: static Private Key entfernen
        /*START*/

        String privKey_base64 = "<KEY>;
        KeyFactory kf = null;
        PrivateKey privKey = null;
        try {
            kf = KeyFactory.getInstance("RSA");
            byte[] privKeyBytes = Base64.decode(privKey_base64, Base64.DEFAULT);
            privKey = kf.generatePrivate(new PKCS8EncodedKeySpec(privKeyBytes));
        } catch (Exception e) {
            e.printStackTrace();
        }

        /*END*/

        //long selfDeviceId = messageKey.getCreatorDevice().getId();
        //PrivateKey privKey = getPrivateRSAKeyFromStorage(selfDeviceId);

        if (privKey != null) {
            String keySigned = rsa.sign(messageKey.getMessageKey(), privKey);
            messageKey.setSign(keySigned);
            return messageKey;
        }<commit_msg>fixed<commit_after>package net.yasme.android.encryption;

/**
 * Created by <NAME> on 22.07.2014.
 */

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Base64;
import android.util.Log;

import net.yasme.android.entities.Device;
import net.yasme.android.entities.MessageKey;
import net.yasme.android.entities.User;
import net.yasme.android.storage.DatabaseManager;

import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

public class KeyEncryption {

    private String RSAKEY_STORAGE = "rsaKeyStorage"; //Storage for Private and Public Keys from user
    private final String PRIVATEKEY = "privateKey";
    private final String PUBLICKEY = "publicKey";
    private RSAEncryption rsa;
    private DatabaseManager db = DatabaseManager.INSTANCE;

    //Constructor
    public KeyEncryption() {
        this.rsa = new RSAEncryption();
    }

    public void generateRSAKeys(){
        rsa.generateKeyPair();
    }

    //save own RSAKeys in SharedPreferences
    public boolean saveRSAKeys(long deviceId){

        RSAKEY_STORAGE += "_"+deviceId;

        try {

            Context context = DatabaseManager.INSTANCE.getContext();
            SharedPreferences privKeyStorage = context.getSharedPreferences(RSAKEY_STORAGE, Context.MODE_PRIVATE);
            SharedPreferences.Editor keyeditor = privKeyStorage.edit();

            //delete existing keys
            if (privKeyStorage.getString(PRIVATEKEY, "") != ""){
                keyeditor.remove(PRIVATEKEY);
            }
            if (privKeyStorage.getString(PUBLICKEY, "") != ""){
                keyeditor.remove(PUBLICKEY);
            }


            keyeditor.putString(PRIVATEKEY, rsa.getPrivKeyinBase64());
            keyeditor.putString(PUBLICKEY, rsa.getPubKeyinBase64());

            keyeditor.commit();

            Log.d(this.getClass().getSimpleName(), "[???] RSA Keys generated and saved");


            return true;
        } catch (Exception e){
            Log.d(this.getClass().getSimpleName(), "[???] saving rsa keys failed");
            Log.d(this.getClass().getSimpleName(),"[???] "+e.getMessage());
            return false;
        }
    }

    public String getGeneratedPubKeyInBase64(){
        return rsa.getPubKeyinBase64();
    }

    //encrypt
    //recipientDevice in MessageKey is needed
    public MessageKey encrypt(MessageKey messageKey){
        //TODO: static Public Key entfernen
        /*START*/

        String pubKey_base64 = "<KEY>";
        KeyFactory kf = null;
        PublicKey pubKey = null;
        try {
            kf = KeyFactory.getInstance("RSA");
            byte[] publicKeyBytes = Base64.decode(pubKey_base64, Base64.DEFAULT);
            pubKey = kf.generatePublic(new X509EncodedKeySpec(publicKeyBytes));
        } catch (Exception e) {
            e.printStackTrace();
        }

        /*END*/

        //long recipientDevice = messageKey.getRecipientDevice().getId();
        //PublicKey pubKey = getPubKeyFromUser(recipientDevice);

        if (pubKey != null){
            String keyEncrypted = rsa.encrypt(messageKey.getMessageKey(), pubKey);
            messageKey.setKey(keyEncrypted);
            return messageKey;
        }

        return null;
    }

    //decrypt
    public MessageKey decrypt(MessageKey messageKey){

        //TODO: static Public Key entfernen
        /*START*/

        String privKey_base64 = "<KEY>;
        KeyFactory kf = null;
        PrivateKey privKey = null;
        try {
            kf = KeyFactory.getInstance("RSA");
            byte[] privKeyBytes = Base64.decode(privKey_base64, Base64.DEFAULT);
            privKey = kf.generatePrivate(new PKCS8EncodedKeySpec(privKeyBytes));
        } catch (Exception e) {
            e.printStackTrace();
        }

        /*END*/

        //long selfDeviceId = messageKey.getRecipientDevice().getId();
        //PrivateKey privKey = getPrivateRSAKeyFromStorage(selfDeviceId);

        if (privKey != null) {
            String key = rsa.decrypt(messageKey.getMessageKey(), privKey);
            messageKey.setKey(key);
            return messageKey;
        }

        return null;
    }

    //sign
    //creatorDevice in MessageKey is needed
    public MessageKey sign(MessageKey messageKey){
        //TODO: static Private Key entfernen
        /*START*/

        String privKey_base64 = "<KEY>;
        KeyFactory kf = null;
        PrivateKey privKey = null;
        try {
            kf = KeyFactory.getInstance("RSA");
            byte[] privKeyBytes = Base64.decode(privKey_base64, Base64.DEFAULT);
            privKey = kf.generatePrivate(new PKCS8EncodedKeySpec(privKeyBytes));
        } catch (Exception e) {
            e.printStackTrace();
        }

        /*END*/

        //long selfDeviceId = messageKey.getCreatorDevice().getId();
        //PrivateKey privKey = getPrivateRSAKeyFromStorage(selfDeviceId);

        if (privKey != null) {
            String keySigned = rsa.sign(messageKey.getMessageKey(), privKey);
            messageKey.setSign(keySigned);
            return messageKey;
        }
{'fraction_non_alphanumeric': 0.10718741003742444, 'fraction_numerical': 0.009787928221859706, 'mean_word_length': 2.3981089012063905, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '418709', 'n_tokens_mistral': 3006, 'n_tokens_neox': 2844, 'n_words': 606}