2012-08-13 141 views
0

我想使用AES加密來加密文本文件。
但是,我不太確定如何結合Aes代碼和文件閱讀代碼。我對這種加密方法很陌生。任何幫助表示讚賞。使用AES加密來加密文件

我試過這樣做。加密時出現錯誤,並且它不適用於參數。或者我應該以另一種方式做到這一點?

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

    FileReader file = new FileReader ("original.txt"); 
    BufferedReader reader = new BufferedReader(file); 

    String text = ""; 
    String line = reader.readLine(); 
    while(line !=null) 
    { 
     text +=line; 
     line = reader.readLine(); 
    } 


    String test = Testing.encrypt(text); 
    System.out.println("Encrypted : " + test); 
    reader.close(); 
    } 

完整的代碼在下面。非常感謝你。

(AES加密)

package encypt.com; 

import java.io.BufferedReader; 
import java.io.FileReader; 
import java.security.*; 
import java.security.spec.InvalidKeySpecException; 
import javax.crypto.Cipher; 
import javax.crypto.spec.SecretKeySpec; 
import sun.misc.*; 

public class Testing { 

private static final String ALGORITHM = "AES"; 
private static final int ITERATIONS = 2; 
private static final byte[] keyValue = 
    new byte[] { 'T', 'h', 'i', 's', 'I', 's', 'A', 'S', 'e', 'c', 'r', 'e', 't', 'K', 'e', 'y'}; 

public static String encrypt(String value, String salt) throws Exception { 
    Key key = generateKey(); 
    Cipher c = Cipher.getInstance(ALGORITHM); 
    c.init(Cipher.ENCRYPT_MODE, key); 

    String valueToEnc = null; 
    String eValue = value; 
    for (int i = 0; i < ITERATIONS; i++) { 
     valueToEnc = salt + eValue; 
     byte[] encValue = c.doFinal(valueToEnc.getBytes()); 
     eValue = new BASE64Encoder().encode(encValue); 
    } 
    return eValue; 
} 

public static String decrypt(String value, String salt) throws Exception { 
    Key key = generateKey(); 
    Cipher c = Cipher.getInstance(ALGORITHM); 
    c.init(Cipher.DECRYPT_MODE, key); 

    String dValue = null; 
    String valueToDecrypt = value; 
    for (int i = 0; i < ITERATIONS; i++) { 
     byte[] decordedValue = new BASE64Decoder().decodeBuffer(valueToDecrypt); 
     byte[] decValue = c.doFinal(decordedValue); 
     dValue = new String(decValue).substring(salt.length()); 
     valueToDecrypt = dValue; 
    } 
    return dValue; 
} 

private static Key generateKey() throws Exception { 
    Key key = new SecretKeySpec(keyValue, ALGORITHM); 
    // SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM); 
    // key = keyFactory.generateSecret(new DESKeySpec(keyValue)); 
    return key; 
} 


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


    String password = "mypassword"; 
    String salt = "this is a simple clear salt"; 
    String passwordEnc = Testing.encrypt(password, salt); 
    String passwordDec = Testing.decrypt(passwordEnc, salt); 

    System.out.println("Salt Text : " + salt); 
    System.out.println("Plain Text : " + password); 
    System.out.println("Encrypted : " + passwordEnc); 
    System.out.println("Decrypted : " + passwordDec); 
} 
} 

(文件讀取代碼)

package encypt.com; 

import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileReader; 
import java.util.Scanner; 

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

    FileReader file = new FileReader ("key.txt"); 
    BufferedReader reader = new BufferedReader(file); 

    String text = ""; 
    String line = reader.readLine(); 
    while(line !=null) 
    { 
     text +=line; 
     line = reader.readLine(); 
    } 

    reader.close(); 
    System.out.println(text); 
    } 
} 
+4

有什麼問題,這個功能呢? – EJP 2012-08-13 10:34:22

+0

我已經改變了。 – Twister 2012-08-13 11:51:52

+0

你沒有改變任何事情,因爲你還沒有說明問題所在。 「我正面臨一些問題」......哪些問題? – m0skit0 2012-08-13 12:52:38

回答

2

我猜是因爲你試圖調用

public static String encrypt(String value, String salt) 

String test = Testing.encrypt(text); 
出現的錯誤

因此salt參數丟失。

嘗試調用用即

String test = Testing.encrypt(text,"mySalt"); 
+0

好吧,它已經可以工作了。非常感謝!! – Twister 2012-08-13 13:24:38