Ad

How To Add End-to-end Encryption In Android Firebase App With Virgil Security SDK

Please help me! I've been trying to include some code in my Android chat app after going through the Virgil Security E3Kit documentation but to no avail. Actually, I don't know where to put those sample code fragments.

  1. My app has a setup/login activity, where a user is authenticated before they can chat. The Firebase authentication goes like this:

    public void registerUser(final String email, final String password){
        if(email.equals("") || password.equals("")){
            Toast.makeText(getBaseContext(), "Please enter email and/or password",
                    Toast.LENGTH_LONG).show();
        }else{
            //auth is already initialized somewhere
            auth.createUserWithEmailAndPassword(email, password)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()) {
    
                        FirebaseUser user = auth.getCurrentUser();
                        Intent intent = new Intent(SetupActivity.this,
                                MoreActivity.class);
                        intent.putExtra("email", email);
                        intent.putExtra("userID", user.getUid());
                        startActivity(intent);
                    }else{
                        Toast.makeText(SetupActivity.this, "Error logging in, try again", Toast.LENGTH_LONG).show();
                    }
                }
            });
        }
    }
    

My problem is that I don't know where/how to add the part that deals with user registration from the Virgil docs to the above method.

  1. After user authentication, they can easily chat with their contacts. Again, there is a problem here as I have no clue where initialization and encryption of chats should be placed - in the onCreate, onStart etc methods or somewhere else.

This is the onCreate method from ChatActivity which inserts the chats entered by users into Firebase database:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat);

        Toolbar toolbar = (Toolbar) findViewById(R.id.chatToolbar);
        setSupportActionBar(toolbar);
        toolbar.setNavigationIcon(getResources().getDrawable(R.drawable.ic_arrow_back_white_24dp));
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //What to do on back clicked
                onSupportNavigateUp();
            }
        });

        //Get receiver name from Intent extras
        Intent intent = getIntent();
        receiverName = intent.getStringExtra("receiverName");
        toolbar.setTitle(receiverName);

        database = FirebaseDatabase.getInstance();
        reference = database.getReference("chats");

        toolbar.setSubtitle(receiverPhone);

        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(true);

        //Firebase instance and user details
        auth = FirebaseAuth.getInstance();
        user = auth.getCurrentUser();
        phone = user.getPhoneNumber();

        userName = user.getDisplayName();
        userId = user.getUid();
        userPhotoUrl = user.getPhotoUrl();

        //Get widgets
        newChat = (EditText) findViewById(R.id.chatMessage);
        receiverMsg = (TextView) findViewById(R.id.receiverMessage);
        myMsg = (TextView) findViewById(R.id.myMessage);

        dateAdded = (TextView) findViewById(R.id.dateAdded);
        receivedDate = (TextView) findViewById(R.id.receivedDate);

        myPicText = (ImageView) findViewById(R.id.myPicture);
        receiverPicText = (ImageView) findViewById(R.id.receiverPicture);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String message = newChat.getText().toString();
                Chat chat = new Chat(userId, message, receiverID, "");
                DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mma");
                String date = dateFormat.format(Calendar.getInstance(
                        TimeZone.getDefault()).getTime());

                chat.setTime(date);
                DatabaseReference ref = reference.push();

                ref.setValue(chat);
                newChat.getText().clear();
            }
        });
    }

My problem is the encryption part as the Firebase authentication and chats implementations already work perfectly.

Ad

Answer

Nayi.

e3kit initialization and user registration should happen just after authentication with firebase. User registration only happens once, preferably in sign up, and initialization every sign in. In your code, it would happen before startActivity(intent);.

e3kit encryption should happen before you build your Chat object, so before Chat chat = new Chat(userId, message, receiverID, ""); make sure you do message = eThree.encrypt(message, publicKeys);. Decryption, of course, will happen after you receive an encrypted message.

If you need more details, you can take a look at this sample: https://github.com/VirgilSecurity/virgil-e3kit-kotlin/blob/master/samples/android-java-firebase-function/app/src/main/java/com/android/virgilsecurity/ethreesamplejavafirebase/EThreeActivity.java

Also, another good place to get support is Virgil Security's slack community: https://virgilsecurity.com/join-community

Ad
source: stackoverflow.com
Ad