Skip to content
Closed
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
4 changes: 2 additions & 2 deletions .idea/libraries/bytedeco_javacv_platform.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions databases.json

This file was deleted.

72 changes: 72 additions & 0 deletions src/Classes/AlgorithmChoice.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package Classes;

import Classes.Enigma.Enigma; // Import the Enigma class for encryption and decryption


public class AlgorithmChoice {

////////////// ENCRYPTION ///////////
// Methods for each encryption algorithm

// Encrypts using the Enigma encryption algorithm
public static String encryptEnigma(String password) {
Enigma encryptionBox = new Enigma(); // Create an instance of the Enigma encryption box
// Calls the Enigma's encrypt method to encrypt the password
return encryptionBox.encrypt(password);
}

// Encrypts using the RC4 algorithm
public static String encryptRC4(String password) {
RC4 encryptionBox = new RC4(); // Create an instance of the RC4 encryption box
String key = "key"; // The encryption key for RC4
encryptionBox.init(key); // Initialize the RC4 encryption with the key
// Calls the RC4's encrypt method to encrypt the password
return encryptionBox.encrypt(password);
}

// Encrypts using the ROT(x) algorithm
public static String encryptROT(String password, int x) {
// Calls the ROTX's encrypt method to perform a ROT(x) shift on the password
return ROTX.encryptROT(password, x);
}

// Encrypts using the Vigenère cipher algorithm
public static String encryptVigenere(String password, String key) {
VigenereAlgo vigenere = new VigenereAlgo();
vigenere.setKey(key);
return vigenere.encrypt(password);
}

/////////// DECRYPTION ///////////
// Methods for each decryption algorithm

// Decrypts using the Enigma decryption algorithm
public static String decryptEnigma(String encryptedMessage) {
Enigma decryptionBox = new Enigma(); // Create an instance of the Enigma decryption box
// Calls the Enigma's decrypt method to decrypt the encrypted message
return decryptionBox.decrypt(encryptedMessage);
}

// Decrypts using the RC4 algorithm
public static String decryptRC4(String encryptedMessage) {
// Create an instance of the RC4 decryption box
RC4 decryptionBox = new RC4();
String key = "key"; // The decryption key for RC4
decryptionBox.init(key); // Initialize the RC4 decryption with the key
// Calls the RC4's decrypt method to decrypt the encrypted message
return decryptionBox.decrypt(encryptedMessage);
}

// Decrypts using the ROT(x) algorithm
public static String decryptROT(String input, int x) {
// Calls the ROTX's decrypt method to reverse the ROT(x) shift on the input
return ROTX.decryptROT(input, x);
}

// Decrypts using the Vigenère cipher algorithm
public static String decryptVigenere(String encryptedMessage, String key) {
VigenereAlgo vigenere = new VigenereAlgo();
vigenere.setKey(key);
return vigenere.decrypt(encryptedMessage);
}
}
18 changes: 18 additions & 0 deletions src/Classes/Database.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package Classes;

public class Database{
private final String hashedPassword;
private final String databaseName;
private final String encryptionMode;


public Database (String databaseName, String hashedPassword, String encryptionMode) {
this.databaseName = databaseName;
this.hashedPassword = hashedPassword;
this.encryptionMode = encryptionMode;
}




}
88 changes: 70 additions & 18 deletions src/Classes/DatabasesManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,45 +13,97 @@
import java.util.Map;

public class DatabasesManager {
private final File databasesFile;
private final Map<String, String> databases;
Sha256 sha256 = new Sha256();
private final File databasesFile; // The file where the databases and their passwords are stored
private final Map<String, DatabaseInfo> databases; // A map holding the database names as keys and their info as values
private final Sha256 sha256 = new Sha256();

public DatabasesManager(File databasesFile) {
this.databasesFile = databasesFile;
this.databases = loadDatabases();
this.databases = new HashMap<>(); // Initialize the map before the try-catch block
if (!databasesFile.exists()) {
try {
databasesFile.createNewFile();
saveDatabases(); // Sauvegarder une base de données vide
} catch (IOException e) {
e.printStackTrace();
}
} else {
this.databases.putAll(loadDatabases());
}
}

public boolean verifyDatabase(String dbName, String password) {
DatabaseInfo dbInfo = databases.get(dbName);
if (dbInfo == null) {
return false;
}
String hashedPassword = sha256.calculateHash(password);
return databases.containsKey(dbName) && databases.get(dbName).equals(hashedPassword);
return hashedPassword.equals(dbInfo.getHashedPassword());
}

public void createDatabase(String dbName, String password) {
if (databases.containsKey(dbName)) {
throw new IllegalArgumentException("Database already exists.");
}
public void createDatabase(String dbName, String password, String algorithm) {
String hashedPassword = sha256.calculateHash(password);
databases.put(dbName, hashedPassword);
DatabaseInfo dbInfo = new DatabaseInfo(hashedPassword, algorithm);
databases.put(dbName, dbInfo);
saveDatabases();
}

public Map<String, String> loadDatabases() {
if (!databasesFile.exists()) return new HashMap<>();
public Map<String, DatabaseInfo> loadDatabases() {
if (!databasesFile.exists()) {
return new HashMap<>();
}
try (FileReader reader = new FileReader(databasesFile)) {
Gson gson = new Gson();
Type type = new TypeToken<Map<String, String>>() {}.getType();
return gson.fromJson(reader, type);
Type type = new TypeToken<Map<String, DatabaseInfo>>() {}.getType();
return new Gson().fromJson(reader, type);
} catch (IOException e) {
e.printStackTrace();
return new HashMap<>();
}
}

private void saveDatabases() {
try (FileWriter writer = new FileWriter(databasesFile)) {
Gson gson = new GsonBuilder().setPrettyPrinting().create();
gson.toJson(databases, writer);
new GsonBuilder().setPrettyPrinting().create().toJson(databases, writer);
} catch (IOException e) {
e.printStackTrace();
}
}
}

public void encryptAllSites(EncryptionStack encryptionStack) {
for (Map.Entry<String, DatabaseInfo> entry : databases.entrySet()) {
String dbName = entry.getKey();
DatabaseInfo dbInfo = entry.getValue();
File siteFile = new File(dbName + ".json");
if (siteFile.exists()) {
try {
String content = new String(java.nio.file.Files.readAllBytes(siteFile.toPath()));
String encryptedContent = encryptionStack.encrypt(content);
try (FileWriter writer = new FileWriter(siteFile)) {
writer.write(encryptedContent);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

// Inner class to hold database information
public static class DatabaseInfo {
private final String hashedPassword;
private final String algorithm;

public DatabaseInfo(String hashedPassword, String algorithm) {
this.hashedPassword = hashedPassword;
this.algorithm = algorithm;
}

public String getHashedPassword() {
return hashedPassword;
}

public String getAlgorithm() {
return algorithm;
}
}
}
7 changes: 7 additions & 0 deletions src/Classes/EncryptionAlgorithm.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package Classes;

public interface EncryptionAlgorithm {
void init(String key);
String encrypt(String data);
String decrypt(String data);
}
28 changes: 28 additions & 0 deletions src/Classes/EncryptionStack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package Classes;

import java.util.ArrayList;
import java.util.List;

public class EncryptionStack {
private final List<EncryptionAlgorithm> algorithms = new ArrayList<>();

public void addAlgorithm(EncryptionAlgorithm algorithm) {
algorithms.add(algorithm);
}

public String encrypt(String data) {
String result = data;
for (EncryptionAlgorithm algorithm : algorithms) {
result = algorithm.encrypt(result);
}
return result;
}

public String decrypt(String data) {
String result = data;
for (int i = algorithms.size() - 1; i >= 0; i--) {
result = algorithms.get(i).decrypt(result);
}
return result;
}
}
68 changes: 40 additions & 28 deletions src/Classes/Enigma/Enigma.java
Original file line number Diff line number Diff line change
@@ -1,52 +1,64 @@
package Classes.Enigma;

public class Enigma {
private Rotors rotors = new Rotors(); // Rotors component
private Reflector reflector = new Reflector(); // Reflector component
private Plugboard plugboard = new Plugboard(); // Plugboard component
import Classes.EncryptionAlgorithm;

public class Enigma implements EncryptionAlgorithm {
private Rotors rotors = new Rotors(); // Rotors component of the Enigma machine
private Reflector reflector = new Reflector(); // Reflector component of the Enigma machine
private Plugboard plugboard = new Plugboard(); // Plugboard component of the Enigma machine

public Enigma() {
// Default constructor
}

public Enigma(Rotors rotors, Reflector reflector, Plugboard plugboard) {
// Constructor with parameters to initialize the components
this.rotors = rotors;
this.reflector = reflector;
this.plugboard = plugboard;
}

public Rotors getRotors() {
return rotors; // Return the rotors component
}

public Reflector getReflector() {
return reflector; // Return the reflector component
}

public Plugboard getPlugboard() {
return plugboard; // Return the plugboard component
@Override
public void init(String key) {
// Initialize the Enigma machine with the given key
// This could involve setting the initial positions of the rotors, etc.
}

@Override
public String encrypt(String message) {
StringBuilder encryptedMessage = new StringBuilder();
for (char c : message.toCharArray()) {
if (Character.isLetter(c)) {
c = Character.toUpperCase(c); // Convert to uppercase
c = plugboard.swap(c); // Swap using plugboard
c = rotors.rotate(c); // Rotate through rotors
c = reflector.reflect(c); // Reflect the character
c = rotors.rotateBack(c); // Rotate back through rotors
c = plugboard.swap(c); // Swap using plugboard again
StringBuilder encryptedMessage = new StringBuilder(); // StringBuilder to build the encrypted message
for (char c : message.toCharArray()) { // Iterate through each character in the message
if (Character.isLetter(c)) { // Process only letters
c = Character.toUpperCase(c); // Convert character to uppercase
c = plugboard.swap(c); // Swap character using the plugboard
c = rotors.rotate(c); // Rotate character through the rotors
c = reflector.reflect(c); // Reflect character using the reflector
c = rotors.rotateBack(c); // Rotate character back through the rotors
c = plugboard.swap(c); // Swap character again using the plugboard
if (Character.isLowerCase(message.charAt(encryptedMessage.length()))) {
c = Character.toLowerCase(c); // Convert back to lowercase if needed
c = Character.toLowerCase(c); // Convert back to lowercase if original character was lowercase
}
}
encryptedMessage.append(c); // Append the encrypted character
encryptedMessage.append(c); // Append the processed character to the encrypted message
}
return encryptedMessage.toString();
return encryptedMessage.toString(); // Return the final encrypted message
}

@Override
public String decrypt(String message) {
rotors.resetToInitialPosition(); // Reset rotors to initial positions
return encrypt(message); // Decrypt by re-encrypting
rotors.resetToInitialPosition(); // Reset rotors to their initial position
return encrypt(message); // Decrypt by re-encrypting the message
}

public Rotors getRotors() {
return rotors; // Getter for rotors
}

public Reflector getReflector() {
return reflector; // Getter for reflector
}

public Plugboard getPlugboard() {
return plugboard; // Getter for plugboard
}
}
1 change: 1 addition & 0 deletions src/Classes/HelpMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public static void displayMenuHelp(Scanner scanner) {
break;
case 6:
System.out.println("Exiting help menu..."); // Exit message.
Menu.main(null); // Return to the main menu.
break;
default:
System.out.println("Invalid choice. Please select a valid option."); // Handle invalid input.
Expand Down
Loading
Loading