Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Read WhatsApp Data

So far I googled a lot about decrypting whatsapp db file with extension as,

.db.crypt5

but no approach worked for me. I tried a Crypto.class which can be seen a lot in stackoverflow links to read WhatsApp database file, but that file also did not work for me.

This is my Crypto.class :

   import java.io.File;
   import java.io.FileInputStream;
   import java.io.FileOutputStream;
   import java.io.InputStream;

   import javax.crypto.Cipher;
   import javax.crypto.CipherInputStream;
   import javax.crypto.spec.SecretKeySpec;

    import android.util.Log;

  public class Crypto
 {

public FileInputStream mIn;
public FileOutputStream mOut;

public Crypto(String fileIn, String fileOut)
{
    try
    {
        mIn = new FileInputStream(new File(fileIn));
        mOut = new FileOutputStream(new File(fileOut));
        decrypt(mIn, mOut);
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}

public static void decrypt(InputStream in, FileOutputStream out) throws Exception
{
    final String string = "346a23652a46392b4d73257c67317e352e3372482177652c";
    byte[] hexAsBytes = hexStringToByteArray(string);

    SecretKeySpec keySpec = new SecretKeySpec(hexAsBytes, "AES");
    Cipher cipher = Cipher.getInstance("AES");

    cipher.init(Cipher.DECRYPT_MODE, keySpec);

    in = new CipherInputStream(in, cipher);
    byte[] buffer = new byte[24];
    int bytesRead;
    while ((bytesRead = in.read(buffer)) != -1)
    {
        out.write(buffer, 0, bytesRead);
        String si = new String(buffer);
        Log.d("Crypto", si);
    }

}

public static byte[] hexStringToByteArray(String s)
{
    int len = s.length();
    byte[] data = new byte[len / 2];
    for(int i = 0; i < len; i += 2)
    {
        data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i + 1), 16));
    }
    return data;
}

       }  

and this is how I am calling the constructor in my activity:

Crypto c = new Crypto("/sdcard/WhatsApp/Databases/msgstore.db.crypt5", "/sdcard/My_Folder_Name/watsapp");

I just want to decrypt this file and see all chats. For testing, I am trying on this file msgstore.db.crypt5.

I need help where I am doing wrong?

like image 671
Noman Avatar asked Mar 21 '14 06:03

Noman


People also ask

How can I see all WhatsApp data?

Step 1: Open the WhatsApp app and tap on the three-dotted icon, which is located on the top right corner of the screen. You then need to go to Settings. Step 2: Tap on the “Data and Storage Usage” option. Here you will be able to find who all are using up your phone storage.

How can I read WhatsApp backup in Google Drive Android?

Step 1: Uninstall WhatsApp and reinstall it. Then launch WhatsApp and you'll be asked to enter the verification number. Step 2: When asked, click "Restore" to restore your conversations and media files from Google Drive.


2 Answers

Working Android Code: (No root required)

Once you have access to the dbcrypt5 file , here is the android code to decrypt it:

private byte[] key = { (byte) 141, 75, 21, 92, (byte) 201, (byte) 255,
        (byte) 129, (byte) 229, (byte) 203, (byte) 246, (byte) 250, 120,
        25, 54, 106, 62, (byte) 198, 33, (byte) 166, 86, 65, 108,
        (byte) 215, (byte) 147 };

private final byte[] iv = { 0x1E, 0x39, (byte) 0xF3, 0x69, (byte) 0xE9, 0xD,
        (byte) 0xB3, 0x3A, (byte) 0xA7, 0x3B, 0x44, 0x2B, (byte) 0xBB,
        (byte) 0xB6, (byte) 0xB0, (byte) 0xB9 };
   long start = System.currentTimeMillis();

    // create paths
    backupPath = Environment.getExternalStorageDirectory()
            .getAbsolutePath() + "/WhatsApp/Databases/msgstore.db.crypt5";
    outputPath = Environment.getExternalStorageDirectory()
            .getAbsolutePath() + "/WhatsApp/Databases/msgstore.db.decrypt";

    File backup = new File(backupPath);

    // check if file exists / is accessible
    if (!backup.isFile()) {
        Log.e(TAG, "Backup file not found! Path: " + backupPath);
        return;
    }

    // acquire account name
    AccountManager manager = AccountManager.get(this);
    Account[] accounts = manager.getAccountsByType("com.google");

    if (accounts.length == 0) {
        Log.e(TAG, "Unable to fetch account!");
        return;
    }

    String account = accounts[0].name;

    try {
        // calculate md5 hash over account name
        MessageDigest message = MessageDigest.getInstance("MD5");
        message.update(account.getBytes());
        byte[] md5 = message.digest();

        // generate key for decryption
        for (int i = 0; i < 24; i++)
            key[i] ^= md5[i & 0xF];

        // read encrypted byte stream
        byte[] data = new byte[(int) backup.length()];
        DataInputStream reader = new DataInputStream(new FileInputStream(
                backup));
        reader.readFully(data);
        reader.close();

        // create output writer
        File output = new File(outputPath);
        DataOutputStream writer = new DataOutputStream(
                new FileOutputStream(output));

        // decrypt file
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        SecretKeySpec secret = new SecretKeySpec(key, "AES");
        IvParameterSpec vector = new IvParameterSpec(iv);
        cipher.init(Cipher.DECRYPT_MODE, secret, vector);
        writer.write(cipher.update(data));
        writer.write(cipher.doFinal());
        writer.close();
    } catch (NoSuchAlgorithmException e) {
        Log.e(TAG, "Could not acquire hash algorithm!", e);
        return;
    } catch (IOException e) {
        Log.e(TAG, "Error accessing file!", e);
        return;
    } catch (Exception e) {
        Log.e(TAG, "Something went wrong during the encryption!", e);
        return;
    }

    long end = System.currentTimeMillis();

    Log.i(TAG, "Success! It took " + (end - start) + "ms");
like image 159
amalBit Avatar answered Oct 05 '22 22:10

amalBit


I have done this by using following :

Root your device and try this code::

public void copyDbToSdcard()
{
    try
    {
        String comando = "cp -r /data/data/com.whatsapp/databases/msgstore.db /sdcard/My_Custom_Folder/";
        Process suProcess = Runtime.getRuntime().exec("su");
        DataOutputStream os = new DataOutputStream(suProcess.getOutputStream());
        os.writeBytes(comando + "\n");
        os.flush();
        os.writeBytes("exit\n");
        os.flush();
        try
        {
            int suProcessRetval = suProcess.waitFor();
            if(255 != suProcessRetval)
            {
                //
            }
            else
            {
                //
            }
        }
        catch (Exception ex)
        {
            Log.e("ERROR-->", ex.toString());
        }
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}  


    private void openAndQueryDatabase()
{
    try
    {
        DataBaseHelper dbHelper = new DataBaseHelper(this.getApplicationContext());
        newDB = dbHelper.getWritableDatabase();

        Cursor c = newDB.rawQuery("SELECT data FROM messages where data!=''", null);

        if(c != null)
        {
            if(c.moveToFirst())
            {
                do
                {

                    String data = c.getString(c.getColumnIndex("data"));

                    results.add(data); //adding to arrayList

                }
                while (c.moveToNext());
            }
        }

                while (c3.moveToNext());
            }
        }
    }

    catch (SQLiteException se)
    {
        Log.e(getClass().getSimpleName(), "Could not create or Open the database");
    }
}  

And then display results in your TextView or wherever you want.

like image 43
Noman Avatar answered Oct 05 '22 22:10

Noman