[英]How to read /write XORed txt file UTF8 in java?
what i did so far : 到目前为止我做了什么:
I read a file1 with text, XORed the bytes with a key and wrote it back to another file2. 我用文本读取了一个文件,用一个键对字节进行了异或,并将其写回另一个文件2。 My problem: I read for example 'H' from file1 , the byte value is 72; 我的问题:我从file1读取例如'H',字节值为72;
72 XOR -32 = -88 Now i wrote -88 in to the file2. 72 XOR -32 = -88现在我写了-88到file2。 when i read file2 i should get -88 as first byte, but i get -3. 当我读取file2时,我应该得到-88作为第一个字节,但我得到-3。
public byte[] readInput(String File) throws IOException {
Path path = Paths.get(File);
byte[] data = Files.readAllBytes(path);
byte[]x=new byte[data.length ];
FileInputStream fis = new FileInputStream(File);
InputStreamReader isr = new InputStreamReader(fis);//utf8
Reader in = new BufferedReader(isr);
int ch;
int s = 0;
while ((ch = in.read()) > -1) {// read till EOF
x[s] = (byte) (ch);
}
in.close();
return x;
}
public void writeOutput(byte encrypted [],String file) {
try {
FileOutputStream fos = new FileOutputStream(file);
Writer out = new OutputStreamWriter(fos,"UTF-8");//utf8
String s = new String(encrypted, "UTF-8");
out.write(s);
out.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
public byte[]DNcryption(byte[]key,byte[] mssg){
if(mssg.length==key.length)
{
byte[] encryptedBytes= new byte[key.length];
for(int i=0;i<key.length;i++)
{
encryptedBytes[i]=Byte.valueOf((byte)(mssg[i]^key[i]));//XOR
}
return encryptedBytes;
}
else
{
return null;
}
}
You're not reading the file as bytes - you're reading it as characters . 您不是以字节形式读取文件 - 您将其作为字符读取。 The encrypted data isn't valid UTF-8-encoded text, so you shouldn't try to read it as such. 加密数据不是有效的UTF-8编码文本,因此您不应该尝试读取它。
Likewise, you shouldn't be writing arbitrary byte arrays as if they're UTF-8-encoded text. 同样,您不应该编写任意字节数组,就好像它们是UTF-8编码的文本一样。
Basically, your methods have signatures accepting or returning arbitrary binary data - don't use Writer
or Reader
classes at all . 基本上,您的方法具有接受或返回任意二进制数据的签名 - 根本不使用Writer
或Reader
类 。 Just write the data straight to the stream. 只需将数据直接写入流。 (And don't swallow the exception, either - do you really want to continue if you've failed to write important data?) (并且不要吞下异常 - 如果你没有写出重要的数据,你真的想继续吗?)
I would actually remove both your readInput
and writeOutput
methods entirely. 我实际上完全删除了readInput
和writeOutput
方法。 Instead, use Files.readAllBytes
and Files.write
. 而是使用Files.readAllBytes
和Files.write
。
In writeOutput method you convert encrypted byte array into UTF-8 String which changes the actual bytes you are writing later to the file. 在writeOutput方法中,您将加密的字节数组转换为UTF-8字符串,这会将您稍后写入的实际字节更改为该文件。 Try this code snippet to see what is happening when you try to convert byte array with negative values to UTF-8 String: 尝试使用此代码段来查看当您尝试将具有负值的字节数组转换为UTF-8字符串时发生的情况:
final String s = new String(new byte[]{-1}, "UTF-8");
System.out.println(Arrays.toString(s.getBytes("UTF-8")));
It will print something like [-17, -65, -67]
. 它将打印类似[-17, -65, -67]
。 Try using OutputStream
to write bytes to the file. 尝试使用OutputStream
将字节写入文件。
new FileOutputStream(file).write(encrypted);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.