Android上的Whatsapp数据库解密

时间:2014-06-30 19:28:00

标签: java android encryption whatsapp

我想写一个自动解密我的Whatsapp数据库的Android应用程序,所以我按照this教程将其翻译成Java。但后来我注意到Android上没有openssl二进制文件所以我问google如何手动解密aes但我找不到有用的东西。

所以基本上我得到了这个shell命令

openssl enc -aes-256-cbc -d -nosalt -nopad  -bufsize 16384 -in msgstore.db.crypt7.nohdr -K $k -iv $iv > msgstore.db

$ k是64位十六进制字符串。但是当我尝试将它用作aes解密的密钥时,我得到一个带有“Unsupported key size:64 bytes”消息的InvalidKeyException。 当我在计算机上执行此命令时,我的工作非常完美。

我目前正在使用这个java代码来解密数据库,它在cipher.init失败了:

public void decryptDatabase(String k, String iv)

throws InvalidKeyException, InvalidAlgorithmParameterException,
        NoSuchAlgorithmException, NoSuchPaddingException, IOException {

    File extStore = Environment.getExternalStorageDirectory();
    FileInputStream fis = new FileInputStream(extStore
            + "/WhatsApp/Databases/msgstore.db.crypt7.nohdr");
    FileOutputStream fos = new FileOutputStream(extStore + "/Decrypted.db");

    SecretKeySpec sks = new SecretKeySpec(k.getBytes(), "AES");
    Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
    cipher.init(Cipher.DECRYPT_MODE, sks,
            new IvParameterSpec(iv.getBytes()));
    CipherInputStream cis = new CipherInputStream(fis, cipher);
    int b;
    byte[] d = new byte[8];
    while ((b = cis.read(d)) != -1) {
        fos.write(d, 0, b);
    }
    fos.flush();
    fos.close();
    cis.close();
}

如果可以,请帮助我:)。

提前致谢,Citron

1 个答案:

答案 0 :(得分:6)

您需要将十六进制字符串正确转换为字节数组:

private 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;
}

public void decryptDatabase(String k, String iv) throws InvalidKeyException, InvalidAlgorithmParameterException,
        NoSuchAlgorithmException, NoSuchPaddingException, IOException {

    File extStore = Environment.getExternalStorageDirectory();
    FileInputStream fis = new FileInputStream(extStore
            + "/WhatsApp/Databases/msgstore.db.crypt7.nohdr");
    FileOutputStream fos = new FileOutputStream(extStore + "/Decrypted.db");

    SecretKeySpec sks = new SecretKeySpec(hexStringToByteArray(k), "AES");
    Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
    cipher.init(Cipher.DECRYPT_MODE, sks,
            new IvParameterSpec(hexStringToByteArray(iv)));
    CipherInputStream cis = new CipherInputStream(fis, cipher);
    int b;
    byte[] d = new byte[8];
    while ((b = cis.read(d)) != -1) {
        fos.write(d, 0, b);
    }
    fos.flush();
    fos.close();
    cis.close();
}