Java 使用FileReader加密文件
在我的java程序中,我想读入一个.txt文件,然后对它进行编码。我知道如何在中读取文件,并尝试学习如何对数组进行编码。我的问题是,我不知道如何组合它,它不能像我尝试的那样工作 以下是我可以在文本文件中阅读的部分:Java 使用FileReader加密文件,java,encryption,filereader,Java,Encryption,Filereader,在我的java程序中,我想读入一个.txt文件,然后对它进行编码。我知道如何在中读取文件,并尝试学习如何对数组进行编码。我的问题是,我不知道如何组合它,它不能像我尝试的那样工作 以下是我可以在文本文件中阅读的部分: public class ReadFile { public static void main(String[] args) throws IOException { FileReader fr = new FileReader("test.tx
public class ReadFile {
public static void main(String[] args) throws IOException
{
FileReader fr = new FileReader("test.txt");
BufferedReader br = new BufferedReader(fr);
String zeile = "";
do
{
zeile = br.readLine();
System.out.println(zeile);
}
while (zeile != null);
br.close();
}
}
在这一部分中,我可以加密和解密字节:
public class Crypt {
public static void main(String[] args) {
try{
KeyGenerator keygenerator = KeyGenerator.getInstance("DES");
SecretKey myDesKey = keygenerator.generateKey();
Cipher desalgCipher;
desalgCipher = Cipher.getInstance("DES");
byte[] text = "test".getBytes("UTF8");
desalgCipher.init(Cipher.ENCRYPT_MODE, myDesKey);
byte[] textEncrypted = desalgCipher.doFinal(text);
String s = new String(textEncrypted);
System.out.println(s);
desalgCipher.init(Cipher.DECRYPT_MODE, myDesKey);
byte[] textDecrypted = desalgCipher.doFinal(textEncrypted);
s = new String(textDecrypted);
System.out.println(s);
}
catch(Exception e)
{
System.out.println("Error");
}
}
}
我想读入文本文件并将其放入字符串中进行编码,但我认为它太复杂了。是否有其他方法连接它们,或者需要另一种编码方法?我强烈建议您使用
流
s(请参阅&),而不是直接使用文件读取器
加密的级别(字节数)低于您尝试执行的级别。
Java密码提供方便的CipherInputStream
(和CipherOutputStream
)来动态加密字节流。它比试图在单个字节[]
中转储整个文件要便宜得多,而且更具可扩展性(因为您正在解码和重新编码文件内容)
如果您想要一个使用示例,请查看以下代码段:
public static void encrypt(Path inputFile, OutputStream output) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IOException {
// init cipher
KeyGenerator keygenerator = KeyGenerator.getInstance("DES");
SecretKey myDesKey = keygenerator.generateKey();
Cipher desalgCipher;
desalgCipher = Cipher.getInstance("DES");
desalgCipher.init(Cipher.ENCRYPT_MODE, myDesKey);
try(InputStream is = Files.newInputStream(inputFile); // get an IS on your file
CipherInputStream cipherIS = new CipherInputStream(is, desalgCipher)){ // wraps input Stream with cipher
copyStreams(cipherIS, output); // copyStream is let to the implementer's choice.
}
}
我会让你知道如何解密
编辑:
通信加密字节而不担心编码问题的一种常见方法是使用base 64对原始字节进行编码
您可以使用Base64.getEncoder()包装输出流。包装(os)
我强烈建议您使用流
s(请参见&),而不是直接使用文件读取器
加密的级别(字节数)低于您尝试执行的级别。
Java密码提供方便的CipherInputStream
(和CipherOutputStream
)来动态加密字节流。它比试图在单个字节[]
中转储整个文件要便宜得多,而且更具可扩展性(因为您正在解码和重新编码文件内容)
如果您想要一个使用示例,请查看以下代码段:
public static void encrypt(Path inputFile, OutputStream output) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IOException {
// init cipher
KeyGenerator keygenerator = KeyGenerator.getInstance("DES");
SecretKey myDesKey = keygenerator.generateKey();
Cipher desalgCipher;
desalgCipher = Cipher.getInstance("DES");
desalgCipher.init(Cipher.ENCRYPT_MODE, myDesKey);
try(InputStream is = Files.newInputStream(inputFile); // get an IS on your file
CipherInputStream cipherIS = new CipherInputStream(is, desalgCipher)){ // wraps input Stream with cipher
copyStreams(cipherIS, output); // copyStream is let to the implementer's choice.
}
}
我会让你知道如何解密
编辑:
通信加密字节而不担心编码问题的一种常见方法是使用base 64对原始字节进行编码
您可以使用Base64.getEncoder().wrap(os)
FileReader/FileWriter
来包装输出流,因为它们使用的是当前平台编码,在一台计算机(希腊语窗口)上加密的文件在另一台计算机(Linux服务器)上无法解密
java中的文本,String
,是Unicode格式的。不能(不应该)向字符串中抛出任意字节
因此,无法做到以下几点
new String(textEncrypted); // Uses the default platform encoding
new String(textEncrypted, "UTF-8"); // Probably the bytes are not valid UTF-8
我们也要这样做:
Path path = Paths.get("text.txt");
byte[] content = Files.readAllBytes(path);
content = encrypt(content);
Files.write(path, content);
FileReader/FileWriter
是错误的(旧的实用程序)类,因为它们使用当前的平台编码,并且在一台计算机(希腊Windows)上加密的文件在另一台计算机(Linux服务器)上无法解密
java中的文本,String
,是Unicode格式的。不能(不应该)向字符串中抛出任意字节
因此,无法做到以下几点
new String(textEncrypted); // Uses the default platform encoding
new String(textEncrypted, "UTF-8"); // Probably the bytes are not valid UTF-8
我们也要这样做:
Path path = Paths.get("text.txt");
byte[] content = Files.readAllBytes(path);
content = encrypt(content);
Files.write(path, content);
您应该看一看流(FileInputStream
,CipherOutputStream
可能对您最有用)读取到字符串,将字符串更改为字节数组,加密字节…@JeremyGrand文件读取器有什么区别?FileInputStream不是也读入了吗?fileReader正在包装一个(文件)InputStream,以便从字节解析字符。将字节解析为字符,然后再将其转换回字节,这会产生相反的效果。您可以直接在文件上打开一个FileInputStream
,用CipherInputStream
对动态读取的每个字节进行加密,然后用读取器将其重新包装。这是一种更合适的方法。你应该看看流(FileInputStream
,CipherOutputStream
可能对你最有用)读取to字符串,将字符串更改为字节数组,加密字节…@JeremyGrand与FileReader有什么区别?FileInputStream不是也读入了吗?fileReader正在包装一个(文件)InputStream,以便从字节解析字符。将字节解析为字符,然后再将其转换回字节,这会产生相反的效果。您可以直接在文件上打开一个FileInputStream
,用CipherInputStream
对动态读取的每个字节进行加密,然后用读取器将其重新包装。这是一种更合适的方法。你能给我一个解密部分的提示吗?那么,我是将OutputStream用作编写器,还是读取编码部分?@MalteS假设您已将加密数据写入一个文件,解密代码完全相同,只是需要在解密模式下初始化密码。因此,我想我将其另存为一个新文件?或者重写它更好吗?如果您想立即解密,可以使用一对PipedOutputStream
(和pipipiedinputstream
)将加密的输出流插入解密的输入流。我知道你只是想练习一下,但对一行文件进行加密和解密实际上毫无意义。加密通常用于存储或通信。您可以将OutputStream
插入到文件或websocket/servlet,这将涵盖加密的大部分用途。我认为这不是很有意义。因此,要单独保存加密文件,我可以只写字符串行因为文件打印正确,所以将代码>复制到文件中?您能给我一个解密部分的提示吗?那么,我是将OutputStream用作编写器,还是读取编码部分?@MalteS假设您已将加密数据写入一个文件,解密代码完全相同,只是需要在解密模式下初始化密码。因此,我想我将其另存为一个新文件?或者重写它更好吗?您可以使用一对管道输出