2013-12-08 70 views
-1

如何使用充氣城堡(DESEngine)加密和解密文件(不是字符串)? 我已經搜索過,但找不到幫助。如何在NetBeans Java中使用充氣城堡(DES)加密和解密文件?

+2

你應該告訴我們你真正有嘗試過。你怎麼知道這個引擎是你需要的?你想要完成什麼? –

+1

爲什麼使用DES而不是AES等安全密碼? – ntoskrnl

+0

sory我自己解決了它。我會很快發佈我的答案。 我想嘗試3DES,但在DES的每個部分都進行了一些修改 – user3078505

回答

6

對不起,我自己解決了這個問題。這裏是我的代碼:

Tesbouncy.java

package tesbouncy; 


import org.bouncycastle.crypto.*; 
import org.bouncycastle.crypto.engines.*; 
import org.bouncycastle.crypto.modes.*; 
import org.bouncycastle.crypto.paddings.*; 
import org.bouncycastle.crypto.params.*; 

public class Tesbouncy { 

    BlockCipher engine = new DESEngine(); 

    public byte[] Encrypt(String keys, byte[] plainText) { 
     byte[] key = keys.getBytes(); 
     byte[] ptBytes = plainText; 
     BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(engine)); 
     cipher.init(true, new KeyParameter(key)); 
     byte[] rv = new byte[cipher.getOutputSize(ptBytes.length)]; 
     int tam = cipher.processBytes(ptBytes, 0, ptBytes.length, rv, 0); 
     try { 
      cipher.doFinal(rv, tam); 
     } catch (Exception ce) { 
      ce.printStackTrace(); 
     } 
     return rv; 
    } 

    public byte[] Decrypt(String key2, byte[] cipherText) { 
     byte[] key = key2.getBytes(); 
     BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(engine)); 
     cipher.init(false, new KeyParameter(key)); 
     byte[] rv = new byte[cipher.getOutputSize(cipherText.length)]; 
     int tam = cipher.processBytes(cipherText, 0, cipherText.length, rv, 0); 
     try { 
      cipher.doFinal(rv, tam); 
     } catch (Exception ce) { 
      ce.printStackTrace(); 
     } 
     return rv; 
    } 

} 

Main.java

package tesbouncy; 


    import java.io.File; 
    import java.io.FileInputStream; 
    import java.io.FileNotFoundException; 
    import java.io.FileOutputStream; 
    import java.io.IOException; 
    import javax.crypto.Cipher; 
    import javax.crypto.CipherOutputStream; 
    import javax.crypto.spec.SecretKeySpec; 



    public class Main { 
/** 
* @param args the command line arguments 
*/ 



public static void main(String[] args) throws Exception { 

    File file = new File("sql/design.jpg");  
      File file2 = new File("sql/design2.jpg"); 
    try { 
     FileInputStream imageInFile = new FileInputStream(file); 
     byte imageData[] = new byte[(int)file.length()]; 
     imageInFile.read(imageData); 

        Tesbouncy esource = new Tesbouncy(); 
    String key = "12345678"; 
    byte[] enc = esource.Encrypt(key, imageData); 
     byte[] imageByteArray = enc; 
     FileOutputStream imageOutFile = new FileOutputStream("sql/design2.jpg"); 
     imageOutFile.write(imageByteArray); 

     imageInFile.close(); 
     imageOutFile.close(); 



        FileInputStream imageInFile2 = new FileInputStream(file2); 
     byte imageData2[] = new byte[(int)file2.length()]; 
     imageInFile2.read(imageData2); 





        Tesbouncy esource2 = new Tesbouncy(); 
    String key2 = "12345678"; 
    byte[] cad2 = imageData2; 
    byte[] keyb2 = key2.getBytes(); 

    byte[] des2 = esource2.Decrypt(key2, cad2); 
    byte[] imageByteArray2 = des2; 
     FileOutputStream imageOutFile2 = new FileOutputStream("sql/design3.jpg"); 
     imageOutFile2.write(imageByteArray2); 

     imageInFile2.close(); 
     imageOutFile2.close(); 

        System.out.println("Image Successfully restore!"); 

    } catch (FileNotFoundException e) { 
     System.out.println("Image not found" + e); 



    } catch (IOException ioe) { 
     System.out.println("Exception while reading the Image " + ioe); 
    } 

} 




}