IMPLEMENTED ENCRYPTION & DECRYPTION

This commit is contained in:
vedang29
2025-04-18 00:28:22 +05:30
parent 5cd396951d
commit 8be15dcac5
@@ -73,29 +73,30 @@ public class HDFScontroller {
@RequestParam String uploadedFileName, @RequestParam String uploadedFileName,
@RequestParam String username) { @RequestParam String username) {
try { try {
// Retrieve the user from the database using the username // Step 1: Retrieve the user and their RSA public key
User user = userRepository.findByUsername(username).orElseThrow(() -> new RuntimeException("User not found")); User user = userRepository.findByUsername(username)
.orElseThrow(() -> new RuntimeException("User not found"));
// Get the public key from the user entity
byte[] publicKeyBytes = user.getPublicKey(); byte[] publicKeyBytes = user.getPublicKey();
PublicKey publicKey = RSAKeyUtil.getPublicKeyFromBytes(publicKeyBytes); PublicKey publicKey = RSAKeyUtil.getPublicKeyFromBytes(publicKeyBytes);
// Encrypt the file content using the public key // Step 2: Encrypt the file content using hybrid encryption (AES + RSA)
byte[] encryptedData = encryptFile(file, publicKey); byte[] fileBytes = file.getBytes();
byte[] encryptedData = EncryptionUtil.encrypt(fileBytes, publicKey);
// Upload the encrypted file to HDFS // Step 3: Upload encrypted data to HDFS
hdfsOperations.uploadFile(encryptedData, hdfsPath, uploadedFileName, username); hdfsOperations.uploadFile(encryptedData, hdfsPath, uploadedFileName, username);
return new ResponseDTO("File uploaded successfully", true); return new ResponseDTO("File uploaded successfully", true);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
return new ResponseDTO("Failed to upload file locally: " + e.getMessage(), false); return new ResponseDTO("Failed to read file: " + e.getMessage(), false);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
return new ResponseDTO("Failed to upload file to HDFS: " + e.getMessage(), false); return new ResponseDTO("Failed to upload file to HDFS: " + e.getMessage(), false);
} }
} }
// Helper method to encrypt the file content using RSA encryption // Helper method to encrypt the file content using RSA encryption
private byte[] encryptFile(MultipartFile file, PublicKey publicKey) throws Exception { private byte[] encryptFile(MultipartFile file, PublicKey publicKey) throws Exception {
// Step 1: Generate a random AES key // Step 1: Generate a random AES key