2012-04-19 111 views
0

我使用這種代碼:http://examples.javacodegeeks.com/core-java/crypto/encrypt-decrypt-file-stream-with-des加密我的zip文件在Android應用程序中使用。它使用zip文件工作得很好,當我嘗試使用java代碼時,但是當我在Android應用程序中嘗試相同的方法 - 它解密文件但我得到的文件已損壞,無法打開它。在Android中加密和解密zip文件?

登錄:

  04-19 10:58:25.711: W/System.err(6752): net.lingala.zip4j.exception.ZipException: zip headers not found. probably not a zip file 
     04-19 10:58:25.721: W/System.err(6752): at net.lingala.zip4j.core.HeaderReader.readEndOfCentralDirectoryRecord(HeaderReader.java:122) 

當我嘗試打開W​​indows上的同一個文件使用WinZip它顯示:

Does not appear to be a valid archive file. 

更新::

public class EncryptDecryptFileStreamWithDES { 

private static Cipher ecipher; 
private static Cipher dcipher; 

// 8-byte initialization vector 
private static byte[] iv = { 
    (byte)0xB2, (byte)0x12, (byte)0xD5, (byte)0xB2, 
    (byte)0x44, (byte)0x21, (byte)0xC3, (byte)0xC3 
}; 

public static void call() { 

    try { 

     SecretKey key = KeyGenerator.getInstance("DES").generateKey(); 

     AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv); 

     ecipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); 
     dcipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); 

     ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec); 
     dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec); 

    // encrypt(new FileInputStream("C:\\Users\\Admin\\Desktop\\zipped\\4.zip"), new FileOutputStream("C:\\Users\\Admin\\Desktop\\zipped\\4.dat")); 
     // decrypt(new FileInputStream("C:\\Users\\Admin\\Desktop\\zipped\\4.dat"), new FileOutputStream("C:\\Users\\Admin\\Desktop\\zipped\\4new.zip")); 

    //} 
    //catch (FileNotFoundException e) { 
     //System.out.println("File Not Found:" + e.getMessage()); 
     //return; 
    } 
    catch (InvalidAlgorithmParameterException e) { 
     System.out.println("Invalid Alogorithm Parameter:" + e.getMessage()); 
     return; 
    } 
    catch (NoSuchAlgorithmException e) { 
     System.out.println("No Such Algorithm:" + e.getMessage()); 
     return; 
    } 
    catch (NoSuchPaddingException e) { 
     System.out.println("No Such Padding:" + e.getMessage()); 
     return; 
    } 
    catch (InvalidKeyException e) { 
     System.out.println("Invalid Key:" + e.getMessage()); 
     return; 
    } 

} 

public static void encrypt(InputStream is, OutputStream os) { 

    try { 

     call(); 

     byte[] buf = new byte[1024]; 

     // bytes at this stream are first encoded 
     os = new CipherOutputStream(os, ecipher); 

     // read in the clear text and write to out to encrypt 
     int numRead = 0; 
     while ((numRead = is.read(buf)) >= 0) { 
      os.write(buf, 0, numRead); 
     } 

     // close all streams 
     os.close(); 

    } 
    catch (IOException e) { 
     System.out.println("I/O Error:" + e.getMessage()); 
    } 

} 

public static void decrypt(InputStream is, OutputStream os) { 

    try { 

     call(); 

     byte[] buf = new byte[1024]; 

     // bytes read from stream will be decrypted 
     CipherInputStream cis = new CipherInputStream(is, dcipher); 

     // read in the decrypted bytes and write the clear text to out 
     int numRead = 0; 
     while ((numRead = cis.read(buf)) > 0) { 
      os.write(buf, 0, numRead); 
     } 

     // close all streams 
     cis.close(); 
     is.close(); 
     os.close(); 

    } 
    catch (IOException e) { 
     System.out.println("I/O Error:" + e.getMessage()); 
    } 

} 

} 

這是類我正在使用:

+0

如果我們無法看到您的代碼,我們將如何知道您做錯了什麼? – waqaslam 2012-04-19 05:47:08

+0

將解密的文件大小與原始文件的大小進行比較。並顯示您的實際代碼。順便說一句,你可能不應該使用DES,現在很容易暴力破解。 – 2012-04-19 05:50:48

+0

尺寸相同,以orignal壓縮文件 – Navdroid 2012-04-19 05:56:31

回答

1

的問題是,你使用不同的密鑰來加密和解密文件:

1)你叫從某處encrypt(..)EncryptDecryptFileStreamWithDES,這反過來調用您call()方法,它初始化新的密鑰外:

SecretKey key = KeyGenerator.getInstance("DES").generateKey(); 

2)然後您撥打decrypt(..),再次調用您的call()方法,您將獲得新的SecretKey

在您使用的示例中沒有這樣的問題,這些方法調用存在相反的順序。 爲了擴展這個例子中,你需要持有的encrypt(..)decrypt(..)調用之間的鍵,然後與存儲的關鍵初始化SecretKeySpec

byte[] keyBytes = KeyGenerator.getInstance("AES").getEncoded(); 

... 

SecretKeySpec skeySpec = new SecretKeySpec(keyBytes, "AES"); 
Cipher cipher = Cipher.getInstance("AES"); 
cipher.init(Cipher.DECRYPT_MODE, skeySpec); 

Here是多一點點真實的例子。

P.S.如前所述,使用DES算法不是最好的主意,請使用AES代替。

+0

Got Ur Point你能幫我這個:http://stackoverflow.com/questions/10228980/encryption-and-decryption-of-a-zip-file-in-android – Navdroid 2012-04-19 13:29:13