Задание, зашифровать строку через XOR и записать в фаил а потом прочитать, выполнить дешифровку и вывести в косноль. Метод write реализовал а вот с емтодом read запутался, помогите
package Task2;
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
FileOutputStream file = new FileOutputStream("src/Task2/1.txt");
FileInputStream file1 = new FileInputStream("src/Task2/1.txt");
XorOutputStream xorOutputStream = new XorOutputStream(file);
XorInputStream xorInputStream = new XorInputStream(file1);
String text = "Ура";
xorOutputStream.write(text.getBytes());
ByteArrayOutputStream bout = new ByteArrayOutputStream();
byte[] buf = new byte[10];
int data;
while ((data = xorInputStream.read(buf)) > 0) {
bout.write(buf);
}
String string = new String(bout.toByteArray());
System.out.println(string);
}
}
class XorInputStream extends FilterInputStream {
public XorInputStream(InputStream in) {
super(in);
}
@Override
public int read(byte[] b) throws IOException {
in.read(b);
byte[] res = new byte[b.length];
byte[] bKey = "55".getBytes();
for (int i = 0; i < b.length; i++) {
res[i] = (byte) (b[i] ^ bKey[i % bKey.length]);
}
return res.length;
}
}
class XorOutputStream extends FilterOutputStream {
public XorOutputStream(OutputStream out) {
super(out);
}
@Override
public void write(byte[] b) throws IOException {
byte[] bKey = "55".getBytes();
byte[] res = new byte[b.length];
for (int i = 0; i < b.length; i++) {
res[i] = (byte) (b[i] ^ bKey[i % bKey.length]);
}
out.write(res);
}
}
Попробуйте так.
@Override
public int read(byte[] b) throws IOException {
int l = in.read(b);
byte[] bKey = "55".getBytes();
for (int i = 0; i < l; i++) {
b[i] = (byte) (b[i] ^ bKey[i % bKey.length]);
}
return l;
}
Это все равно не правильно, но в вашем случае должно сработать.
Основные этапы разработки сайта для стоматологической клиники
Продвижение своими сайтами как стратегия роста и независимости