Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

generateur de mdp en java #188

Copy link
Copy link
@zainoxIII

Description

@zainoxIII
Issue body actions

// Fichier: PasswordGeneratorGUI.java
import javax.swing.;
import java.awt.
;
import java.awt.datatransfer.;
import java.awt.event.
;
import java.security.SecureRandom;
import java.util.*;

public class PasswordGeneratorGUI extends JFrame {
private static final String LOWER = "abcdefghijklmnopqrstuvwxyz";
private static final String UPPER = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
private static final String DIGIT = "0123456789";
private static final String SYMBOL = "!@#$%^&*;

private static final Set<Character> SIMILAR = new HashSet<>(Arrays.asList(
        '0','O','o','1','l','I','|','5','S','2','Z','8','B'
));


private static final SecureRandom RNG = new SecureRandom();

// UI components
private JTextField outputField;
private JSlider lengthSlider;
private JCheckBox lowerBox, upperBox, digitBox, symbolBox, excludeSimilarBox, requireEachBox;
private JLabel entropyLabel, strengthLabel;

public PasswordGeneratorGUI() {
    super("🔐 Générateur de mots de passe");

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(500, 300);
    setLayout(new BorderLayout(10,10));

    // Top output
    JPanel top = new JPanel(new BorderLayout(5,5));
    outputField = new JTextField();
    outputField.setFont(new Font("Monospaced", Font.PLAIN, 16));
    top.add(outputField, BorderLayout.CENTER);

    JButton copyBtn = new JButton("Copier");
    copyBtn.addActionListener(e -> copyToClipboard());
    top.add(copyBtn, BorderLayout.EAST);

    add(top, BorderLayout.NORTH);

    // Center controls
    JPanel center = new JPanel();

    center.setLayout(new GridLayout(0,2));

    lowerBox = new JCheckBox("Minuscules (a-z)", true);
    upperBox = new JCheckBox("Majuscules (A-Z)", true);
    digitBox = new JCheckBox("Chiffres (0-9)", true);
    symbolBox = new JCheckBox("Symboles (!@#…)", true);
    excludeSimilarBox = new JCheckBox("Exclure caractères ambigus (0,O,1,l...)", true);
    requireEachBox = new JCheckBox("Exiger ≥1 caractère de chaque catégorie", true);

    center.add(lowerBox);
    center.add(upperBox);
    center.add(digitBox);
    center.add(symbolBox);
    center.add(excludeSimilarBox);
    center.add(requireEachBox);

    add(center, BorderLayout.CENTER);

    // Bottom panel
    JPanel bottom = new JPanel(new BorderLayout(5,5));

    lengthSlider = new JSlider(8, 64, 16);
    lengthSlider.setMajorTickSpacing(14);
    lengthSlider.setMinorTickSpacing(2);
    lengthSlider.setPaintTicks(true);
    lengthSlider.setPaintLabels(true);

    bottom.add(new JLabel("Longueur :"), BorderLayout.WEST);
    bottom.add(lengthSlider, BorderLayout.CENTER);

    JButton genBtn = new JButton("Générer");
    genBtn.addActionListener(e -> generatePassword());
    bottom.add(genBtn, BorderLayout.EAST);

    add(bottom, BorderLayout.SOUTH);

    // Status panel
    JPanel status = new JPanel(new GridLayout(2,1));
    entropyLabel = new JLabel("Entropie : -- bits");
    strengthLabel = new JLabel("Robustesse : --");
    status.add(entropyLabel);
    status.add(strengthLabel);
    add(status, BorderLayout.WEST);

    setLocationRelativeTo(null); // center window
    setVisible(true);
}

private void copyToClipboard() {
    String text = outputField.getText();
    if (text.isEmpty()) return;
    Toolkit.getDefaultToolkit()
            .getSystemClipboard()
            .setContents(new StringSelection(text), null);
    JOptionPane.showMessageDialog(this, "Mot de passe copié !");
}

private void generatePassword() {
    int length = lengthSlider.getValue();

    java.util.List<String> sets = new ArrayList<>();
    if (lowerBox.isSelected()) sets.add(LOWER);
    if (upperBox.isSelected()) sets.add(UPPER);
    if (digitBox.isSelected()) sets.add(DIGIT);
    if (symbolBox.isSelected()) sets.add(SYMBOL);

    String pool = combineAndFilter(sets, excludeSimilarBox.isSelected());

    if (pool.isEmpty()) {
        JOptionPane.showMessageDialog(this, "Aucun jeu de caractères disponible !");
        return;
    }

    String pwd = requireEachBox.isSelected()
            ? generateWithRequirement(length, sets, pool, excludeSimilarBox.isSelected())
            : randomFromPool(pool, length);

    outputField.setText(pwd);

    // update entropy
    int uniqueChars = (int) pool.chars().distinct().count();
    double entropyBits = length * (Math.log(uniqueChars)/Math.log(2));
    String strength = strengthLabel(entropyBits);

    entropyLabel.setText(String.format("Entropie : ~%.0f bits", entropyBits));
    strengthLabel.setText("Robustesse : " + strength);
}

private String combineAndFilter(java.util.List<String> sets, boolean excludeSimilar) {
    StringBuilder sb = new StringBuilder();
    for (String s : sets) sb.append(s);

    StringBuilder out = new StringBuilder();
    for (char c : sb.toString().toCharArray()) {
        if (excludeSimilar && SIMILAR.contains(c)) continue;
        out.append(c);
    }
    return out.toString();
}

private String generateWithRequirement(int length, java.util.List<String> sets, String pool, boolean excludeSimilar) {
    java.util.List<Character> chars = new ArrayList<>();

    for (String s : sets) {
        String filtered = combineAndFilter(Collections.singletonList(s), excludeSimilar);
        if (!filtered.isEmpty()) {
            chars.add(filtered.charAt(RNG.nextInt(filtered.length())));
        }
    }

    for (int i = chars.size(); i < length; i++) {
        chars.add(pool.charAt(RNG.nextInt(pool.length())));
    }

    // shuffle
    Collections.shuffle(chars, RNG);
    StringBuilder sb = new StringBuilder();
    for (char c : chars) sb.append(c);
    return sb.toString();
}

private String randomFromPool(String pool, int length) {
    StringBuilder sb = new StringBuilder(length);
    for (int i = 0; i < length; i++) {
        sb.append(pool.charAt(RNG.nextInt(pool.length())));
    }
    return sb.toString();
}

private String strengthLabel(double bits) {
    if (bits < 28) return "Très faible";
    if (bits < 36) return "Faible";
    if (bits < 60) return "Correct";
    if (bits < 128) return "Fort";
    return "Très fort";
}

public static void main(String[] args) {
    SwingUtilities.invokeLater(PasswordGeneratorGUI::new);
}

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.