2013-03-23 44 views
0

我试图读取我的加密文件,并将其解密内容放入列表中,但朝向末尾的一些行随机或半途分裂到新行。任何想法为什么这样做? (在解密方法中)。顺便说一句,缓冲区是1024如果有帮助。将文件解密为列表时分割的行

public Crypto() { 
    try { 
     PBEKeySpec keySpec = new PBEKeySpec(password.toCharArray()); 
     SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES"); 
     SecretKey key = keyFactory.generateSecret(keySpec); 
     ecipher = Cipher.getInstance("PBEWithMD5AndDES"); 
     dcipher = Cipher.getInstance("PBEWithMD5AndDES"); 
     byte[] salt = new byte[8]; 
     PBEParameterSpec paramSpec = new PBEParameterSpec(salt, 100); 
     ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec); 
     dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec); 
     } catch (Exception e) { 
    } 
} 

@SuppressWarnings("resource") 
public static List<String> decrypt(String file) { 
    List<String> list = new LinkedList<String>(); 
    try { 
     InputStream in = new CipherInputStream(new FileInputStream(file), dcipher); 
     int numRead = 0; 
     while ((numRead = in.read(buffer)) >= 0) { 
      list.add(new String(buffer, 0, numRead); 
     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    return list; 
} 
+0

的这不能是你的实际代码。 'list.add(新的String(缓冲,0,numRead);'声明似乎错过一个右括号 – 2013-03-23 01:15:36

+0

您正在阅读的字符串1024个字节长成'List' - 如果你想读的行你行。将需要使用'BufferedReader'和'的readLine()'方法,所以'新的BufferedReader(新的InputStreamReader(新CipherInputStream(新的FileInputStream(文件),dcipher)))'应该做的伎俩。 – 2013-03-23 01:19:14

+0

太谢谢你了!!!它工作xD。最后我需要什么。 – CBennett 2013-03-23 02:00:17

回答

1

Streamread方法只是读取数目的Byte s转换为缓冲器。因此,您的代码以大小为1024的块读取文件,并将每个块读入List

有几种方法来读取Stream一行行,我会建议BufferedReader

final List<String> list = new LinkedList<String>(); 
try { 
    final BufferedReader reader = new BufferedReader(new InputStreamReader(new CipherInputStream(new FileInputStream(file), dcipher))); 
    String line; 
    while ((line = reader.readLine()) != null) { 
     list.add(line); 
    } 
} catch (IOException e) { 
    e.printStackTrace(); 
} 

有一点要注意的是已经抓住了我了多次就是InputStreamReader确实从Byte的隐式转换到String - 这需要编码。默认情况下会使用平台编码,这意味着您的代码是平台相关的。这同样适用于你的原始代码new String(byte[])也使用该平台的编码是默认的。

我会建议总是指定编码明确:

final BufferedReader reader = new BufferedReader(new InputStreamReader(new CipherInputStream(new FileInputStream(file), dcipher), "UTF-8")); 

或为String构造

new String(bytes, "UTF-8") 

这同样适用于一个写入StringFile任何代码:

try { 
    try (final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new CipherOutputStream(new FileOutputStream(file), dcipher), "UTF-8"))) { 
     writer.append(data); 
    } 
} catch (IOException ex) { 
    e.printStackTrace(); 
} 

这样在不同的操作系统上运行的应用程序时,你避免讨厌的惊喜,因为每个家庭(在Mac UTF-8在Linux上,ISO-8859-1 Windows和MacRoman)使用不同的默认编码。

另一个值得注意的是,你不要关闭您的Stream(或Reader现在) - 这是必要的,它可以在一个finally进行Java 6中,或使用新的try-with-resources构造Java 7中

try (final BufferedReader reader = new BufferedReader(new InputStreamReader(new CipherInputStream(new FileInputStream(file), dcipher), "UTF-8"))) { 
    String line; 
    while ((line = reader.readLine()) != null) { 
     list.add(line); 
    } 
}