-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncryptingAndDecryptingData.java
More file actions
76 lines (66 loc) · 2.22 KB
/
Copy pathEncryptingAndDecryptingData.java
File metadata and controls
76 lines (66 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
* Copyright (c) 2013. Alterovych Illiya
*/
package test;
import java.io.*;
import java.util.ArrayList;
public class EncryptingAndDecryptingData
{
public static void main(String[] args)
{
String fileName = args[1];
String fileOutputName = args[2];
ArrayList<Integer> dataArrayList = new ArrayList<Integer>();
BufferedInputStream inputStream = null;
BufferedOutputStream outputStream = null;
try
{
inputStream = new BufferedInputStream(new FileInputStream(fileName));
while (inputStream.available() > 0)
dataArrayList.add(inputStream.read());
if (args[0].equals("-e"))
dataArrayList = encrypt(dataArrayList);
if (args[0].equals("-d"))
dataArrayList = decrypt(dataArrayList);
outputStream = new BufferedOutputStream(new FileOutputStream(fileOutputName));
for (Integer data : dataArrayList)
outputStream.write(data);
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
if (inputStream != null)
inputStream.close();
if (outputStream != null)
outputStream.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
private static ArrayList<Integer> encrypt(ArrayList<Integer> dataArrayList)
{
ArrayList<Integer> encryptingDataArrayList = new ArrayList<Integer>(dataArrayList.size());
for (int i = dataArrayList.size()-1; i >= 0; i--)
encryptingDataArrayList.add(dataArrayList.get(i));
return encryptingDataArrayList;
}
private static ArrayList<Integer> decrypt(ArrayList<Integer> dataArrayList)
{
ArrayList<Integer> decryptingDataArrayList = new ArrayList<Integer>(dataArrayList.size());
for (int i = dataArrayList.size()-1; i >= 0; i--)
decryptingDataArrayList.add(dataArrayList.get(i));
return decryptingDataArrayList;
}
}