3 Commits

Author SHA1 Message Date
Kshitij 2622667de4 Moved contents from ./Backend/src/ to ./src/ 2025-07-23 14:54:13 +05:30
SonaliChaudhari dd958b0fde REMOVED OLD ENDPOINTS AND SOME ENCRYPTION AND DECRYPTION METHODS 2025-07-23 11:51:01 +05:30
Kshitij 4e028dd971 Added wiki directory in gitignore. 2025-07-04 01:45:15 +05:30
9 changed files with 313 additions and 313 deletions
+1
View File
@@ -1,3 +1,4 @@
wiki/
HELP.md HELP.md
target/ target/
!.mvn/wrapper/maven-wrapper.jar !.mvn/wrapper/maven-wrapper.jar
@@ -62,71 +62,71 @@ public class HDFScontroller {
} }
} }
@PostMapping("/uploadFile") // @PostMapping("/uploadFile")
public ResponseDTO uploadFile( // public ResponseDTO uploadFile(
@RequestParam("file") MultipartFile file, // @RequestParam("file") MultipartFile file,
@RequestParam String hdfsPath, // @RequestParam String hdfsPath,
@RequestParam String uploadedFileName, // @RequestParam String uploadedFileName,
@RequestParam String username) { // @RequestParam String username) {
try { // try {
// Retrieve the user from the database using the username // // Retrieve the user from the database using the username
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();
// PublicKey publicKey = RSAKeyUtil.getPublicKeyFromBytes(publicKeyBytes);
//
// // Encrypt the file content using the public key
// byte[] encryptedData = encryptFile(file, publicKey);
//
// // Upload the encrypted file to HDFS
// hdfsOperations.uploadFile(encryptedData, hdfsPath, uploadedFileName, username);
//
// return new ResponseDTO("File uploaded successfully", true);
// } catch (IOException e) {
// e.printStackTrace();
// return new ResponseDTO("Failed to upload file locally: " + e.getMessage(), false);
// } catch (Exception e) {
// e.printStackTrace();
// return new ResponseDTO("Failed to upload file to HDFS: " + e.getMessage(), false);
// }
// }
//
// // Helper method to encrypt the file content using RSA encryption
// private byte[] encryptFile(MultipartFile file, PublicKey publicKey) throws Exception {
// // Step 1: Generate a random AES key
// SecretKey aesKey = generateAESKey();
//
// // Step 2: Encrypt the file data using AES
// Cipher aesCipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
// aesCipher.init(Cipher.ENCRYPT_MODE, aesKey);
// byte[] fileData = file.getBytes();
// byte[] encryptedData = aesCipher.doFinal(fileData);
//
// // Step 3: Encrypt the AES key with RSA
// Cipher rsaCipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
// rsaCipher.init(Cipher.ENCRYPT_MODE, publicKey);
// byte[] encryptedAesKey = rsaCipher.doFinal(aesKey.getEncoded());
//
// // Step 4: Combine the encrypted AES key and the encrypted data
// byte[] combined = new byte[4 + encryptedAesKey.length + encryptedData.length];
// combined[0] = (byte) (encryptedAesKey.length >> 24);
// combined[1] = (byte) (encryptedAesKey.length >> 16);
// combined[2] = (byte) (encryptedAesKey.length >> 8);
// combined[3] = (byte) encryptedAesKey.length;
//
// System.arraycopy(encryptedAesKey, 0, combined, 4, encryptedAesKey.length);
// System.arraycopy(encryptedData, 0, combined, 4 + encryptedAesKey.length, encryptedData.length);
//
// return combined;
// }
// Get the public key from the user entity // // Generate a random AES key
byte[] publicKeyBytes = user.getPublicKey(); // private SecretKey generateAESKey() throws NoSuchAlgorithmException {
PublicKey publicKey = RSAKeyUtil.getPublicKeyFromBytes(publicKeyBytes); // KeyGenerator keyGen = KeyGenerator.getInstance("AES");
// keyGen.init(256); // Use 256 bits for AES
// Encrypt the file content using the public key // return keyGen.generateKey();
byte[] encryptedData = encryptFile(file, publicKey); // }
// Upload the encrypted file to HDFS
hdfsOperations.uploadFile(encryptedData, hdfsPath, uploadedFileName, username);
return new ResponseDTO("File uploaded successfully", true);
} catch (IOException e) {
e.printStackTrace();
return new ResponseDTO("Failed to upload file locally: " + e.getMessage(), false);
} catch (Exception e) {
e.printStackTrace();
return new ResponseDTO("Failed to upload file to HDFS: " + e.getMessage(), false);
}
}
// Helper method to encrypt the file content using RSA encryption
private byte[] encryptFile(MultipartFile file, PublicKey publicKey) throws Exception {
// Step 1: Generate a random AES key
SecretKey aesKey = generateAESKey();
// Step 2: Encrypt the file data using AES
Cipher aesCipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
aesCipher.init(Cipher.ENCRYPT_MODE, aesKey);
byte[] fileData = file.getBytes();
byte[] encryptedData = aesCipher.doFinal(fileData);
// Step 3: Encrypt the AES key with RSA
Cipher rsaCipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
rsaCipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] encryptedAesKey = rsaCipher.doFinal(aesKey.getEncoded());
// Step 4: Combine the encrypted AES key and the encrypted data
byte[] combined = new byte[4 + encryptedAesKey.length + encryptedData.length];
combined[0] = (byte) (encryptedAesKey.length >> 24);
combined[1] = (byte) (encryptedAesKey.length >> 16);
combined[2] = (byte) (encryptedAesKey.length >> 8);
combined[3] = (byte) encryptedAesKey.length;
System.arraycopy(encryptedAesKey, 0, combined, 4, encryptedAesKey.length);
System.arraycopy(encryptedData, 0, combined, 4 + encryptedAesKey.length, encryptedData.length);
return combined;
}
// Generate a random AES key
private SecretKey generateAESKey() throws NoSuchAlgorithmException {
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
keyGen.init(256); // Use 256 bits for AES
return keyGen.generateKey();
}
private String saveFileLocally(MultipartFile file) throws IOException { private String saveFileLocally(MultipartFile file) throws IOException {
// Create a temporary directory if it doesn't exist // Create a temporary directory if it doesn't exist
@@ -143,107 +143,107 @@ public class HDFScontroller {
return path.toString(); // Return the local path for further processing return path.toString(); // Return the local path for further processing
} }
@PostMapping("/downloadFile") // @PostMapping("/downloadFile")
public ResponseEntity<Resource> downloadFile( // public ResponseEntity<Resource> downloadFile(
@RequestParam String hdfsEncPath, // @RequestParam String hdfsEncPath,
@RequestParam String username) { // @RequestParam String username) {
try { // try {
// Extract the file name and extension // // Extract the file name and extension
String encFileName = new File(hdfsEncPath).getName(); // String encFileName = new File(hdfsEncPath).getName();
String originalFileName = encFileName.replace(".enc", ""); // String originalFileName = encFileName.replace(".enc", "");
String fileExtension = originalFileName.substring(originalFileName.lastIndexOf(".") + 1); // String fileExtension = originalFileName.substring(originalFileName.lastIndexOf(".") + 1);
//
// Define local decrypted file path // // Define local decrypted file path
String localDecryptedPath = "/SkyCrate/downloaded/" + originalFileName; // String localDecryptedPath = "/SkyCrate/downloaded/" + originalFileName;
//
// Define HDFS paths for encrypted file // // Define HDFS paths for encrypted file
String encFilePath = "/SkyCrate/downloaded/" + encFileName; // String encFilePath = "/SkyCrate/downloaded/" + encFileName;
//
FileSystem fs = HDFSConfig.getHDFS(); // FileSystem fs = HDFSConfig.getHDFS();
//
// Download encrypted file from HDFS // // Download encrypted file from HDFS
fs.copyToLocalFile(new org.apache.hadoop.fs.Path(hdfsEncPath), new org.apache.hadoop.fs.Path(encFilePath)); // fs.copyToLocalFile(new org.apache.hadoop.fs.Path(hdfsEncPath), new org.apache.hadoop.fs.Path(encFilePath));
//
// Retrieve the RSA private key for the user // // Retrieve the RSA private key for the user
User user = userRepository.findByUsername(username) // User user = userRepository.findByUsername(username)
.orElseThrow(() -> new RuntimeException("User not found")); // .orElseThrow(() -> new RuntimeException("User not found"));
PrivateKey privateKey = RSAKeyUtil.getPrivateKeyFromBytes(user.getPrivateKey()); // PrivateKey privateKey = RSAKeyUtil.getPrivateKeyFromBytes(user.getPrivateKey());
//
// Read the encrypted file content // // Read the encrypted file content
byte[] encryptedFileContent = Files.readAllBytes(Paths.get(encFilePath)); // byte[] encryptedFileContent = Files.readAllBytes(Paths.get(encFilePath));
//
// Step 1: Extract the AES key length from the combined data // // Step 1: Extract the AES key length from the combined data
int aesKeyLength = ((encryptedFileContent[0] & 0xFF) << 24) | // int aesKeyLength = ((encryptedFileContent[0] & 0xFF) << 24) |
((encryptedFileContent[1] & 0xFF) << 16) | // ((encryptedFileContent[1] & 0xFF) << 16) |
((encryptedFileContent[2] & 0xFF) << 8) | // ((encryptedFileContent[2] & 0xFF) << 8) |
(encryptedFileContent[3] & 0xFF); // (encryptedFileContent[3] & 0xFF);
//
// Step 2: Extract the encrypted AES key and encrypted data // // Step 2: Extract the encrypted AES key and encrypted data
byte[] encryptedAesKey = new byte[aesKeyLength]; // byte[] encryptedAesKey = new byte[aesKeyLength];
byte[] encryptedData = new byte[encryptedFileContent.length - 4 - aesKeyLength]; // byte[] encryptedData = new byte[encryptedFileContent.length - 4 - aesKeyLength];
//
System.arraycopy(encryptedFileContent, 4, encryptedAesKey, 0, aesKeyLength); // System.arraycopy(encryptedFileContent, 4, encryptedAesKey, 0, aesKeyLength);
System.arraycopy(encryptedFileContent, 4 + aesKeyLength, encryptedData, 0, encryptedData.length); // System.arraycopy(encryptedFileContent, 4 + aesKeyLength, encryptedData, 0, encryptedData.length);
//
// Step 3: Decrypt the AES key using RSA // // Step 3: Decrypt the AES key using RSA
Cipher rsaCipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); // Cipher rsaCipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
rsaCipher.init(Cipher.DECRYPT_MODE, privateKey); // rsaCipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] aesKeyBytes = rsaCipher.doFinal(encryptedAesKey); // byte[] aesKeyBytes = rsaCipher.doFinal(encryptedAesKey);
//
// Create the AES key // // Create the AES key
SecretKey aesKey = new SecretKeySpec(aesKeyBytes, "AES"); // SecretKey aesKey = new SecretKeySpec(aesKeyBytes, "AES");
//
// Step 4: Decrypt the data using AES // // Step 4: Decrypt the data using AES
Cipher aesCipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); // Cipher aesCipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
aesCipher.init(Cipher.DECRYPT_MODE, aesKey); // aesCipher.init(Cipher.DECRYPT_MODE, aesKey);
//
// Decrypt the file content using the provided decrypt method // // Decrypt the file content using the provided decrypt method
// byte[] decryptedFileContent = RSAKeyUtil.decrypt(encryptedFileContent, privateKey); //// byte[] decryptedFileContent = RSAKeyUtil.decrypt(encryptedFileContent, privateKey);
byte[] decryptedFileContent = aesCipher.doFinal(encryptedData); // byte[] decryptedFileContent = aesCipher.doFinal(encryptedData);
//
// Write the decrypted content to the original file // // Write the decrypted content to the original file
Files.write(Paths.get(localDecryptedPath + "." + fileExtension), decryptedFileContent); // Files.write(Paths.get(localDecryptedPath + "." + fileExtension), decryptedFileContent);
//
//
// Log the file creation // // Log the file creation
if (Files.exists(Paths.get(localDecryptedPath + "." + fileExtension))) { // if (Files.exists(Paths.get(localDecryptedPath + "." + fileExtension))) {
System.out.println("File created successfully at: " + localDecryptedPath + "." + fileExtension); // System.out.println("File created successfully at: " + localDecryptedPath + "." + fileExtension);
} else { // } else {
System.out.println("Failed to create file at: " + localDecryptedPath + "." + fileExtension); // System.out.println("Failed to create file at: " + localDecryptedPath + "." + fileExtension);
} // }
//
// Create the decrypted file resource // // Create the decrypted file resource
File decryptedFile = new File(localDecryptedPath + "." + fileExtension); // File decryptedFile = new File(localDecryptedPath + "." + fileExtension);
Resource resource = new FileSystemResource(decryptedFile); // Resource resource = new FileSystemResource(decryptedFile);
//
// Return the file as a response // // Return the file as a response
return ResponseEntity.ok() // return ResponseEntity.ok()
.contentLength(decryptedFile.length()) // .contentLength(decryptedFile.length())
.contentType(MediaType.APPLICATION_OCTET_STREAM) // .contentType(MediaType.APPLICATION_OCTET_STREAM)
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + decryptedFile.getName() + "\"") // .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + decryptedFile.getName() + "\"")
.body(resource); // .body(resource);
//
} catch (Exception e) { // } catch (Exception e) {
e.printStackTrace(); // e.printStackTrace();
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) // return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body(null); // .body(null);
} // }
} // }
//
//
public void initializeKeysForUser(String username) { // public void initializeKeysForUser(String username) {
try { // try {
// Check if the public key file exists // // Check if the public key file exists
Path publicKeyPath = Paths.get("C:\\Users\\sonal\\OneDrive\\Desktop\\SkyCrate\\Skycrate\\keys", username + "_public.key"); // Path publicKeyPath = Paths.get("C:\\Users\\sonal\\OneDrive\\Desktop\\SkyCrate\\Skycrate\\keys", username + "_public.key");
if (!Files.exists(publicKeyPath)) { // if (!Files.exists(publicKeyPath)) {
// Generate and store keys if they do not exist // // Generate and store keys if they do not exist
KeyUtil.generateAndStoreKeyPair(username); // KeyUtil.generateAndStoreKeyPair(username);
} // }
} catch (Exception e) { // } catch (Exception e) {
e.printStackTrace(); // e.printStackTrace();
} // }
} // }
//
@DeleteMapping("/deleteFile") @DeleteMapping("/deleteFile")
public ResponseDTO deleteFile(@RequestParam String hdfsPath) { public ResponseDTO deleteFile(@RequestParam String hdfsPath) {
@@ -17,63 +17,63 @@ public class EncryptionUtil {
private static final int IV_LENGTH = 16; // for AES CBC private static final int IV_LENGTH = 16; // for AES CBC
private static final int ITERATIONS = 65536; private static final int ITERATIONS = 65536;
private static final int KEY_LENGTH = 256; // bits private static final int KEY_LENGTH = 256; // bits
//
// --- AES key derivation using PBKDF2 --- // // --- AES key derivation using PBKDF2 ---
public static SecretKey deriveAESKey(char[] password, byte[] salt) // public static SecretKey deriveAESKey(char[] password, byte[] salt)
throws NoSuchAlgorithmException, InvalidKeySpecException { // throws NoSuchAlgorithmException, InvalidKeySpecException {
//
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256"); // SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
//
KeySpec spec = new PBEKeySpec(password, salt, ITERATIONS, KEY_LENGTH); // KeySpec spec = new PBEKeySpec(password, salt, ITERATIONS, KEY_LENGTH);
byte[] keyBytes = factory.generateSecret(spec).getEncoded(); // byte[] keyBytes = factory.generateSecret(spec).getEncoded();
//
return new SecretKeySpec(keyBytes, "AES"); // return new SecretKeySpec(keyBytes, "AES");
} // }
//
// --- Encrypt data using AES-CBC --- // // --- Encrypt data using AES-CBC ---
public static byte[] encrypt(byte[] data, SecretKey key, byte[] iv) // public static byte[] encrypt(byte[] data, SecretKey key, byte[] iv)
throws GeneralSecurityException { // throws GeneralSecurityException {
//
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); // Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
//
IvParameterSpec ivSpec = new IvParameterSpec(iv); // IvParameterSpec ivSpec = new IvParameterSpec(iv);
cipher.init(Cipher.ENCRYPT_MODE, key, ivSpec); // cipher.init(Cipher.ENCRYPT_MODE, key, ivSpec);
//
return cipher.doFinal(data); // return cipher.doFinal(data);
} // }
// --- Decrypt data using AES-CBC --- // --- Decrypt data using AES-CBC ---
public static byte[] decrypt(byte[] encryptedData, SecretKey key, byte[] iv) // public static byte[] decrypt(byte[] encryptedData, SecretKey key, byte[] iv)
throws GeneralSecurityException { // throws GeneralSecurityException {
//
// Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
//
// IvParameterSpec ivSpec = new IvParameterSpec(iv);
// cipher.init(Cipher.DECRYPT_MODE, key, ivSpec);
//
// return cipher.doFinal(encryptedData);
// }
//
// // --- Generate random salt ---
// public static byte[] generateSalt() {
// byte[] salt = new byte[SALT_LENGTH];
// new SecureRandom().nextBytes(salt);
// return salt;
// }
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); // // --- Generate random IV ---
// public static byte[] generateIV() {
IvParameterSpec ivSpec = new IvParameterSpec(iv); // byte[] iv = new byte[IV_LENGTH];
cipher.init(Cipher.DECRYPT_MODE, key, ivSpec); // new SecureRandom().nextBytes(iv);
// return iv;
return cipher.doFinal(encryptedData); // }
} //
// // --- Optional: Utility to base64 encode data ---
// --- Generate random salt --- // public static String encodeBase64(byte[] data) {
public static byte[] generateSalt() { // return Base64.getEncoder().encodeToString(data);
byte[] salt = new byte[SALT_LENGTH]; // }
new SecureRandom().nextBytes(salt); //
return salt; // public static byte[] decodeBase64(String base64) {
} // return Base64.getDecoder().decode(base64);
// }
// --- Generate random IV ---
public static byte[] generateIV() {
byte[] iv = new byte[IV_LENGTH];
new SecureRandom().nextBytes(iv);
return iv;
}
// --- Optional: Utility to base64 encode data ---
public static String encodeBase64(byte[] data) {
return Base64.getEncoder().encodeToString(data);
}
public static byte[] decodeBase64(String base64) {
return Base64.getDecoder().decode(base64);
}
} }
@@ -120,41 +120,41 @@ public class HDFSOperations {
// } // }
// } // }
public void uploadFile(byte[] fileData, String hdfsPath, String uploadedFileName, String username) { // public void uploadFile(byte[] fileData, String hdfsPath, String uploadedFileName, String username) {
try { // try {
FileSystem fs = HDFSConfig.getHDFS(); // FileSystem fs = HDFSConfig.getHDFS();
ByteArrayInputStream inputStream = new ByteArrayInputStream(fileData); // ByteArrayInputStream inputStream = new ByteArrayInputStream(fileData);
String finalHdfsPath = hdfsPath.endsWith("/") ? hdfsPath + uploadedFileName : hdfsPath + "/" + uploadedFileName; // String finalHdfsPath = hdfsPath.endsWith("/") ? hdfsPath + uploadedFileName : hdfsPath + "/" + uploadedFileName;
Path hdfsFilePath = new Path(finalHdfsPath); // Path hdfsFilePath = new Path(finalHdfsPath);
try (FSDataOutputStream outputStream = fs.create(hdfsFilePath)) { // try (FSDataOutputStream outputStream = fs.create(hdfsFilePath)) {
IOUtils.copyBytes(inputStream, outputStream, 4096, true); // IOUtils.copyBytes(inputStream, outputStream, 4096, true);
} // }
} catch (IOException e) { // } catch (IOException e) {
throw new RuntimeException("Failed to upload file to HDFS: " + e.getMessage(), e); // throw new RuntimeException("Failed to upload file to HDFS: " + e.getMessage(), e);
} catch (Exception e) { // } catch (Exception e) {
throw new RuntimeException(e); // throw new RuntimeException(e);
} // }
} // }
//
public void downloadFile(String hdfsEncPath, String localPathWithoutExt, String username) { // public void downloadFile(String hdfsEncPath, String localPathWithoutExt, String username) {
try { // try {
FileSystem fs = HDFSConfig.getHDFS(); // FileSystem fs = HDFSConfig.getHDFS();
String encFilePath = localPathWithoutExt + ".enc"; // String encFilePath = localPathWithoutExt + ".enc";
fs.copyToLocalFile(new Path(hdfsEncPath), new Path(encFilePath)); // fs.copyToLocalFile(new Path(hdfsEncPath), new Path(encFilePath));
//
User user = userRepository.findByUsername(username) // User user = userRepository.findByUsername(username)
.orElseThrow(() -> new RuntimeException("User not found")); // .orElseThrow(() -> new RuntimeException("User not found"));
PrivateKey privateKey = RSAKeyUtil.getPrivateKeyFromBytes(user.getPrivateKey()); // PrivateKey privateKey = RSAKeyUtil.getPrivateKeyFromBytes(user.getPrivateKey());
//
byte[] encryptedFileContent = Files.readAllBytes(Paths.get(encFilePath)); // byte[] encryptedFileContent = Files.readAllBytes(Paths.get(encFilePath));
byte[] decryptedFileContent = RSAKeyUtil.decrypt(encryptedFileContent, privateKey); // byte[] decryptedFileContent = RSAKeyUtil.decrypt(encryptedFileContent, privateKey);
//
Files.write(Paths.get(localPathWithoutExt), decryptedFileContent); // Files.write(Paths.get(localPathWithoutExt), decryptedFileContent);
Files.deleteIfExists(Paths.get(encFilePath)); // Files.deleteIfExists(Paths.get(encFilePath));
} catch (Exception e) { // } catch (Exception e) {
throw new RuntimeException("Failed to download or decrypt file: " + e.getMessage(), e); // throw new RuntimeException("Failed to download or decrypt file: " + e.getMessage(), e);
} // }
} // }
public void createFolder(String hdfsPath) { public void createFolder(String hdfsPath) {
try { try {
@@ -80,15 +80,15 @@ public class EncryptionUtil {
return cipher.doFinal(data); return cipher.doFinal(data);
} }
// --------- Encrypt/decrypt RSA private key using AES derived from password --------- // // --------- Encrypt/decrypt RSA private key using AES derived from password ---------
//
public static byte[] encryptPrivateKey(PrivateKey privateKey, String password, byte[] salt, byte[] iv) throws Exception { // public static byte[] encryptPrivateKey(PrivateKey privateKey, String password, byte[] salt, byte[] iv) throws Exception {
SecretKey aesKey = deriveKey(password.toCharArray(), salt); // SecretKey aesKey = deriveKey(password.toCharArray(), salt);
return encrypt(privateKey.getEncoded(), aesKey, iv); // return encrypt(privateKey.getEncoded(), aesKey, iv);
} // }
//
public static byte[] decryptPrivateKey(byte[] encryptedPrivateKey, String password, byte[] salt, byte[] iv) throws Exception { // public static byte[] decryptPrivateKey(byte[] encryptedPrivateKey, String password, byte[] salt, byte[] iv) throws Exception {
SecretKey aesKey = deriveKey(password.toCharArray(), salt); // SecretKey aesKey = deriveKey(password.toCharArray(), salt);
return decrypt(encryptedPrivateKey, aesKey, iv); // return decrypt(encryptedPrivateKey, aesKey, iv);
} // }
} }
@@ -9,31 +9,31 @@ import java.security.spec.X509EncodedKeySpec;
public class KeyUtil { public class KeyUtil {
public static void generateAndStoreKeyPair(String username) throws Exception { // public static void generateAndStoreKeyPair(String username) throws Exception {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA"); // KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(2048); // Key size // keyPairGenerator.initialize(2048); // Key size
KeyPair keyPair = keyPairGenerator.generateKeyPair(); // KeyPair keyPair = keyPairGenerator.generateKeyPair();
//
// Store the public key // // Store the public key
Path publicKeyPath = Paths.get("C:\\Users\\sonal\\OneDrive\\Desktop\\SkyCrate\\Skycrate\\keys", username + "_public.key"); // Path publicKeyPath = Paths.get("C:\\Users\\sonal\\OneDrive\\Desktop\\SkyCrate\\Skycrate\\keys", username + "_public.key");
Files.write(publicKeyPath, keyPair.getPublic().getEncoded()); // Files.write(publicKeyPath, keyPair.getPublic().getEncoded());
//
// Store the private key // // Store the private key
Path privateKeyPath = Paths.get("C:\\Users\\sonal\\OneDrive\\Desktop\\SkyCrate\\Skycrate\\keys", username + "_private.key"); // Path privateKeyPath = Paths.get("C:\\Users\\sonal\\OneDrive\\Desktop\\SkyCrate\\Skycrate\\keys", username + "_private.key");
Files.write(privateKeyPath, keyPair.getPrivate().getEncoded()); // Files.write(privateKeyPath, keyPair.getPrivate().getEncoded());
} // }
//
public static PublicKey getPublicKeyForUser(String username) throws Exception { // public static PublicKey getPublicKeyForUser(String username) throws Exception {
Path path = Paths.get("C:\\Users\\sonal\\OneDrive\\Desktop\\SkyCrate\\Skycrate\\keys", username + "_public.key"); // Path path = Paths.get("C:\\Users\\sonal\\OneDrive\\Desktop\\SkyCrate\\Skycrate\\keys", username + "_public.key");
byte[] bytes = Files.readAllBytes(path); // byte[] bytes = Files.readAllBytes(path);
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(bytes); // X509EncodedKeySpec keySpec = new X509EncodedKeySpec(bytes);
return KeyFactory.getInstance("RSA").generatePublic(keySpec); // return KeyFactory.getInstance("RSA").generatePublic(keySpec);
} // }
//
public static PrivateKey getPrivateKeyForUser(String username) throws Exception { // public static PrivateKey getPrivateKeyForUser(String username) throws Exception {
Path path = Paths.get("C:\\Users\\sonal\\OneDrive\\Desktop\\SkyCrate\\Skycrate\\keys", username + "_private.key"); // Path path = Paths.get("C:\\Users\\sonal\\OneDrive\\Desktop\\SkyCrate\\Skycrate\\keys", username + "_private.key");
byte[] bytes = Files.readAllBytes(path); // byte[] bytes = Files.readAllBytes(path);
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(bytes); // PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(bytes);
return KeyFactory.getInstance("RSA").generatePrivate(keySpec); // return KeyFactory.getInstance("RSA").generatePrivate(keySpec);
} // }
} }
@@ -52,22 +52,22 @@ public class RSAKeyUtil {
return cipher.doFinal(encryptedData); return cipher.doFinal(encryptedData);
} }
// AES key generation // // AES key generation
public static SecretKey generateAESKey(int keySize) throws NoSuchAlgorithmException { // public static SecretKey generateAESKey(int keySize) throws NoSuchAlgorithmException {
if (keySize != 128 && keySize != 192 && keySize != 256) { // if (keySize != 128 && keySize != 192 && keySize != 256) {
throw new IllegalArgumentException("Invalid AES key size. Must be 128, 192, or 256 bits."); // throw new IllegalArgumentException("Invalid AES key size. Must be 128, 192, or 256 bits.");
} // }
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES"); // KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
keyGenerator.init(keySize); // keyGenerator.init(keySize);
return keyGenerator.generateKey(); // return keyGenerator.generateKey();
} // }
//
public static byte[] encryptAESKey(SecretKey aesKey, PublicKey publicKey) throws Exception { // public static byte[] encryptAESKey(SecretKey aesKey, PublicKey publicKey) throws Exception {
return encrypt(aesKey.getEncoded(), publicKey); // return encrypt(aesKey.getEncoded(), publicKey);
} // }
//
public static SecretKey decryptAESKey(byte[] encryptedAESKey, PrivateKey privateKey, int keySize) throws Exception { // public static SecretKey decryptAESKey(byte[] encryptedAESKey, PrivateKey privateKey, int keySize) throws Exception {
byte[] decryptedKey = decrypt(encryptedAESKey, privateKey); // byte[] decryptedKey = decrypt(encryptedAESKey, privateKey);
return new SecretKeySpec(decryptedKey, 0, decryptedKey.length, "AES"); // return new SecretKeySpec(decryptedKey, 0, decryptedKey.length, "AES");
} // }
} }
-1
View File
@@ -6,4 +6,3 @@ server:
key-store-password: changeit key-store-password: changeit
key-store-type: PKCS12 key-store-type: PKCS12
key-alias: tomcat key-alias: tomcat
Binary file not shown.