Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.Scanner;
- public class gammaCode {
- public static void Encode(String path) throws IOException {
- FileInputStream fileInputStream = new FileInputStream(new File(path));
- byte[] buffer = new byte[fileInputStream.available()];
- byte[] bufferRand = new byte[buffer.length];
- // считываем файл в массив байт
- fileInputStream.read(buffer, 0, buffer.length);
- // объявляем класс lfsr из прошлой лабораторной
- LFSR lfsr = new LFSR(0xC4, 20);
- // заполняем массив рандомными
- for (int i = 0; i < bufferRand.length; i++) {
- bufferRand[i] = (byte) lfsr.next(i);
- }
- // шифруем
- for (int i = 0; i < buffer.length; i++) {
- buffer[i] = (byte) (buffer[i] ^ bufferRand[i]);
- }
- // записываем обратно в файл
- FileOutputStream fileOutputStream = new FileOutputStream(new File(path));
- fileOutputStream.write(buffer, 0, buffer.length);
- }
- public static void Decode(String path) throws IOException {
- FileInputStream fileInputStream = new FileInputStream(new File(path));
- byte[] buffer = new byte[fileInputStream.available()];
- byte[] bufferRand = new byte[buffer.length];
- // считываем файл в массив байт
- fileInputStream.read(buffer, 0, buffer.length);
- // объявляем класс lfsr из прошлой лабораторной
- LFSR lfsr = new LFSR(0xC4, 20);
- // заполняем массив рандомными
- for (int i = 0; i < bufferRand.length; i++) {
- bufferRand[i] = (byte) lfsr.next(i);
- }
- // дешифруем
- for (int i = 0; i < buffer.length; i++) {
- buffer[i] = (byte) (buffer[i] ^ bufferRand[i]);
- }
- // записываем обратно в файл
- FileOutputStream fileOutputStream = new FileOutputStream(new File(path));
- fileOutputStream.write(buffer, 0, buffer.length);
- }
- public static void main(String[] args) throws IOException {
- // вводим путь к файлу
- Scanner scanner = new Scanner(System.in);
- String path = scanner.nextLine();
- // кодируем
- Encode(path);
- // декодируем
- Decode(path);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement