Cryptographie Java
Chapitre 2: Création des clés
M. Remane NIZAR
PhD student in Coding, Cryptology, Algebra and application at UCAD
Information Systems Security and Cybersecurity Engineer at ESP
[Link]@[Link] remanenizar@[Link]
Licence 3 Cybersécurité
ISI 2024 - 2025
3 décembre 2024
M. Remane NIZAR PhD student in Coding, Cryptology,Java
Cryptographie Algebra
Chapitre
and application
2: Création at
des
UCAD
clés Information
3 décembreSystems
2024 Security
1 / 25an
Plan
1 Génération des clés
Génération des clés
Les methodes à Implementer pour l’interface Key
2 Génération d’une clé secrète
KeyGenerato
PBE : SecretKey à partir de mot de passe
3 Génération de paire de clé publique et privée
RSAPrivateKey et RSAPublicKey
DSAPrivateKey et DSAPublicKey
DHPrivateKey et DHPublicKey
Classe [Link]
Générteur de paire de clé
4 TP 2 : Génération des clés
M. Remane NIZAR PhD student in Coding, Cryptology,Java
Cryptographie Algebra
Chapitre
and application
2: Création at
des
UCAD
clés Information
3 décembreSystems
2024 Security
2 / 25an
Génération des clés
Figure – Génération des clé
M. Remane NIZAR PhD student in Coding, Cryptology,Java
Cryptographie Algebra
Chapitre
and application
2: Création at
des
UCAD
clés Information
3 décembreSystems
2024 Security
3 / 25an
Génération des clés
Interface
[Link]
L’interface qui encapsule une clé cryptographique.
[Link]
L’interface qui encapsule une clé privée
[Link]..PublicKey
L’interface qui encapsule une clé publique
[Link]
L’interface qui encapsule une clé secrète
M. Remane NIZAR PhD student in Coding, Cryptology,Java
Cryptographie Algebra
Chapitre
and application
2: Création at
des
UCAD
clés Information
3 décembreSystems
2024 Security
4 / 25an
Les méthodes à implémenter pour l’interface Key
getAlgorithm() : retourne un String représentant le nom de
l’algorithme utilisé pour la génération de la clé.
getFormat() : retourne un String représentant le nom du format de la
clé
getEncoded() : retourne un tableau de byte représentant la valeur de
la clé codée suivant le format précisée.
Exemple de format d’une clé
X.509 : Standard de syntaxe d’information de clé publique conçu par
l’Union internationale des télécommunications pour les
infrastructures à clé publique.
PKCS : Standard de syntaxe d’information de clé privée conçu par les
laboratoires RSA.
RAW : Standard de syntaxe d’information de clé secrète.
M. Remane NIZAR PhD student in Coding, Cryptology,Java
Cryptographie Algebra
Chapitre
and application
2: Création at
des
UCAD
clés Information
3 décembreSystems
2024 Security
5 / 25an
Génération d’une clé Secrète : Clé
cryptographiquement sure
Figure – Génération des clé
M. Remane NIZAR PhD student in Coding, Cryptology,Java
Cryptographie Algebra
Chapitre
and application
2: Création at
des
UCAD
clés Information
3 décembreSystems
2024 Security
6 / 25an
Engine : KeyGenerator
1 Récupération d’une instance de KeyGenerator
getInstance(String algorithm)
getInstance(String algorithm, Provider provider)
getInstance(String algorithm, String provider)
2 Initialisation du KeyGenerator (Facultative mais intéressante)
init(int keysize)
init(SecureRandom random)
init(int keysize, SecureRandom random)
3 Génération de la SecretKey
public final SecretKey generateKey()
M. Remane NIZAR PhD student in Coding, Cryptology,Java
Cryptographie Algebra
Chapitre
and application
2: Création at
des
UCAD
clés Information
3 décembreSystems
2024 Security
7 / 25an
Interface : SecretKey
1 Méthodes de SecretKey
String getAlgorithm()
String getFormat()
byte[] getEncoded()
2 Sérialisation : L’interface SecretKey est sérialisable.
Enregistrer la SecretKey dans un fichier à l’aide d’un
ObjectOutputStream
Plus tard récupérer la SecretKey à l’aide d’un ObjectInputStream
M. Remane NIZAR PhD student in Coding, Cryptology,Java
Cryptographie Algebra
Chapitre
and application
2: Création at
des
UCAD
clés Information
3 décembreSystems
2024 Security
8 / 25an
PBE : SecretKey à partir de mot de passe
Password Based Encryption : PBE
Objectif
Créer une clé cryptographiquement sure à partir de mot de passe.
Réduire l’efficacité d’une attaque par dictionnaire
1 Spécification de clé PBE
password en char [ ]
salt en byte [ ] : donnée de préférence aléatoire concaténée au password
un nombre d’itérations : nombre d’application d’un MD sur password +
salt
2 Une Factory de SecretKey PBE
Engine : SecretKeyFactory
Instance : 1 des 3 getInstance(· · · )
Methode : SecretKey generateSecret(KeySpec keySpec)
M. Remane NIZAR PhD student in Coding, Cryptology,Java
Cryptographie Algebra
Chapitre
and application
2: Création at
des
UCAD
clés Information
3 décembreSystems
2024 Security
9 / 25an
PBE : SecretKey à partir de mot de passe
1 Spécification de clé PBE
PBEKeySpec aPBEKeySpec = new PBEKeySpec(password, salt,
iteration) ;
2 Une Factory de SecretKey SecretKeyFactory
aSecretKeyFactory =
[Link]("PBEWITHSHAAND256BITAES-CBC-
BC") ;
SecretKey aSecretKey =
[Link](aPBEKeySpec) ;
M. Remane NIZAR PhD student in Coding, Cryptology,Java
Cryptographie Algebra
Chapitre
and application
2: Création at
des
UCAD
clés Information
3 décembreSystems
2024 Security
10 / 25an
Génération de paire de clés publique et privée
Les sous interfaces les plus connues de l’interface PublicKey et
PrivateKey
Figure – Génération des clé privé et publique
M. Remane NIZAR PhD student in Coding, Cryptology,Java
Cryptographie Algebra
Chapitre
and application
2: Création at
des
UCAD
clés Information
3 décembreSystems
2024 Security
11 / 25an
Génération de paire de clés publique et privée
Figure – Génération des clé privé et publique
M. Remane NIZAR PhD student in Coding, Cryptology,Java
Cryptographie Algebra
Chapitre
and application
2: Création at
des
UCAD
clés Information
3 décembreSystems
2024 Security
12 / 25an
RSAPrivateKey et RSAPublicKey
Étapes pour la génération de paire de clés RSA
1 Générer aléatoirement 2 grands nombres premiers p et q
2 Calculer n = P × q
3 Calculer ϕ(n) = (P − 1)(q − 1)
4 Choisir un entier e vérifiant 0 < e < ϕ(n) et pgdc(e, ϕ(n)) = 1
5 Trouver un d tel que 0 < dϕ(n) et ed≡ 1modϕ(n)
6 La clé publique est constituée de e(l’exposant publique) et n (le
module)
7 La clé privée est constituée de d (l’exposant privé) et n.
M. Remane NIZAR PhD student in Coding, Cryptology,Java
Cryptographie Algebra
Chapitre
and application
2: Création at
des
UCAD
clés Information
3 décembreSystems
2024 Security
13 / 25an
RSAPrivateKey et RSAPublicKey
Package [Link]
Figure – Génération des clé RSA
M. Remane NIZAR PhD student in Coding, Cryptology,Java
Cryptographie Algebra
Chapitre
and application
2: Création at
des
UCAD
clés Information
3 décembreSystems
2024 Security
14 / 25an
DSAPrivateKey et DSAPublicKey
Génération des paramètres du DSA-domaine
1 Choisir un nombre premier q vérifiant : 2159 < q < 260
2 Choisir un entier t : 0 < t < 8
3 Choisir un nombre entier P vérifiant : q divise P - 1 et
2511+64t < P < 2512+64t
4 Choisir un entier g d’orde q
4.1. Choisir un entier h ∈ [1, P − 1]
4.2. Calculer g =h(P−1)/q mod P
4.3. Si g = 1 alors retourne à 4.1.
5 Retourner (P, q, g)
M. Remane NIZAR PhD student in Coding, Cryptology,Java
Cryptographie Algebra
Chapitre
and application
2: Création at
des
UCAD
clés Information
3 décembreSystems
2024 Security
15 / 25an
DSAPrivateKey et DSAPublicKey
Génération de la DSA - paire de clés
Entrée : DSA-domaine de paramètre (p ; q ; g).
Sortie : clé publique y et clé privée x.
1 Choisir un entier x ∈ [1, q − 1]
2 Calculer y = g x mod P
3 Retourne (x,y)
package [Link]
Figure – Génération des clé DSA
M. Remane NIZAR PhD student in Coding, Cryptology,Java
Cryptographie Algebra
Chapitre
and application
2: Création at
des
UCAD
clés Information
3 décembreSystems
2024 Security
16 / 25an
DHPrivateKey et DHPublicKey
package [Link]
Figure – Génération des clé DHP
M. Remane NIZAR PhD student in Coding, Cryptology,Java
Cryptographie Algebra
Chapitre
and application
2: Création at
des
UCAD
clés Information
3 décembreSystems
2024 Security
17 / 25an
Classe [Link]
Figure – Génération de paire des clés
M. Remane NIZAR PhD student in Coding, Cryptology,Java
Cryptographie Algebra
Chapitre
and application
2: Création at
des
UCAD
clés Information
3 décembreSystems
2024 Security
18 / 25an
Générateur de paire de clés
[Link]
Une instance de cette classe est créée à l’aide de l’une des méthodes de
factory suivantes :
public static KeyPairGenerator getInstance( String algorithm ) throws
NoSuchAlgorithmException
public static KeyPairGenerator getInstance(String algorithm, Provider
provider) throws NoSuchAlgorithmException
public static KeyPairGenerator getInstance(String algorithm, String
provider) throws NoSuchAlgorithmException,
NoSuchProviderException
Le paramètre "algorithm" peut être remplacer par :
"RSA" exemple de provider "SUN"
"DH" exemple de provider "SunJCE"
"DSA" exemple de provider "SUN"
M. Remane NIZAR PhD student in Coding, Cryptology,Java
Cryptographie Algebra
Chapitre
and application
2: Création at
des
UCAD
clés Information
3 décembreSystems
2024 Security
19 / 25an
Générateur de paire de clés
Initialisation
On peut initialiser l’objet KeyPairGenerator pour régénérer une paire de
clés d’une longueur keysize à l’aide de la méthode :
public void initialize ( int keysize ) ;
On peut aussi introduire une source d’aléatoire :
public void initialize ( int keysize , SecureRandom random ) ;
Ces deux méthodes rejettent une exception InvalidParameterException si le
keysize choisi n’est pas supporté par l’objet KeyPairGenerator.
Pour le "DSA" et le "DH" le Keysize est un multiple de 64 compris entre
512 et 1024.
Pour le "RSA", c’est un multiple de 8 supérieur ou égal à 512.
M. Remane NIZAR PhD student in Coding, Cryptology,Java
Cryptographie Algebra
Chapitre
and application
2: Création at
des
UCAD
clés Information
3 décembreSystems
2024 Security
20 / 25an
Générateur de paire de clés
1 Finalement on récupérer la paire de clés KeyPair à l’aide de la
méthode suivante :
public KeyPair generateKeyPair()
2 Pour extraire la clé publique ou privée de l’objet KeyPair, on utilise les
deux méthodes suivantes :
public PrivateKey getPrivate()
public PublicKey getPublic()
M. Remane NIZAR PhD student in Coding, Cryptology,Java
Cryptographie Algebra
Chapitre
and application
2: Création at
des
UCAD
clés Information
3 décembreSystems
2024 Security
21 / 25an
Générateur de paire de clés
Exemple
KeyPairGenerator kpg = [Link]("DH", "SunJCE") ;
[Link](704, new SecureRandom()) ;
KeyPair kp = [Link]() ;
DHPrivateKey privatekey = (DHPrivateKey) [Link]() ;
DHPublicKey publickey = (DHPublicKey)[Link]() ;
Les objets Key, PrivateKey, PublicKey, RSAPrivateKey, RSAPublicKey,
DSAPrivateKey,.... ont une représentation opaque : l’accès à leur structures
est limité.
L’utilisateur ne peut pas les créer avec ses propres données.
M. Remane NIZAR PhD student in Coding, Cryptology,Java
Cryptographie Algebra
Chapitre
and application
2: Création at
des
UCAD
clés Information
3 décembreSystems
2024 Security
22 / 25an
Générateur de paire de clés
Exemple
Figure – Clé publique RSA
M. Remane NIZAR PhD student in Coding, Cryptology,Java
Cryptographie Algebra
Chapitre
and application
2: Création at
des
UCAD
clés Information
3 décembreSystems
2024 Security
23 / 25an
Générateur de paire de clés
Figure – Génération de paire des clés
M. Remane NIZAR PhD student in Coding, Cryptology,Java
Cryptographie Algebra
Chapitre
and application
2: Création at
des
UCAD
clés Information
3 décembreSystems
2024 Security
24 / 25an
TP 2 : Génération des clés
1 Clé secrète (Key ou SecretKey) :
a. Ecrire un programme permettant de créer une clé secrète avec les
algorithmes DES puis AES et avec les providers SUN et BC.
b. Ecrire un programme permettant de créer une clé secrète avec les
algorithmes DES puis AES avec PBE (PassWord Based Encryption).
2 En utilisant la JCA et JCE, écrire une classe java permettant :
a. de créer une paire de clés ALGO avec le provider PROV et avec
une source aléatoire sous forme opaque ;
b. de recréer la paire de clé ALGO avec les spécifications
(KeyFactory) ;
c. de recréer la paire de clé ALGO avec les encodages.
d. enfin de comparer les trois paires de clés.
NB : Remplacer ALGO par RSA , DSA et DH puis remplacer PROV
par SUN ,SUNJCE et BC (BouncyCastle).
M. Remane NIZAR PhD student in Coding, Cryptology,Java
Cryptographie Algebra
Chapitre
and application
2: Création at
des
UCAD
clés Information
3 décembreSystems
2024 Security
25 / 25an