2013-03-28 149 views
0

我正在閱讀關於使用私鑰進行加密的IBM教程。我寫下如下代碼從文件中讀取加密數據

import java.security.*; 
import javax.crypto.*; 

// encrypt and decrypt using the DES private key algorithm 

public class PrivateExample { 

    public static void main (String[] args) throws Exception { 
    String text=new String(); 
    text="THIS IS AN ENCRYPTION TEST"; 
    byte[] plainText = text.getBytes("UTF8"); 

    // get a DES private key 
    System.out.println("\nStart generating DES key"); 
    KeyGenerator keyGen = KeyGenerator.getInstance("DES"); 
    keyGen.init(56); 
    Key key = keyGen.generateKey(); 
    System.out.println("Finish generating DES key"); 

    // get a DES cipher object and print the provider 
    Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); 
    System.out.println("\n" + cipher.getProvider().getInfo()); 
    // 
    // encrypt using the key and the plaintext 
    System.out.println("\nStart encryption"); 
    cipher.init(Cipher.ENCRYPT_MODE, key); 
    byte[] cipherText = cipher.doFinal(plainText); 
    System.out.println("Finish encryption: "); 
    System.out.println(new String(cipherText, "UTF8")); 

    // 
    // decrypt the ciphertext using the same key 
    System.out.println("\nStart decryption"); 
    cipher.init(Cipher.DECRYPT_MODE, key); 
    byte[] newPlainText = cipher.doFinal(cipherText); 
    System.out.println("Finish decryption: "); 

    System.out.println(new String(newPlainText, "UTF8")); 
    } 
} 

上面的代碼很好用。我能夠看到結果和一切。但我想修改它如下,以便我可以將cipherText存儲在文件中。然後另一個程序從文件中讀取加密的文本並對其進行解密。以下是我迄今爲止所做的,但我無法理解如何繼續。只有一點點提示如何進行將有所幫助。

import java.security.*; 
import javax.crypto.*; 

// encrypt and decrypt using the DES private key algorithm 
public class PrivateExample { 

    public static void main (String[] args) throws Exception { 
    String text=new String(); 
    text="This is an encryption test"; 

    byte[] plainText = text.getBytes("UTF8"); 

    // get a DES private key 
    System.out.println("\nStart generating DES key"); 
    KeyGenerator keyGen = KeyGenerator.getInstance("DES"); 
    keyGen.init(56); 
    Key key = keyGen.generateKey(); 
    System.out.println("Finish generating DES key"); 
    // 
    // get a DES cipher object and print the provider 
    Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); 
    System.out.println("\n" + cipher.getProvider().getInfo()); 
    // 
    // encrypt using the key and the plaintext 
    System.out.println("\nStart encryption"); 
    cipher.init(Cipher.ENCRYPT_MODE, key); 
    byte[] cipherText = cipher.doFinal(plainText); 
    System.out.println("Finish encryption: "); 
    System.out.println(new String(cipherText, "UTF8")); 

    //Now writing to an ouput file the cipherText 
    try{ 
     FileOutputStream fs=new FileOutputStream("c:/test.txt"); 
     fs.write(cipherText); 
    }catch(Exception e){ 
     e.printStackTrace(); 
    } 
//How to proceed from here 

} 
} 

該程序的工作已經完成。它已成功將加密的字符串寫入文件。新程序只需要解密數據。如何從文件讀取加密的字節?新程序顯然不知道原始字符串是什麼,但我將使用與算法中相同的密鑰。請幫忙!我是新來的加密

+0

所以你只需要從文件中讀取它? –

+0

將密鑰存儲在文件中並將其用於解密。 – user1516873

+2

如果你在程序結束之前沒有存儲'key',將會非常私密。 – Anthon

回答

2

這裏是你如何編寫到一個文件:

 //Write your key to an output file. 
     byte[] keyAsByte = key.getEncoded(); 
     FileOutputStream keyfos = new FileOutputStream("key.txt"); 
     keyfos.write(keyAsByte); 
     keyfos.close(); 

我不會建議把鑰匙在同一個文件中的加密文本。

這裏是你如何閱讀ecrypted文本和關鍵背部和解密:

//Read your key 
    FileInputStream keyFis = new FileInputStream("key.txt"); 
    byte[] encKey = new byte[keyFis.available()]; 
    keyFis.read(encKey); 
    keyFis.close(); 
    Key keyFromFile = new SecretKeySpec(encKey, "DES"); 
    //Read your text 
    FileInputStream encryptedTextFis = new FileInputStream("test.txt"); 
    byte[] encText = new byte[encryptedTextFis.available()]; 
    encryptedTextFis.read(encText); 
    encryptedTextFis.close(); 
    //Decrypt 
    Cipher decrypter = Cipher.getInstance("DES/ECB/PKCS5Padding"); 
    decrypter.init(Cipher.DECRYPT_MODE, keyFromFile); 
    byte[] decryptedText = decrypter.doFinal(encText); 
    //Print result 
    System.out.println("Decrypted Text: " + new String(decryptedText)); 

:我沒有使用相同的路徑,你寫的信息。

+0

非常感謝你的幫助 –

+0

還有一個疑問。讀回數值後,還可以獲得填充。有什麼方法可以輕鬆刪除填充? –

+0

在解密的文本上填充? – nattyddubbs