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