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")*/true) {
- //encodeFile("c:\\1.txt","c:\\temp.txt");
- deCodeFile("c:\\temp.txt","c:\\2.txt");
- }
- }
- //Кодируем
- //byte[]array (элементы в обратном порядке)*
- public static byte [] encodeArray (byte[]byteArray) {
- byte[]codedbytes = new byte[byteArray.length];
- byte tempbyte;
- for (int i = byteArray.length-1;i>=0;i--) {
- tempbyte = byteArray[(byteArray.length-1)-i];
- codedbytes[i] = tempbyte;
- }
- return codedbytes;
- }
- public static byte [] dCodeArray (byte[]byteArray) {
- byte[]codedbytes = new byte[byteArray.length];
- byte tempbyte;
- for (int i = 0;i<byteArray.length;i++) {
- tempbyte = byteArray[(byteArray.length-1)-i];
- codedbytes[i] = tempbyte;
- }
- return codedbytes;
- }
- public static void encodeFile (String toCodeName, String writeToName) throws IOException{
- FileOutputStream toFile = null;
- FileInputStream fileInputStream = null;
- try {
- toFile=new FileOutputStream(writeToName);
- byte[]bytes = new byte[1024];
- fileInputStream = new FileInputStream(toCodeName);
- while (fileInputStream.available()>0)
- {
- int count = fileInputStream.read(bytes);
- bytes=encodeArray(bytes);
- toFile.write(bytes, 0, count);
- }
- }
- catch (Exception e) {
- }
- finally
- {
- toFile.close();
- fileInputStream.close();
- }
- }
- public static void deCodeFile (String toCodeName, String writeToName) throws IOException{
- FileOutputStream toFile = null;
- FileInputStream fileInputStream = null;
- try {
- toFile=new FileOutputStream(writeToName);
- byte[]bytes = new byte[1024];
- fileInputStream = new FileInputStream(toCodeName);
- while (fileInputStream.available()>0)
- {
- int count = fileInputStream.read(bytes);
- bytes = dCodeArray(bytes);
- toFile.write(bytes, 0, count);
- }
- }
- catch (Exception e) {
- }
- finally
- {
- toFile.close();
- fileInputStream.close();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement