2014-02-28 43 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

回答

相關問題