-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEncrypt.java~
139 lines (112 loc) · 4.46 KB
/
Encrypt.java~
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import java.io.*;
import java.util.ArrayList;
/**
*
* @author Brett
*/
public class Encrypt {
static File inFile;
static File key;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
String plaintext = args[0];
String keyFile = args[1];
inFile = new File(plaintext);
key = new File(keyFile);
char[] charArray;
byte[] asciiArray;
ArrayList<String> binaryList = new ArrayList<String>();
String binBlock = "";
long numBlock = 0;
long n = 0;
long e = 0;
long d = 0;
try
{
BufferedReader in = new BufferedReader(new FileReader(key));
n = Long.valueOf(in.readLine()).longValue();
e = Long.valueOf(in.readLine()).longValue();
d = Long.valueOf(in.readLine()).longValue();
in.close();
} catch (FileNotFoundException ex) {
System.err.println("FileNotFoundException: " + ex.getMessage());
} catch (IOException ex) {
System.err.println("IOException: " + ex.getMessage());
}
String binaryE = Long.toBinaryString(e);
String padding = "00000000";
//Read file (getArray method from io)
char[] ret = new char[(int) (inFile.length()+1)];
int retCursor = 0;
try
{
FileInputStream from = new FileInputStream(inFile);
while (true)
{
byte nextByte = (byte) from.read();
if ( nextByte == -1) break ;
char nextChar = (char) nextByte;
ret[retCursor++] = nextChar;
}
ret[(int)(inFile.length())] = '\000';
} catch (FileNotFoundException ex) {
System.err.println("FileNotFoundException: " + ex.getMessage());
} catch (IOException ex) {
System.err.println("IOException: " + ex.getMessage());
}
charArray = ret;
//
asciiArray = new byte[charArray.length-1];
for(int i = 0; i< asciiArray.length; i++)
asciiArray[i] = (byte)charArray[i];
for(int i : asciiArray)
{
String temp = padding + ((Integer)i).toBinaryString(i);
temp = temp.substring(temp.length() - 8, temp.length());
binaryList.add(temp);
}
while(binaryList.size() % 3 != 0)
binaryList.add(padding);
OutputStream out = null;
try
{
out = new BufferedOutputStream(new FileOutputStream("encrypted"));
for(int i = 0; i < binaryList.size(); i+=3)
{
for(int j = 0; j < 3; j++)
binBlock += binaryList.get(i + j);
binBlock = padding + binBlock;
numBlock = Long.parseLong(binBlock, 2);
//Encryption
long c = 1;
for(int k = 0; k < binaryE.length(); k++)
{
if(binaryE.charAt(k) == '0')
c = ((c*c) % n);
else
c = ((((c*c) % n)*numBlock) % n);
}
String encrypted = Long.toBinaryString(c);
System.out.println(encrypted.length());
String temp = padding + encrypted;
if(temp.length() >= 32)
encrypted = temp.substring(temp.length() - 32);
else
encrypted = temp.substring(0);
binBlock = "";
out.write((byte)((c >> 24) & 0xFF));
out.write((byte)((c >> 16) & 0xFF));
out.write((byte)((c >> 8) & 0xFF));
out.write((byte)(c & 0xFF));
}
out.close();
}
catch(Exception ex)
{
System.err.println("Error:" + ex.getMessage());
}
}
}