From 4575de680f208fe13833116470664c7252e84bc8 Mon Sep 17 00:00:00 2001
From: Kenmegne <stephanie.kenmegne@gmail.com>
Date: Fri, 19 Jun 2026 11:07:23 +0000
Subject: [PATCH] move to new repo

---
 Encryption/src/main/java/com/megatim/module/encryption/aes/symetric/SymetricCryptography.java |  170 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 170 insertions(+), 0 deletions(-)

diff --git a/Encryption/src/main/java/com/megatim/module/encryption/aes/symetric/SymetricCryptography.java b/Encryption/src/main/java/com/megatim/module/encryption/aes/symetric/SymetricCryptography.java
new file mode 100644
index 0000000..53a27c8
--- /dev/null
+++ b/Encryption/src/main/java/com/megatim/module/encryption/aes/symetric/SymetricCryptography.java
@@ -0,0 +1,170 @@
+/*
+ * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
+ * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
+ */
+package com.megatim.module.encryption.aes.symetric;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.security.InvalidKeyException;
+import java.security.NoSuchAlgorithmException;
+import java.security.spec.InvalidKeySpecException;
+import java.security.spec.KeySpec;
+import java.util.Base64;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import javax.crypto.BadPaddingException;
+import javax.crypto.Cipher;
+import javax.crypto.IllegalBlockSizeException;
+import javax.crypto.NoSuchPaddingException;
+import javax.crypto.SecretKey;
+import javax.crypto.SecretKeyFactory;
+import javax.crypto.spec.PBEKeySpec;
+import javax.crypto.spec.SecretKeySpec;
+
+/**
+ *
+ * @author STEPHANIE
+ */
+public class SymetricCryptography {
+
+    private static final String SALT = "hdbgshdbvnc";
+    private static final String HASH_ALGORITHM = "PBKDF2WithHmacSHA256";
+    private static final String ALGORITHM = "AES";
+    private static final int HASH_ITERATIONS = 65536;
+
+    /**
+     * Fonction qui sert à chiffrer
+     *
+     * @param inputPath : chemin vers le fichier à chiffrer
+     * @param keyLength : {128,192,256}
+     * @param outputPath : chemin vers le resutlat du chiffrement
+     * @param password : clé de chiffrement
+     */
+    public static void encryptFile(String password, int keyLength, String inputPath, String outputPath) {
+        File file = new File(inputPath);
+
+        try {
+            if (file.exists()) {
+                /* Chiffre le fichier */
+                cryptOrDecryptFile(Cipher.ENCRYPT_MODE, password, keyLength, file, new File(outputPath));
+            }
+        } catch (Exception ex) {
+            Logger.getLogger(SymetricCryptography.class.getName()).log(Level.SEVERE, ex.getMessage(), ex);
+        }
+    }
+
+    /**
+     * Fonction qui sert à déchiffrer
+     *
+     * @param inputPath : chemin du fichier à déchiffrer
+     * @param keyLength
+     * @param outputPath : chemin où stocké le resultat du déchiffrement
+     * @param password : clé de déchiffrement
+     */
+    public static void decryptFile(String password, int keyLength, String inputPath, String outputPath) {
+        File file = new File(inputPath);
+
+        try {
+        /* Déchiffre le fichier  */
+        cryptOrDecryptFile(Cipher.DECRYPT_MODE, password, keyLength, file,
+                new File(outputPath));
+
+        } catch (Exception ex) {
+            Logger.getLogger(SymetricCryptography.class.getName()).log(Level.SEVERE, ex.getMessage(), ex);
+    }
+    }
+
+    
+    private static void cryptOrDecryptFile(int mode, String password, int keyLength, File inputFile, File outputFile)
+            throws InvalidKeySpecException, NoSuchAlgorithmException, InvalidKeyException, 
+            NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, IOException {
+
+        SecretKey secret = getKeyFromPassword(password, SALT, keyLength);
+            /* Utilisation de l'algorithme AES */
+            Cipher aesCipher = Cipher.getInstance(ALGORITHM);
+            aesCipher.init(mode, secret);
+
+            if (!outputFile.exists()) {
+                Files.createFile(outputFile.toPath());
+            }
+
+            FileInputStream inputStream = new FileInputStream(inputFile);
+            FileOutputStream outputStream = new FileOutputStream(outputFile);
+
+            byte[] buffer = new byte[64];
+            int bytesRead;
+
+            while ((bytesRead = inputStream.read(buffer)) != -1) {
+                byte[] output = aesCipher.update(buffer, 0, bytesRead);
+                if (output != null) {
+                    outputStream.write(output);
+                }
+            }
+            byte[] outputBytes = aesCipher.doFinal();
+            if (outputBytes != null) {
+                outputStream.write(outputBytes);
+            }
+            inputStream.close();
+            outputStream.close();
+    }
+
+    /**
+     * Méthode servant à générer une clé secrète à partir du mot de passe de l'utilisateur
+     * @param password : mot de passe à partir duquel on veut générer la clé sécrète
+     * @param salt : valeur servant à renforcer la sécurité de la clé sécrète
+     * @param keylength : longueur de la clé en bit
+     * @return
+     * @throws NoSuchAlgorithmException
+     * @throws InvalidKeySpecException 
+     */
+    private static SecretKey getKeyFromPassword(String password, String salt, int keylength)
+            throws NoSuchAlgorithmException, InvalidKeySpecException {
+
+        SecretKeyFactory factory = SecretKeyFactory.getInstance(HASH_ALGORITHM);
+        KeySpec spec = new PBEKeySpec(password.toCharArray(), salt.getBytes(), HASH_ITERATIONS, keylength);
+        SecretKey secret = new SecretKeySpec(factory.generateSecret(spec)
+                .getEncoded(), ALGORITHM);
+        return secret;
+    }
+
+    
+    public static String encryptString(String password, int keyLength, String strToEncrypt) {
+
+        try {
+            SecretKey secret = getKeyFromPassword(password, SALT, keyLength);
+
+            /* Utilisation de l'algorithme AES */
+            Cipher aesCipher = Cipher.getInstance(ALGORITHM);
+            aesCipher.init(Cipher.ENCRYPT_MODE, secret);
+
+            /* Chiffre la chaine de charactères */
+            return Base64.getEncoder().encodeToString(aesCipher.doFinal(strToEncrypt.getBytes(StandardCharsets.UTF_8)));
+        } catch (Exception ex) {
+            Logger.getLogger(SymetricCryptography.class.getName()).log(Level.SEVERE, ex.getMessage(), ex);
+            return "";
+        }
+    }
+
+    public static String decryptString(String password, int keyLength, String strToDecrypt) {
+
+        try {
+             SecretKey secret = getKeyFromPassword(password, SALT, keyLength);
+
+            /* Utilisation de l'algorithme AES */
+            Cipher aesCipher = Cipher.getInstance(ALGORITHM);
+            aesCipher.init(Cipher.DECRYPT_MODE, secret);
+
+            /* Déchiffre le fichier  */
+            return new String(aesCipher.doFinal(Base64.getDecoder().decode(strToDecrypt))); 
+
+        } catch (Exception ex) {
+            Logger.getLogger(SymetricCryptography.class.getName()).log(Level.SEVERE, ex.getMessage(), ex);
+            return "";
+        }
+    }
+}

--
Gitblit v1.10.0