-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileTest001.java
More file actions
154 lines (148 loc) · 4.6 KB
/
FileTest001.java
File metadata and controls
154 lines (148 loc) · 4.6 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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package com.tigerwolf.encryption;
import static java.lang.System.*;
import java.io.*;
import java.util.InputMismatchException;
import java.util.Scanner;
import java.util.ArrayList;
class FileTest
{
public static void main(String[] args)
{
Scanner fix=new Scanner(in);
String input="";
while(true)
{
out.print("Enter file name:: ");
input=fix.nextLine();
try
{
Bit[] key1,key2,key3; //declares keys
out.print("Enter new file name:: ");
String newFile=fix.nextLine();
out.print("Enter key1:: ");
key1=Bit.bitValue(fix.next().charAt(0));
out.print("Enter key2:: ");
key2=Bit.bitValue(fix.next().charAt(0));
out.print("Enter key3:: ");
key3=Bit.bitValue(fix.next().charAt(0));
//encipher(data,input,key1,key2,key3); //creates file
decipher(encipher(input,newFile,key1,key2,key3));
}
catch(NumberFormatException e)
{
out.println("Incorrect number format");
}
catch(IOException e)
{
out.println("File:: "+input+" not found.");
}
finally
{
out.print("Continue? (Y/N) ");
fix.nextLine();
try
{
char x = fix.nextLine().charAt(0);
if(x!='Y'&&x!='y')
return;
out.println();
}
catch(StringIndexOutOfBoundsException e)
{
return;
}
}
}
}
public static File encipher(String inputFile,String newFile,Bit[] key1,Bit[] key2,Bit[] key3) throws IOException
{
ArrayList<Bit[]> buff=new ArrayList<>();
File output = new File(newFile); //input == name of file
//Original algorithm: (Input XOR key1) AND (key2 XOR key3)
//New algorithm:: rotateRightNoCarry(input);XOR(input,key1);rotateLeftNoCarry(input);
// XOR(input,key2);AND(input,key3);rotateRightNoCarry(input);
// rotateRightNoCarry(input);
try(BufferedOutputStream outputWriter = new BufferedOutputStream(new FileOutputStream(output));BufferedInputStream data = new BufferedInputStream(new FileInputStream(inputFile)))
{
while(true)
{
int tester = data.read();
if(tester == -1)
break;
buff.add(Bit.bitValue(tester));
}
for(int x=0;x<buff.size();x++) //encrypts input
{
Bit[] bitArray = buff.get(x);
Bit.rotateRightNoCarry(bitArray);
bitArray = Bit.XOR(bitArray,key1);
Bit.rotateLeftNoCarry(bitArray);
bitArray = Bit.XOR(bitArray,key2);
bitArray = Bit.AND(bitArray,key3);
Bit.rotateRightNoCarry(bitArray);
Bit.rotateRightNoCarry(bitArray);
buff.set(x,bitArray);
}
Bit.rotateRightNoCarry(key2); //rotates key2 one position right
outputWriter.write(Bit.longValue(key2)); //writes hidden key2 to output
outputWriter.write(Bit.longValue(Bit.AND(Bit.XOR(key1,key2),key3))); //writes hidden key1 to output
Bit.rotateRightNoCarry(key3); //rotates key3 one position right
key3 = Bit.XOR(key2,key3); //creates dependency on key2 for decryption
Bit.rotateRightNoCarry(key3); //rotates hidden key3 one position right
outputWriter.write(Bit.longValue(key3)); //writes hidden key3 to output
for(Bit[] x : buff) //writes to file
{
outputWriter.write(Bit.charValue(x));
}
return output;
}
catch(FileNotFoundException e)
{
return null;
}
}
public static File decipher(File input)
{
File output=new File(input.getPath()+input.getName().substring(0,input.getName().lastIndexOf(".")+1)+"Decrypted"+input.getName().substring(input.getName().lastIndexOf(File.pathSeparator.indexOf("."))));
ArrayList<Bit[]> buff=new ArrayList<>();
try(BufferedInputStream outputTester=new BufferedInputStream(new FileInputStream(input));BufferedOutputStream outputWriter=new BufferedOutputStream(new FileOutputStream(output)))
{
Bit[] key3 = Bit.bitValue((long)outputTester.read());
Bit.rotateLeftNoCarry(key2);
Bit[] key2 = Bit.bitValue((long)outputTester.read());
Bit[] key1 = Bit.bitValue((long)outputTester.read());
Bit.rotateLeftNoCarry(key3);
key3 = Bit.XOR(key2,key3);
Bit.rotateLeftNoCarry(key3);
key1 = Bit.special(Bit.XOR(key1,key2),key3);
while(true)
{
try
{
buff.add(Bit.bitValue(outputTester.read()));
}
catch(EOFException e)
{
break;
}
}
for(int x=0;x<buff.size();x++)
{
Bit[] bitArray = buff.get(x);
Bit.rotateLeftNoCarry(bitArray);
Bit.rotateLeftNoCarry(bitArray);
bitArray = Bit.special(bitArray,key3);
bitArray = Bit.XOR(bitArray,key2);
Bit.rotateRightNoCarry(bitArray);
bitArray = Bit.XOR(bitArray,key1);
Bit.rotateLeftNoCarry(bitArray);
outputWriter.writeChar(Bit.charValue(bitArray));
}
}
catch(IOException e)
{
return output;
}
return output;
}
}