java - 为什么我的 java 代码不能正确解密 db 文件?

标签 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文件的大小时,我意识到原始文件和Python解密是29 kb,但java解密是28 kb。那么我的java代码错误在哪里呢?

最佳答案

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

关于java - 为什么我的 java 代码不能正确解密 db 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22102881/

相关文章:

sqlite - 如何覆盖 UIManagedDocument 中的 NSPersistentStoreCoordinator

security - El Gamal 比具有相同模数长度的 RSA 快吗?

java - 将 C# RSACryptoServiceProvider 代码翻译成 Java

linux - 仅针对Linux中的特定进程自动解密文件

security - 我应该使用什么算法将密码散列到我的数据库中?

java - java中如何转换分钟百分比

java - 通过环绕立体声扬声器输出多声道音频

java - CSV 文件模式匹配也匹配分隔符

java - 由 : org. springframework.beans.factory.NoSuchBeanDefinitionException 引起:Spring 5 中没有符合类型的 bean

JavaFX : can controller be an abstract class?