11package self_encryption ;
2- // import java.util.Scanner;
3- // import java.sql.*;
4- // import java.io.*;
2+ import java .util .Scanner ;
3+ import java .sql .*;
4+ import java .io .*;
55
66/**
77 *
@@ -11,42 +11,177 @@ public class Self_encryption {
1111
1212 public static void main (String [] args ) {
1313
14- // ********************************************************************
15- // VERSION 1
16- // ********************************************************************
17- String data = "The data privacy laws are the need of the hour." ;
18- String key = "hard" ;
19- KeyGenerator kGen = new KeyGenerator (key );
20- String nK =kGen .getNumericKey ();
14+ // *********************************************************************
15+ // VERSION 2( Files Compatible)
16+ // *********************************************************************
17+
18+ Scanner sc = new Scanner (System .in );
19+
20+ try
21+ {
22+ String src ="" ,enc ="" ,dec ="" ,key ="" ;
23+
24+ System .out .println ("\t Welcome to encryptor!!! \n \t --secure your files--\n =====================================\n " );
25+
26+
27+ // System.out.println("Key is = "+key);
28+ String ch ="" ;
29+ do {
30+
31+ System .out .print ("Enter file path: " );
32+ src =sc .nextLine ();
33+ System .out .print ("Enter encrypted file path: " );
34+ enc =sc .nextLine ();
35+ System .out .print ("Enter decrypted file path: " );
36+ dec =sc .nextLine ();
37+
38+ // switch(choice)
39+ // {
40+ // // for Text Files
41+ // case 1: src = "g:/sample/sampleText.txt";
42+ // enc = "g:/sample/encryptedFile.txt";
43+ // dec = "g:/sample/decryptedFile.txt";
44+ // break;
45+ //
46+ // // for Image Files
47+ // case 2: src = "g:/sample/sample_img.jpg";
48+ // enc = "g:/sample/encryptImg.jpg";
49+ // dec = "g:/sample/decrpytImg.jpg";
50+ // break;
51+ //
52+ // // for Audio Files
53+ // case 3: src = "g:/sample/sampleAudio.m4a";
54+ // enc = "g:/sample/encryptedAudio.m4a";
55+ // dec = "g:/sample/decryptedAudio.m4a";
56+ // break;
57+ //
58+ // // for Video Files
59+ // case 4: src = "g:/sample/sample.mp4";
60+ // enc = "G:/sample/encryptVid.mp4";
61+ // dec = "G:/sample/decryptVid.mp4";
62+ // break;
63+ //
64+ // default:
65+ // System.out.println("Invalid Choice");
66+ //
67+ // }
68+
69+ // =========================================================================
70+ // Establishing connection to the database
71+ String user = "root" ;
72+ String pass = "" ;
73+ String url = "jdbc:mysql://localhost:3306/test" ;
74+
75+ Connection conn ;
76+ Statement stmt ;
77+
78+ Class .forName ("com.mysql.cj.jdbc.Driver" );
79+ conn = DriverManager .getConnection (url , user ,pass );
80+ stmt = conn .createStatement ();
81+ System .out .println ("Database connection established" );
82+
83+ // ========================================================================
84+ // Encrypting file
85+ System .out .print ("Enter key to encrypt : " );
86+ key =sc .nextLine ();
87+ Encryptor objEnc = new Encryptor (key );
88+ objEnc .encrypt (src ,enc );
89+ System .out .println ("Encryption Done!!!" );
90+ // ========================================================================
91+ // Storing encrypted file to database
92+
93+ stmt .execute ("create table if not exists filetable(filename varchar(40) not null primary key,data LONGBLOB)" );
94+
95+
96+ PreparedStatement ps =conn .prepareStatement ("insert into filetable values(?,?)" );
97+
98+ FileInputStream fin =new FileInputStream (enc );
99+ ps .setString (1 ,src );
100+ ps .setBinaryStream (2 , fin );
101+ int i =ps .executeUpdate ();
102+
103+ System .out .println ("(" +src +") file encrypted and stored in database" );
104+
105+ //code to delete enc file after storing it to database
106+ //code it up here
107+
108+ // ==========================================================================
109+ // Retrieving enc file from the database
110+ // System.out.print("Enter key to Decrypt : ");
111+ // String dkey=sc.nextLine();
112+ // if(!dkey.equals(key)){
113+ // System.out.println("Incorrect Key");
114+ // }
115+
116+ PreparedStatement ps_stmt = conn .prepareStatement ("select data from filetable where filename=?" );
117+ ps_stmt .setString (1 , src );
118+ ResultSet rs =ps_stmt .executeQuery ();
119+
120+ FileOutputStream fout = new FileOutputStream (enc );
121+
122+ while (rs .next ())
123+ {
124+ InputStream input = rs .getBinaryStream ("data" );
125+ input .transferTo (fout );
126+ }
127+ fin .close ();
128+ fout .close ();
129+
130+ // =========================================================================
131+ // Decrypting the enc file from the database
132+ Decryptor objDec = new Decryptor (key );
133+ objDec .decrypt (enc ,dec );
134+ System .out .println ("Decryption Done!!!" );
135+
136+
137+ System .out .println ("Do you want to continue?(y/n)" );
138+ ch =sc .nextLine ();
139+ }while (ch .equalsIgnoreCase ("y" ));
140+ sc .close ();
141+ }
142+ catch (Exception e )
143+ {
144+ System .out .println ("Err : " +e .getMessage ());
145+ }
146+
147+
148+
149+ // // ********************************************************************
150+ // // VERSION 1
151+ // // ********************************************************************
152+ // String data = "The data privacy laws are the need of the hour.";
153+ // String key = "hard";
154+ // KeyGenerator kGen = new KeyGenerator(key);
155+ // String nK=kGen.getNumericKey();
21156
22- // System.out.println("generated key: "+nK);
157+ // // System.out.println("generated key: "+nK);
23158
24- ArmstrongManager aMgr = new ArmstrongManager (nK );
25- ColorManager cMgr = new ColorManager (nK );
159+ // ArmstrongManager aMgr = new ArmstrongManager(nK);
160+ // ColorManager cMgr = new ColorManager(nK);
26161
27- String encData = "" ;
28- int temp ;
29- int i ;
30- for (i =0 ;i <data .length ();i ++)
31- {
32- temp = aMgr .encrypt (data .charAt (i ));
33- temp = cMgr .encrypt (temp );
34- encData = encData + (char )temp ;
35- }
162+ // String encData = "";
163+ // int temp;
164+ // int i;
165+ // for(i=0;i<data.length();i++)
166+ // {
167+ // temp = aMgr.encrypt(data.charAt(i));
168+ // temp = cMgr.encrypt(temp);
169+ // encData = encData + (char)temp;
170+ // }
36171
37- String decData = "" ;
172+ // String decData = "";
38173
39- for (i =0 ;i <encData .length ();i ++)
40- {
41- temp = cMgr .decrypt (encData .charAt (i ));
42- temp = aMgr .decrypt (temp );
43- decData = decData + (char )temp ;
44- }
174+ // for(i=0;i<encData.length();i++)
175+ // {
176+ // temp = cMgr.decrypt(encData.charAt(i));
177+ // temp = aMgr.decrypt(temp);
178+ // decData = decData + (char)temp;
179+ // }
45180
46- System .out .println ("================================================================================\n " );
47- System .out .println ("data: " + data );
48- System .out .println ("Encrypted Data: " + encData );
49- System .out .println ("Decrypted Data: " + decData );
181+ // System.out.println("================================================================================\n");
182+ // System.out.println("data: "+ data );
183+ // System.out.println("Encrypted Data: " + encData);
184+ // System.out.println("Decrypted Data: "+ decData);
50185 }
51186
52187}
0 commit comments