Advertisement
jaVer404

level18.lesson10.bonus01 (preBeta)

Jan 19th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.43 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.     public static void main(String[] args) throws IOException{
  22.        if (/*args[0].equals("e")*/true) {
  23.             //encodeFile("c:\\1.txt","c:\\temp.txt");
  24.             deCodeFile("c:\\temp.txt","c:\\2.txt");
  25.         }
  26.        
  27.  
  28.     }
  29.  
  30.  
  31.    //Кодируем
  32.    
  33.     //byte[]array (элементы в обратном порядке)*
  34.        public static byte [] encodeArray (byte[]byteArray) {
  35.         byte[]codedbytes = new byte[byteArray.length];
  36.         byte tempbyte;
  37.         for (int i = byteArray.length-1;i>=0;i--) {
  38.             tempbyte = byteArray[(byteArray.length-1)-i];
  39.             codedbytes[i] = tempbyte;
  40.         }
  41.         return codedbytes;
  42.     }
  43.  
  44.     public static byte [] dCodeArray (byte[]byteArray) {
  45.         byte[]codedbytes = new byte[byteArray.length];
  46.         byte tempbyte;
  47.         for (int i = 0;i<byteArray.length;i++) {
  48.             tempbyte = byteArray[(byteArray.length-1)-i];
  49.             codedbytes[i] = tempbyte;
  50.         }
  51.         return codedbytes;
  52.     }
  53.  
  54.     public static void encodeFile (String toCodeName, String writeToName) throws IOException{
  55.         FileOutputStream toFile = null;
  56.         FileInputStream fileInputStream = null;
  57.         try {
  58.             toFile=new FileOutputStream(writeToName);
  59.             byte[]bytes = new byte[1024];
  60.             fileInputStream = new FileInputStream(toCodeName);
  61.             while (fileInputStream.available()>0)
  62.              {
  63.                  int count = fileInputStream.read(bytes);
  64.                  bytes=encodeArray(bytes);
  65.                  toFile.write(bytes, 0, count);
  66.                 }
  67.             }
  68.         catch (Exception e) {
  69.  
  70.         }
  71.         finally
  72.         {
  73.             toFile.close();
  74.             fileInputStream.close();
  75.         }
  76.     }
  77.  
  78.     public static void deCodeFile (String toCodeName, String writeToName) throws IOException{
  79.         FileOutputStream toFile = null;
  80.         FileInputStream fileInputStream = null;
  81.         try {
  82.             toFile=new FileOutputStream(writeToName);
  83.             byte[]bytes = new byte[1024];
  84.             fileInputStream = new FileInputStream(toCodeName);
  85.             while (fileInputStream.available()>0)
  86.              {
  87.                  int count = fileInputStream.read(bytes);
  88.                  bytes = dCodeArray(bytes);
  89.                  toFile.write(bytes, 0, count);
  90.                 }
  91.             }
  92.         catch (Exception e) {
  93.  
  94.         }
  95.         finally
  96.         {
  97.             toFile.close();
  98.             fileInputStream.close();
  99.         }
  100.     }
  101.  
  102.  
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement