Advertisement
jaVer404

level18.lesson10.bonus01(done)

Jan 19th, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.28 KB | None | 0 0
  1. package com.javarush.test.level18.lesson10.bonus01;
  2.  
  3. /* Шифровка
  4. Придумать механизм шифровки/дешифровки
  5.  
  6. Программа запускается с одним из следующих наборов параметров:
  7. -e fileName fileOutputName
  8. -d fileName fileOutputName
  9. где
  10. fileName - имя файла, который необходимо зашифровать/расшифровать
  11. fileOutputName - имя файла, куда необходимо записать результат шифрования/дешифрования
  12. -e - ключ указывает, что необходимо зашифровать данные
  13. -d - ключ указывает, что необходимо расшифровать данные
  14. */
  15.  
  16. import java.io.FileInputStream;
  17. import java.io.FileOutputStream;
  18. import java.io.IOException;
  19.  
  20. public class Solution {
  21.  
  22.     public static void main(String[] args) throws IOException {
  23.         if (args[0].equals("-e")) {
  24.             encodeFile (args[1], args[2]);
  25.         }
  26.         else if (args[0].equals("-d")) {
  27.             decodeFile(args[1], args[2]);
  28.         }
  29.     }
  30.  
  31.     public static void encodeFile (String toCodeOn, String resultFile) throws IOException {
  32.         FileOutputStream toFile = null;
  33.         FileInputStream  fileInputStream = null;
  34.         try {
  35.             toFile=new FileOutputStream(resultFile);
  36.             byte[]bytes = new byte[1024];
  37.             fileInputStream = new FileInputStream(toCodeOn);
  38.             while (fileInputStream.available()>0)
  39.             {
  40.                 int count = fileInputStream.read(bytes);
  41.                 bytes = reverseByte(bytes);
  42.                 toFile.write(bytes, 0, count);
  43.             }
  44.         }
  45.         catch (Exception e) {
  46.         }
  47.         finally
  48.         {
  49.             fileInputStream.close();
  50.             toFile.close();
  51.         }
  52.  
  53.     }
  54.  
  55.     public static void decodeFile (String toCodeOn, String resultFile) throws IOException
  56.     {
  57.         FileOutputStream toFile = null;
  58.         FileInputStream fileInputStream = null;
  59.         try {
  60.             toFile=new FileOutputStream(resultFile);
  61.             byte[]bytes = new byte[1024];
  62.             fileInputStream = new FileInputStream(toCodeOn);
  63.             while (fileInputStream.available()>0)
  64.             {
  65.                 int count = fileInputStream.read(bytes);
  66.                 bytes = straightByte(bytes);
  67.                 toFile.write(bytes, 0, count);
  68.             }
  69.         }
  70.         catch (Exception e) {
  71.  
  72.         }
  73.         finally
  74.         {
  75.             fileInputStream.close();
  76.             toFile.close();
  77.         }
  78.  
  79.     }
  80.  
  81.     public static byte [] reverseByte (byte [] someArray) {
  82.         int temp;
  83.         byte [] resultArray = new byte[someArray.length];
  84.         for (int i = 0; i < someArray.length; i++) {
  85.             temp = someArray[i]+1;
  86.             resultArray[i] = (byte)temp;
  87.         }
  88.         return resultArray;
  89.     }
  90.  
  91.     public static byte [] straightByte (byte [] someArray) {
  92.         int temp;
  93.         byte [] resultArray = new byte[someArray.length];
  94.         for (int i = 0; i < someArray.length; i++) {
  95.             temp = someArray[i]-1;
  96.             resultArray[i] = (byte)temp;
  97.         }
  98.         return resultArray; }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement