2014-02-28 42 views
1

我想用java代码解密whatsapp数据库文件。检查whatsapp_xtract正在使用Python解密的代码。我相信这是代码的解密部分:为什么我的java代码不能正确解密数据库文件?

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() 

此代码工作得很好,我可以打开BD文件,SqLiteBrowser。这是我的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解密是29kb,但是java解密是28kb。那么我的Java代码中的错误在哪里?

+0

您应该包含任何和所有错误以帮助我们帮助您进行调试。 –

+0

没有错误。实际上,在Java解密之后,当我用记事本++打开它时,我可以看到消息。只是我不能用Sqlite浏览器打开数据库文件,或者当我尝试用android打开它时,它说该文件已加密,或者它不是数据库文件。我的意思是java解密,但不能保存文件格式。 –

+0

那么你是用你的Python脚本进行加密,然后用你的Java脚本进行解密,或者用双方进行加密/解密等。 – Drewness

回答

相关问题