How to use Facebook Conceal Method to Encrypt your data like password before storing in SQLite Database in Android? – Complete source code

In this video, it is showns how one can encrypt the data using the Facebook Conceal encryption method. To demonstrate the same it takes the example of storing critical data such as password in SQLite database. So, this video will also be useful for someone who needs to understand how to work with SQLite database.

For other encryption method like AES, you can refer to my below video which shows the steps to “Create Android chat message App with End to End AES(Advanced Encryption Standard) method in Firebase”

The Facebbok Conceal Method files such library jar files, packages, etc. used in the video can be found below. These files can also be sent on demand. Please write to: programmerworld1990@gmail.com

facebook – Unzip and Place it at MyFacebookConcealEncrypt\app\src\main\java\com

libs – Unzip and Place it at MyFacebookConcealEncrypt\app folder.

Source Code:

package com.example.myfacebookconcealencrypt;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class mySQLLiteHandle extends SQLiteOpenHelper {
    public mySQLLiteHandle(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {

    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }
}
package com.example.myfacebookconcealencrypt;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.facebook.android.crypto.keychain.SharedPrefsBackedKeyChain;
import com.facebook.crypto.Crypto;
import com.facebook.crypto.CryptoConfig;
import com.facebook.crypto.Entity;
import com.facebook.crypto.exception.CryptoInitializationException;
import com.facebook.crypto.exception.KeyChainException;
import com.facebook.crypto.util.SystemNativeCryptoLibrary;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

public class MainActivity extends AppCompatActivity {

    private mySQLLiteHandle dbHandle;
    private SQLiteDatabase sqLiteDatabase;

    private EditText editTextKey;
    private EditText editTextPassword;

    private TextView textViewRetrievePassword;
    private TextView textViewStoredPassword;

    private Entity entity = new Entity("password"); //Key for Facebook Conceal Method

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

        editTextKey = findViewById(R.id.editTextKey);
        editTextPassword = findViewById(R.id.editTextPwd);

        textViewRetrievePassword = findViewById(R.id.textViewActualPwd);
        textViewStoredPassword = findViewById(R.id.textViewEncryptedPwd);

        try{

            dbHandle = new mySQLLiteHandle(this, "PassWordStorage", null, 1);
            sqLiteDatabase = dbHandle.getWritableDatabase();
            sqLiteDatabase.execSQL("CREATE TABLE PasswordTable(Keyword TEXT, Password, TEXT)");
        }
        catch (Exception e){
            e.printStackTrace();
        }


    }

    public void WriteDatabase(View view){
        Crypto crypto = new Crypto(new SharedPrefsBackedKeyChain(this, CryptoConfig.KEY_256),
                new SystemNativeCryptoLibrary(), CryptoConfig.KEY_256);

        byte[] ciphertext = null;

        try {
            ciphertext = crypto.encrypt(editTextPassword.getText().toString().getBytes(), entity);
        } catch (KeyChainException e) {
            e.printStackTrace();
        } catch (CryptoInitializationException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        ContentValues contentValues = new ContentValues();

        contentValues.put("Keyword", editTextKey.getText().toString());

        String finalString = null;
        try {
            finalString = new String(ciphertext, "ISO-8859-1");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        contentValues.put("Password", finalString);

        sqLiteDatabase.insert("PasswordTable", null, contentValues);
        sqLiteDatabase.update("PasswordTable",contentValues, null, null);
    }

    public void RetrievePassword(View view){

        String query = "Select Password from PasswordTable where Keyword = " +"\"" + editTextKey.getText().toString() + "\"";
        Cursor cursor = sqLiteDatabase.rawQuery(query, null);
        cursor.moveToFirst();
        String finalString =  cursor.getString(0);

        try {
            byte[] cipherText = cursor.getString(0).getBytes("ISO-8859-1");

            Crypto crypto = new Crypto(new SharedPrefsBackedKeyChain(this, CryptoConfig.KEY_256),
                    new SystemNativeCryptoLibrary(), CryptoConfig.KEY_256);

            finalString = new String(crypto.decrypt(cipherText, entity));


        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (KeyChainException e) {
            e.printStackTrace();
        } catch (CryptoInitializationException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        textViewRetrievePassword.setText(finalString);

    }

    public void EncryptedPassword(View view){
        String query = "Select Password from PasswordTable where Keyword = " +"\"" + editTextKey.getText().toString() + "\"";
        Cursor cursor = sqLiteDatabase.rawQuery(query, null);
        cursor.moveToFirst();
        textViewStoredPassword.setText(cursor.getString(0));

    }
}

We will be glad to hear from you regarding any query, suggestions or appreciations at: programmerworld1990@gmail.com

9 comments

  1. Getting this error
    com.facebook.crypto.exception.CryptoInitializationException: java.lang.UnsatisfiedLinkError

    How to fix it ?

Leave a Reply