Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions src/Classes/DatabasesManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,23 @@ public DatabasesManager(File databasesFile) {
this.databasesFile = databasesFile;
this.databases = loadDatabases() != null ? loadDatabases() : new HashMap<>();
}

//Verify the hashed password with the password entered by the user
public boolean verifyDatabase(String dbName, String password) {
if (!databases.containsKey(dbName)) return false;
Database database = databases.get(dbName);
String hashedPassword = sha256.calculateHash(password);
return database.getHashPassword().equals(hashedPassword);
}

//create database object with user inputs
public void createDatabase(String dbName, String password, Map<String, String> encryptionMap) {
if (databases.containsKey(dbName)) {
throw new IllegalArgumentException("Database already exists.");
}
String hashedPassword = sha256.calculateHash(password);
Database newDatabase = new Database(dbName, hashedPassword, encryptionMap);
databases.put(dbName, newDatabase);
saveDatabases();
databases.put(dbName, newDatabase); //add the dbname and the db object to a map

saveDatabases();//save the map
}

private Map<String, Database> loadDatabases() {
Expand All @@ -61,7 +62,7 @@ private void saveDatabases() {
}
}

// Classe interne représentant une base de données
// class database to make the database manager easy to use
public static class Database {
private final String name;
private final String hashPassword;
Expand All @@ -85,15 +86,16 @@ public Map<String, String> getEncryptionMap() {
return encryptionMap;
}
}
//method to get the encryption map for the encryption and decryption during the database user selection
public Map<String, String> getEncryptionMap(String dbName) {
if (!databases.containsKey(dbName)) {
if (!databases.containsKey(dbName)) { // if the database name doesn't exist
throw new IllegalArgumentException("Database does not exist: " + dbName);
}
try (FileReader reader = new FileReader(databasesFile)) {
Gson gson = new Gson();
Map<String, Map<String, Object>> allDatabases = gson.fromJson(reader, Map.class);
Map<String, Object> dbData = allDatabases.get(dbName);
return (Map<String, String>) dbData.get("encryptionMap");
Map<String, Map<String, Object>> allDatabases = gson.fromJson(reader, Map.class); //get all the databases info from databases.json
Map<String, Object> dbData = allDatabases.get(dbName); //get the wanted database
return (Map<String, String>) dbData.get("encryptionMap"); // get all the encryption methods from the current database
} catch (IOException e) {
throw new RuntimeException("Failed to read databases file.", e);
}
Expand Down
12 changes: 6 additions & 6 deletions src/Classes/Menu.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ else if (dbChoice == 2) {
Map<String, String> encryptionMap = new HashMap<>();
boolean addMoreEncryptions = true;

while (addMoreEncryptions) {
while (addMoreEncryptions) { //ask user what encryption method he wants to use
System.out.println("Choose an encryption method:");
System.out.println("1. RotX");
System.out.println("2. RC4");
Expand All @@ -80,25 +80,25 @@ else if (dbChoice == 2) {
scanner.nextLine(); // Consume newline character

switch (encryptionChoice) {
case 1 -> {
case 1 -> { //case rotX ask value for future executions
System.out.println("Enter the shift value for RotX:");
String shiftValue = scanner.nextLine();
encryptionMap.put("RotX", shiftValue);
}
case 2 -> {
case 2 -> {//case rc4 ask value for future executions
System.out.println("Enter the key for RC4:");
String rc4Key = scanner.nextLine();
encryptionMap.put("RC4", rc4Key);
}
case 3 -> {
case 3 -> {//case vigenere ask value for future execution
System.out.println("Enter the key for Vigenere:");
String vigenereKey = scanner.nextLine();
encryptionMap.put("Vigenere", vigenereKey);
}
case 4 -> {
case 4 -> {//case vigenere ask value for future execution
encryptionMap.put("Polybios", "default");
}
case 5 -> addMoreEncryptions = false;
case 5 -> addMoreEncryptions = false;//leave the encryptions chain
default -> System.out.println("Invalid choice. Please choose a valid encryption method.");
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Classes/PasswordUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public static String generateRandomPassword(int length) {
Random random = new Random();
StringBuilder password = new StringBuilder();
for (int i = 0; i < length; i++) {
password.append(chars.charAt(random.nextInt(chars.length())));
password.append(chars.charAt(random.nextInt(chars.length()))); // generate random password in range chars, from random int
}
return password.toString();
}
Expand Down
49 changes: 30 additions & 19 deletions src/Classes/SiteManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ public class SiteManager {
private final Map<String, String> encryptionMap; // Méthodes de chiffrement associées

public SiteManager(File dbFile, Map<String, String> encryptionMap) {
this.dbFile = dbFile;
this.encryptionMap = encryptionMap;
this.dbFile = dbFile; //file name
this.encryptionMap = encryptionMap;// encryption method with parameter needed
this.sites = loadSites();
}

public void manageSites(Scanner scanner) {
public void manageSites(Scanner scanner) { //methods to ask user what method he will call
while (true) {
System.out.println("Choose an action:");
System.out.println("1. Add a site");
Expand All @@ -31,7 +31,7 @@ public void manageSites(Scanner scanner) {
int choice = scanner.nextInt();
scanner.nextLine();

switch (choice) {
switch (choice) { //calls method based on the user input
case 1 -> addSite(scanner);
case 2 -> modifySite(scanner);
case 3 -> deleteSite(scanner);
Expand All @@ -42,7 +42,7 @@ public void manageSites(Scanner scanner) {
}
}

public void addSite(String siteName, String username, String password) {
public void addSite(String siteName, String username, String password) {//add method use for applying value from scanner method and for the test file
Map<String, String> site = new HashMap<>();
site.put("siteName", siteName);
site.put("username", username);
Expand All @@ -51,7 +51,7 @@ public void addSite(String siteName, String username, String password) {
saveSites();
}

public void addSite(Scanner scanner) {
public void addSite(Scanner scanner) { //add site in the selected json
System.out.println("Enter site name:");
String siteName = scanner.nextLine();
System.out.println("Enter username:");
Expand All @@ -61,7 +61,7 @@ public void addSite(Scanner scanner) {
addSite(siteName, username, password);
}

public void modifySite(String siteName, String newUsername, String newPassword) {
public void modifySite(String siteName, String newUsername, String newPassword) { //modify method use for applying value from scanner method and for the test file
for (Map<String, String> site : sites) {
if (site.get("siteName").equals(siteName)) {
if (newUsername != null && !newUsername.isEmpty()) {
Expand All @@ -77,7 +77,7 @@ public void modifySite(String siteName, String newUsername, String newPassword)
throw new IllegalArgumentException("Site not found: " + siteName);
}

public void modifySite(Scanner scanner) {
public void modifySite(Scanner scanner) { //modify the selected site from the selected json
System.out.println("Enter the site name to modify:");
String siteName = scanner.nextLine();
System.out.println("Enter the new username (leave empty to keep current):");
Expand All @@ -87,18 +87,18 @@ public void modifySite(Scanner scanner) {
modifySite(siteName, newUsername, newPassword);
}

public void deleteSite(String siteName) {
public void deleteSite(String siteName) { //delete method use for the test file
sites.removeIf(site -> site.get("siteName").equals(siteName));
saveSites();
}

public void deleteSite(Scanner scanner) {
public void deleteSite(Scanner scanner) { //delete the selected site from the selected json
System.out.println("Enter the site name to delete:");
String siteName = scanner.nextLine();
deleteSite(siteName);
}

public void displaySites() {
public void displaySites() { // displays all sites from selected json
if (sites.isEmpty()) {
System.out.println("No sites available.");
} else {
Expand All @@ -117,14 +117,14 @@ public void displaySites() {
public List<Map<String, String>> loadSites() {
if (!dbFile.exists()) return new ArrayList<>();
try (FileReader reader = new FileReader(dbFile)) {
// Lire le fichier et déchiffrer son contenu
// read file and decrypt
StringBuilder encryptedContent = new StringBuilder();
int c;
while ((c = reader.read()) != -1) {
encryptedContent.append((char) c);
}

// Déchiffrement du contenu entier
// decrypt the file content
String decryptedContent = decrypt(encryptedContent.toString());

Gson gson = new Gson();
Expand All @@ -138,12 +138,12 @@ public List<Map<String, String>> loadSites() {

public void saveSites() {
try (FileWriter writer = new FileWriter(dbFile)) {
// Conversion des sites en JSON
// convert sites into java json file
Gson gson = new GsonBuilder().setPrettyPrinting().create();
Map<String, Object> data = Map.of("sites", sites);
String jsonContent = gson.toJson(data);

// Chiffrer le contenu JSON
// crypt json content
String encryptedContent = encrypt(jsonContent);

writer.write(encryptedContent);
Expand All @@ -154,20 +154,26 @@ public void saveSites() {

private String encrypt(String input) {
String result = input;
//all case for all encryption methods
for (String method : encryptionMap.keySet()) {
switch (method) {
case "RotX" -> {
int shift = Integer.parseInt(encryptionMap.get("RotX"));
//rotx encryption
int shift = Integer.parseInt(encryptionMap.get("RotX")); //get encryption value from the encryption map from databases.json
result = ROTX.encryptROT(result, shift);
}
case "RC4" -> {
//rc4 encryption

RC4 rc4 = new RC4();
String key = encryptionMap.get("RC4");
String key = encryptionMap.get("RC4");//get encryption value from the encryption map from databases.json
rc4.init(key);
result = rc4.encrypt(result);
}
case "Vigenere" -> {
String key = encryptionMap.get("Vigenere");
//vigenere encryption

String key = encryptionMap.get("Vigenere");//get encryption value from the encryption map from databases.json
result = VigenereAlgo.encrypt(result, key);
}
case "Polybios" -> {
Expand All @@ -181,23 +187,28 @@ private String encrypt(String input) {

private String decrypt(String input) {
String result = input;
// Inverser l'ordre des méthodes pour le déchiffrement
// invert encryption order to handle the multiple encryption
List<String> methods = new ArrayList<>(encryptionMap.keySet());
Collections.reverse(methods);

for (String method : methods) {
switch (method) {
case "RotX" -> {
//decrypt json content from databases RotX encryptionmethod
int shift = Integer.parseInt(encryptionMap.get("RotX"));
result = ROTX.decryptROT(result, shift);
}
case "RC4" -> {
//decrypt json content from databases RC4 encryptionmethod

RC4 rc4 = new RC4();
String key = encryptionMap.get("RC4");
rc4.init(key);
result = rc4.decrypt(result);
}
case "Vigenere" -> {
//decrypt json content from databases Vigenere encryptionmethod

String key = encryptionMap.get("Vigenere");
result = VigenereAlgo.decrypt(result, key);
}
Expand Down
Loading