为什么我的java代码没有正确解密db文件?

时间:2014-02-28 18:19:56

标签: java encryption

我试图用java代码解密whatsapp数据库文件。检查使用Python解密的whatsapp_xtract代码。我相信这是代码的解密部分:

from Crypto.Cipher import AES
code = "346a23652a46392b4d73257c67317e352e3372482177652c"
if PYTHON_VERSION == 2:
     code = code.decode('hex')
elif PYTHON_VERSION == 3:
     code = bytes.fromhex(code)
ipher = AES.new(code,1)
decoded = cipher.decrypt(open(options.infile,"rb").read())
decodedfile = options.infile.replace(".db.crypt","")+".plain.db"
output = open(decodedfile,"wb")
output.write(decoded)
output.close()

此代码运行良好,我可以使用SqLiteBrowser打开bd文件。这是我的java代码:

public class Crypto {

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

public static void decrypt(InputStream in, FileOutputStream out, String password) {
        try {
                // byte[] iv = new byte[IV_LENGTH];
                byte[] iv = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
                Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
                in.read(iv);
                System.out.println(">>>>>>>>red" + Arrays.toString(iv));

                String s = "346a23652a46392b4d73257c67317e352e3372482177652c";

                byte[] sBytes = hexStringToByteArray(s);

                byte[] bytes = new BigInteger(s, 16).toByteArray();
                SecretKeySpec keySpec = new SecretKeySpec(sBytes, "AES");
                Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding", "BC"); // "AES/CFB8/NoPadding";"AES/CBC/PKCS5Padding";
                // //"AES/ECB/PKCS5Padding"

                IvParameterSpec ivSpec = new IvParameterSpec(iv);
                 cipher.init(Cipher.DECRYPT_MODE, keySpec);// , ivSpec);
                //cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);

                in = new CipherInputStream(in, cipher);
                byte[] buf = new byte[iv.length];
                int numRead = 0;
                while ((numRead = in.read(buf)) >= 0) {
                    String si = new String(buf);
                //  System.out.println(si);
                     out.write(buf, 0, numRead);
                        // Log.d("Crypto", buf.toString());
                }
                out.close();

        } catch (Exception e) {
                e.printStackTrace();
        }

}

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;
}
    public static void main(String[] args) throws ShortBufferException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException, InvalidKeyException {

        Crypto c = new Crypto("C:\\msgstore.db.crypt", "D:\\WhatsappDeneme", "test");
        System.out.println("Done");

    }

}

当我使用这个java代码出错时,我无法用SqLiteBrowser打开db文件。此外,当我检查db文件的大小时,我意识到原始文件和Python解密是29 kb但java解密是28 kb。那么我在java代码中的错误在哪里?

1 个答案:

答案 0 :(得分:0)

WhatsApp DB crypt5可以使用用户的帐户名在android中解密 请检查此答案以解密whatsApp crypt5 DB(android code).