2014-06-30 34 views
1

我想編寫一個自動解密我的WhatsApp數據庫的Android應用程序,所以我跟着this教程並將其翻譯成Java。但後來我注意到Android上沒有openssl二進制文件,所以我問谷歌如何手動解密aes,但我找不到有用的東西。Android上的Whatsapp數據庫解密

所以基本上我得到這個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(); 
} 

請幫助我,如果你能提前:)

感謝,柚子

+0

什麼是堆棧跟蹤和exce ption錯誤信息? –

+0

異常消息是「InvalidKeyException:不支持的密鑰大小:64字節」 – Citron

+0

只是爲了闡明:您發佈的Java代碼在您的PC上執行,但在您的Android設備上執行時無效? –

回答

5

您需要將十六進制字符串正確轉換爲字節數組:

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(); 
} 
+0

非常感謝你,那就是:)不幸的是我沒有足夠的聲望來投票你的答案:/ – Citron

+0

嗨,我們可以把所有從該數據庫中聯繫狀態?聯繫狀態意味着:人們已經設置瞭如下狀態:我很忙,可用。 Cant Talk只有Whatsapp –

+0

decryptDatabase(k,iv);上述電話中'k'和'iv'是什麼意思?請給我解釋一下。 – Ravi