Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.javarush.test.level18.lesson10.bonus01;
- /* Шифровка
- Придумать механизм шифровки/дешифровки
- Программа запускается с одним из следующих наборов параметров:
- -e fileName fileOutputName
- -d fileName fileOutputName
- где
- fileName - имя файла, который необходимо зашифровать/расшифровать
- fileOutputName - имя файла, куда необходимо записать результат шифрования/дешифрования
- -e - ключ указывает, что необходимо зашифровать данные
- -d - ключ указывает, что необходимо расшифровать данные
- */
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- public class Solution {
- public static void main(String[] args) throws IOException {
- if (args[0].equals("-e")) {
- encodeFile (args[1], args[2]);
- }
- else if (args[0].equals("-d")) {
- decodeFile(args[1], args[2]);
- }
- }
- public static void encodeFile (String toCodeOn, String resultFile) throws IOException {
- FileOutputStream toFile = null;
- FileInputStream fileInputStream = null;
- try {
- toFile=new FileOutputStream(resultFile);
- byte[]bytes = new byte[1024];
- fileInputStream = new FileInputStream(toCodeOn);
- while (fileInputStream.available()>0)
- {
- int count = fileInputStream.read(bytes);
- bytes = reverseByte(bytes);
- toFile.write(bytes, 0, count);
- }
- }
- catch (Exception e) {
- }
- finally
- {
- fileInputStream.close();
- toFile.close();
- }
- }
- public static void decodeFile (String toCodeOn, String resultFile) throws IOException
- {
- FileOutputStream toFile = null;
- FileInputStream fileInputStream = null;
- try {
- toFile=new FileOutputStream(resultFile);
- byte[]bytes = new byte[1024];
- fileInputStream = new FileInputStream(toCodeOn);
- while (fileInputStream.available()>0)
- {
- int count = fileInputStream.read(bytes);
- bytes = straightByte(bytes);
- toFile.write(bytes, 0, count);
- }
- }
- catch (Exception e) {
- }
- finally
- {
- fileInputStream.close();
- toFile.close();
- }
- }
- public static byte [] reverseByte (byte [] someArray) {
- int temp;
- byte [] resultArray = new byte[someArray.length];
- for (int i = 0; i < someArray.length; i++) {
- temp = someArray[i]+1;
- resultArray[i] = (byte)temp;
- }
- return resultArray;
- }
- public static byte [] straightByte (byte [] someArray) {
- int temp;
- byte [] resultArray = new byte[someArray.length];
- for (int i = 0; i < someArray.length; i++) {
- temp = someArray[i]-1;
- resultArray[i] = (byte)temp;
- }
- return resultArray; }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement