Skip to content

Commit 651f5db

Browse files
committed
v1.0
1 parent e170696 commit 651f5db

3 files changed

Lines changed: 262 additions & 0 deletions

File tree

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,7 @@
2121

2222
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
2323
hs_err_pid*
24+
25+
target
26+
.idea
27+
dependency-reduced-pom.xml

pom.xml

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>net.boybacks</groupId>
8+
<artifactId>XOREncrypt</artifactId>
9+
<version>1.0</version>
10+
11+
<properties>
12+
<maven.compiler.source>16</maven.compiler.source>
13+
<maven.compiler.target>16</maven.compiler.target>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
</properties>
16+
17+
<build>
18+
<plugins>
19+
<plugin>
20+
<artifactId>maven-compiler-plugin</artifactId>
21+
<version>3.1</version>
22+
<configuration>
23+
<source>16</source>
24+
<target>16</target>
25+
</configuration>
26+
</plugin>
27+
<plugin>
28+
<groupId>org.apache.maven.plugins</groupId>
29+
<artifactId>maven-shade-plugin</artifactId>
30+
<version>2.3</version>
31+
<executions>
32+
<execution>
33+
<phase>package</phase>
34+
<goals>
35+
<goal>shade</goal>
36+
</goals>
37+
</execution>
38+
</executions>
39+
<configuration>
40+
<finalName>${project.artifactId}-${project.version}</finalName>
41+
<transformers>
42+
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
43+
<resource>META-INF/spring.handlers</resource>
44+
</transformer>
45+
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
46+
<resource>META-INF/spring.schemas</resource>
47+
</transformer>
48+
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
49+
<mainClass>net.boybacks.main.Main</mainClass>
50+
</transformer>
51+
</transformers>
52+
<filters>
53+
<filter>
54+
<artifact>*:*</artifact>
55+
<excludes>
56+
<exclude>META-INF/*.SF</exclude>
57+
<exclude>META-INF/*.DSA</exclude>
58+
<exclude>META-INF/*.RSA</exclude>
59+
</excludes>
60+
</filter>
61+
</filters>
62+
</configuration>
63+
</plugin>
64+
</plugins>
65+
</build>
66+
</project>
Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
package net.boybacks.main;
2+
3+
import java.io.*;
4+
import java.util.Properties;
5+
import java.util.Scanner;
6+
7+
public class Main {
8+
9+
static File input, encrypted, output, keySaver;
10+
11+
static Scanner in = new Scanner(System.in);
12+
13+
static String word = "test";
14+
15+
public static void main(String[] args) throws InterruptedException, IOException {
16+
input = new File("input.txt");
17+
encrypted = new File("encrypted.data");
18+
output = new File("output.txt");
19+
keySaver = new File("key.properties");
20+
if (!input.exists()) {
21+
input.createNewFile();
22+
System.out.println("[Console Log( Creating File)] Creating file \"" + input.getName() + "\"");
23+
}
24+
if (!encrypted.exists()) {
25+
encrypted.createNewFile();
26+
System.out.println("[Console Log (Creating File)] Creating file \"" + encrypted.getName() + "\"");
27+
}
28+
if (!output.exists()) {
29+
output.createNewFile();
30+
System.out.println("[Console Log (Creating File)] Creating file \"" + output.getName() + "\"");
31+
}
32+
if (keySaver.exists() && keySaver.getPath().contains("key")) {
33+
String keyFileAsker;
34+
System.out.println("[Console Log (Key Saver File)] Password file exists, do you want to use it? Y - yes | N - no (Default: Y)");
35+
keyFileAsker = in.nextLine().toUpperCase();
36+
if (!keyFileAsker.equalsIgnoreCase("Y") && !keyFileAsker.equalsIgnoreCase("N")) {
37+
keyFileAsker = "Y";
38+
}
39+
if (keyFileAsker.equalsIgnoreCase("Y")) {
40+
loadKeyWord();
41+
}
42+
else {
43+
System.out.println("[Console Log (Encrypt Word)] Enter the key you want to encrypt the file with (Default: \"test\")");
44+
word = in.nextLine();
45+
if (word.equals("")) {
46+
word = "test";
47+
}
48+
System.out.println("[Console Log (Encrypt Word)] The encrypting key is \"" + word + "\"");
49+
String keyAsker;
50+
System.out.println("[Console Log (Key Saver)] Do you want to save the encryption key? Y - yes | N - no (Default: Y)");
51+
keyAsker = in.nextLine().toUpperCase();
52+
if (!keyAsker.equalsIgnoreCase("Y") && !keyAsker.equalsIgnoreCase("N")) {
53+
keyAsker = "Y";
54+
}
55+
if (keyAsker.equalsIgnoreCase("Y")) {
56+
saveKeyWord(word);
57+
}
58+
}
59+
}
60+
else {
61+
System.out.println("[Console Log (Encrypt Word)] Enter the key you want to encrypt the file with (Domyślne \"test\")");
62+
word = in.nextLine();
63+
if (word.equals("")) {
64+
word = "test";
65+
}
66+
System.out.println("[Console Log (Encrypt Word)] The encrypting key is \"" + word + "\"");
67+
String keyAsker;
68+
System.out.println("[Console Log (Key Saver)] Do you want to save the encryption key? Y - yes | N - no (Default: Y)");
69+
keyAsker = in.nextLine().toUpperCase();
70+
if (!keyAsker.equalsIgnoreCase("Y") && !keyAsker.equalsIgnoreCase("N")) {
71+
keyAsker = "Y";
72+
}
73+
if (keyAsker.equalsIgnoreCase("Y")) {
74+
saveKeyWord(word);
75+
}
76+
}
77+
Thread.sleep(1500);
78+
int i = 0;
79+
System.out.println("[Console Log (Encrypting Method)] Enter 0 - to encrypt, 1 - to decrypt");
80+
i = in.nextByte();
81+
if (i == 0) {
82+
fileChecker(input);
83+
System.out.println("[Console Log (Encrypting File)] File encryption. This may take a while");
84+
encryptFile();
85+
System.out.println("[Console Log (Encrypting File)] The file has been encrypted \"" + encrypted.getName() + "\"");
86+
Thread.sleep(1500);
87+
System.exit(0);
88+
}
89+
else if (i == 1) {
90+
fileChecker(encrypted);
91+
System.out.println("[Console Log (Encrypting File)] File decryption. This may take a while");
92+
decryptFile();
93+
System.out.println("[Console Log (Decrytping File)] The file has been decrypted \"" + output.getName() + "\"");
94+
Thread.sleep(1500);
95+
System.exit(0);
96+
}
97+
else {
98+
System.out.printf("[Console Error (Wrong Method)] Wrong encryption method, exiting the program \n");
99+
Thread.sleep(1500);
100+
System.exit(0);
101+
}
102+
}
103+
104+
public static void encryptFile(){
105+
FileInputStream in = null;
106+
FileOutputStream out = null;
107+
try {
108+
in = new FileInputStream(input);
109+
out = new FileOutputStream(encrypted);
110+
int data = 0;
111+
while ((data=in.read())!=-1){
112+
out.write(data^word.length());
113+
}
114+
}
115+
catch (Exception e) {
116+
e.printStackTrace();
117+
}
118+
finally {
119+
if (in!=null){
120+
try {
121+
in.close();
122+
} catch (IOException e) {
123+
e.printStackTrace();
124+
}
125+
}
126+
if (out!=null){
127+
try {
128+
out.close();
129+
} catch (IOException e) {
130+
e.printStackTrace();
131+
}
132+
}
133+
}
134+
}
135+
136+
public static void decryptFile(){
137+
FileInputStream in = null;
138+
FileOutputStream out = null;
139+
try {
140+
in = new FileInputStream(encrypted);
141+
out = new FileOutputStream(output);
142+
int data = 0;
143+
while ((data=in.read())!=-1){
144+
out.write(data^word.length());
145+
}
146+
}catch (Exception e){
147+
e.printStackTrace();
148+
}finally {
149+
if (in!=null){
150+
try {
151+
in.close();
152+
} catch (IOException e) {
153+
e.printStackTrace();
154+
}
155+
}
156+
if (out!=null){
157+
try {
158+
out.close();
159+
} catch (IOException e) {
160+
e.printStackTrace();
161+
}
162+
}
163+
}
164+
}
165+
166+
public static void fileChecker(File file) {
167+
if (file.length() == 0) {
168+
System.out.printf("[Console Error (Checking File)] The file is empty. Complete it with data");
169+
System.exit(0);
170+
}
171+
}
172+
173+
static Properties properties = new Properties();
174+
175+
public static void saveKeyWord(String word) {
176+
try {
177+
properties.setProperty("key", word);
178+
properties.store(new FileOutputStream("key.properties"), null);
179+
System.out.println("[Console Log (Key Saver)] You have successfully saved the key to a file \"key.properties\"");
180+
}
181+
catch (IOException e) {
182+
e.printStackTrace();
183+
}
184+
}
185+
186+
public static void loadKeyWord() throws IOException {
187+
FileInputStream inputStream = new FileInputStream(keySaver);
188+
properties.load(inputStream);
189+
word = String.valueOf(properties.get("key"));
190+
System.out.println("[Console Log (Key Saver)] You have successfully saved the key to a file \"key.properties\"");
191+
}
192+
}

0 commit comments

Comments
 (0)