2012-06-30 80 views
0

我必須使用DES算法通過使用存儲在文本文件中的KEY來加密和解密zip文件。加密和解密算法都需要密鑰該文本文件做相應的功能。是否可以使用DES中的DES算法加密和解密zip文件

是否有可用做在Java中DES算法任何內置包...

請指引我走出這個問題...

+0

爲什麼不把密碼設置爲zip而不是加密和解密? –

+0

不要忘了,DES是壞的。使用(至少)三重des,或者甚至更好的是,像AES這樣的現代算法。 –

回答

3

你可以使用的東西從javax.crypto中包:

 // read the key 
    FileInputStream fis = new FileInputStream(keyFile); 
    byte[] keyBytes = new byte[fis.available()]; 
    fis.read(keyBytes); 
    SecretKeySpec spec = new SecretKeySpec(keyBytes, "DES"); 

    // encrypt 
    Cipher encCipher = Cipher.getInstance("DES"); 
    encCipher.init(Cipher.ENCRYPT_MODE, spec); 

    CipherInputStream cipherIn = new CipherInputStream(new FileInputStream(zipFile), encCipher); 
    FileChannel out = new FileOutputStream(encZipFile).getChannel(); 
    out.transferFrom(Channels.newChannel(cipherIn), 0, Long.MAX_VALUE); 

    // decrypt 
    Cipher decCipher = Cipher.getInstance("DES"); 
    decCipher.init(Cipher.DECRYPT_MODE, spec); 

    cipherIn = new CipherInputStream(new FileInputStream(encZipFile), decCipher); 
    out = new FileOutputStream(decZipFile).getChannel(); 
    out.transferFrom(Channels.newChannel(cipherIn), 0, Long.MAX_VALUE); 
0

這是可能的。更好的是,你可以用castly來替代彈性。他們提供API。