使用 FileReader 加密文件
Encrypt file with FileReader
在我的 java 程序中,我想读取一个 .txt 文件,然后对其进行编码。我知道如何读取文件并尝试学习如何对数组进行编码。我遇到的问题是我不知道如何组合它,它没有像我尝试的那样工作。
这是我可以在我的文本文件中阅读的部分:
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");
}
}
}
我想读入文本文件并将其放入字符串中进行编码,但我认为这太复杂了。是否有另一种连接方式,或者是否需要另一种编码方式?
我强烈建议您使用 Stream
s(参见 https://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html & https://docs.oracle.com/javase/7/docs/api/java/io/OutputStream.html)而不是直接使用 FileReader
。
加密发生在比您尝试做的更低的级别(字节)。
Java 密码提供方便的 CipherInputStream
(和 CipherOutputStream
)来动态加密字节流。与尝试将整个文件转储到单个 byte[]
相比,它更便宜且更具可扩展性(更重要的是因为您正在对文件内容进行解码和重新编码)。
如果您想要使用示例,请查看以下代码片段:
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)
包装 outputStream
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);
在我的 java 程序中,我想读取一个 .txt 文件,然后对其进行编码。我知道如何读取文件并尝试学习如何对数组进行编码。我遇到的问题是我不知道如何组合它,它没有像我尝试的那样工作。
这是我可以在我的文本文件中阅读的部分:
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");
}
}
}
我想读入文本文件并将其放入字符串中进行编码,但我认为这太复杂了。是否有另一种连接方式,或者是否需要另一种编码方式?
我强烈建议您使用 Stream
s(参见 https://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html & https://docs.oracle.com/javase/7/docs/api/java/io/OutputStream.html)而不是直接使用 FileReader
。
加密发生在比您尝试做的更低的级别(字节)。
Java 密码提供方便的 CipherInputStream
(和 CipherOutputStream
)来动态加密字节流。与尝试将整个文件转储到单个 byte[]
相比,它更便宜且更具可扩展性(更重要的是因为您正在对文件内容进行解码和重新编码)。
如果您想要使用示例,请查看以下代码片段:
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
是错误的(旧实用程序)类,因为它们使用当前平台编码,并且在一台计算机上加密的文件(希腊文 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);