1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
public class Client {
public static void main(String[] args) throws NoSuchAlgorithmException, Exception {
KeyPair kp = generateRSAKeyPair(); //Création de la paire de clé, on considere que c'est la pair de clé du serveur.
Login_MDP lm = new Login_MDP("Patate","Bouch"); //Notre objet
/*
Crypter notre objet lm avce la clé publique de kp (kp.getPublic())
*/
/*
Décrypter l'objet avec la clé privée de kp (kp.getPrivate())
*/
}
//Générateur de paire de clé RSA
public static KeyPair generateRSAKeyPair() throws Exception {
KeyPairGenerator kpGen = KeyPairGenerator.getInstance("RSA");
kpGen.initialize(512, new SecureRandom());
return kpGen.generateKeyPair();
}
//fonction qui chiffrer une donnée avec une clé privée
public static byte[] encrypt(PublicKey key, byte[] data) throws IllegalBlockSizeException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, BadPaddingException {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] text = cipher.doFinal(data);
return text;
}
//fonction qui déchiffre une donnée avec une clé privée
public static byte[] decrypt(PrivateKey key, byte[] data) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] text = cipher.doFinal(data);
return text;
}
} |
Partager