Prompt Title: File Structure Mini Projects

Created 11 months ago
Votes 0
Views 57 times
IT
0

Prompt Detail:

Certainly! Here's an example of Java code for a simple secure file transfer system that encrypts files during the transfer process. This code assumes the use of AES encryption algorithm and Java's built-in libraries for encryption and network communication.

java
import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.PBEKeySpec; import javax.crypto.spec.SecretKeySpec; import javax.net.ssl.SSLSocket; import javax.net.ssl.SSLSocketFactory; import java.io.*; import java.net.ServerSocket; import java.net.Socket; import java.security.spec.KeySpec; public class SecureFileTransferSystem { private static final String ALGORITHM = "AES"; private static final String TRANSFORMATION = "AES/ECB/PKCS5Padding"; private static final String SECRET_KEY = "YourSecretKey"; public static void main(String[] args) { // Start the server Thread serverThread = new Thread(() -> startServer()); serverThread.start(); // Upload a file as a client uploadFile("path/to/file.txt", "localhost", 8888); } private static void startServer() { try { ServerSocket serverSocket = new ServerSocket(8888); System.out.println("Server started. Waiting for connections..."); while (true) { Socket socket = serverSocket.accept(); System.out.println("Client connected: " + socket.getInetAddress()); // Handle client connection in a separate thread Thread clientThread = new Thread(() -> handleClient(socket)); clientThread.start(); } } catch (IOException e) { e.printStackTrace(); } } private static void handleClient(Socket socket) { try { // Receive the encrypted file from the client InputStream inputStream = socket.getInputStream(); FileOutputStream fileOutputStream = new FileOutputStream("path/to/destination/file.txt"); decryptFile(inputStream, fileOutputStream); // Close streams and socket fileOutputStream.close(); inputStream.close(); socket.close(); System.out.println("File received and decrypted successfully."); } catch (IOException e) { e.printStackTrace(); } } private static void uploadFile(String filePath, String serverHost, int serverPort) { try { // Create a SSL socket and connect to the server SSLSocketFactory sslSocketFactory = (SSLSocketFactory) SSLSocketFactory.getDefault(); SSLSocket sslSocket = (SSLSocket) sslSocketFactory.createSocket(serverHost, serverPort); System.out.println("Connected to server: " + sslSocket.getInetAddress()); // Send the file to the server OutputStream outputStream = sslSocket.getOutputStream(); FileInputStream fileInputStream = new FileInputStream(filePath); encryptFile(fileInputStream, outputStream); // Close streams and socket outputStream.close(); fileInputStream.close(); sslSocket.close(); System.out.println("File uploaded and encrypted successfully."); } catch (IOException e) { e.printStackTrace(); } } private static void encryptFile(InputStream inputStream, OutputStream outputStream) throws IOException { try { SecretKeySpec secretKeySpec = generateSecretKey(); Cipher cipher = Cipher.getInstance(TRANSFORMATION); cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec); // Encrypt and write the file in chunks byte[] buffer = new byte[8192]; int bytesRead; while ((bytesRead = inputStream.read(buffer)) != -1) { byte[] encryptedBytes = cipher.update(buffer, 0, bytesRead); outputStream.write(encryptedBytes); } byte[] finalBytes = cipher.doFinal(); outputStream.write(finalBytes); } catch (Exception e) { e.printStackTrace(); } } private
avatar
satyam
Shared 4 prompts
Created 11 months ago

Leave a Comment