TP3 : RSA - JAVA
1. Objectifs :
Mettre en œuvre un crypto système RSA en java en utilisant la classe BigInteger
(utilisation des grands nombres entiers)
- Générer une paire de clés RSA
- Utiliser cette paire de clés pour chiffrer et déchiffrer un message
- Utiliser la paire de clés générée avec les algorithmes RSA implémentés dans
JAVA
2. Implémentation de RSA
Principe de l’algorithme RSA :
Soient deux nombres premiers p et q, et un entier positif n.
- On pose 𝑛 = 𝑝 × 𝑞.
- On choisit un nombre d tel que 𝑒 × 𝑑 = 1(𝑚𝑜𝑑𝑢𝑙𝑜((𝑝 − 1)(𝑞 − 1))).
- La clef publique est (e, n) et la clef privée est (p, q, d, n).
Chiffrement et déchiffrement
Soit M un message en clair ayant la même longueur (en bits) que n. Il est chiffré de la
manière suivante :
- 𝐶 = 𝑀𝑒 (𝑚𝑜𝑑𝑢𝑙𝑜 𝑛)
Déchiffrement du message :
- 𝐶 𝑑 (𝑚𝑜𝑑𝑢𝑙𝑜 𝑛) = (𝑀𝑒 )𝑑 )(𝑚𝑜𝑑𝑢𝑙𝑜 𝑛) = 𝑀 𝑒.𝑑 (𝑚𝑜𝑑𝑢𝑙𝑜 𝑛) = 𝑀
a. Génération de paire de clés RSA
/ * algorithme de génération de clés */
/ * initialisation d’un générateur de nombres pseudo-aléatoire avec comme graine
[Link]() qui est le nombre de millisecondes écoulées depuis le 1 er
janvier 1970 */
Random rd = new Random([Link]());
/ * génération de p, le constructeur Java crée un nombre aléatoire de 512 bits, 1024 bits
ou 2048 bits et renvoie le premier nombre premier supérieur à celui-ci /
BigInteger bigP = [Link] (512,rd);
/* génération de q */
BigInteger bigQ = [Link] (512,rd);
/* verifier que le pgcd(p,q) = 1 */
BigInteger pgcd =[Link](bigQ);
[Link]("PGCD de P et Q = " + pgcd);
/* calcul de n */
BigInteger bigN = [Link](bigQ);
[Link]("N: " + bigN);
/* calcul de (p-1)(q-1)*/
BigInteger bigP1 = [Link]([Link]);
BigInteger bigQ1 = [Link]([Link]);
BigInteger bigP1Q1 = [Link](bigQ1);
/* on choisit un nombre e arbitrairement, 65537 (0x10001 en hexadécimal) est
généralement utilisé */
BigInteger bigE = [Link](0x10001L);
/* on calcule d l’inverse de e modulo (p-1)(q-1)*/
BigInteger bigD = [Link](bigP1Q1);
/* on verifie que e.d = 1 modulo (p-1)(q-1)*/
[Link] Page 1
BigInteger ED =[Link](bigD).mod(bigP1Q1);
[Link]("e.d mod (p-1)(q-1): " + ED);
b. Chiffrement d’un message avec RSA
String plaintext = "abcdef";
// encryption
byte[] cipher = [Link]([Link]());
[Link]("Encrypting Bytes: " + bToS(cipher));
[Link]("Cipher text is: " + new String(cipher));
La procedure publique encrypt
public byte[] encrypt(byte[] message) {
return (new BigInteger(message)).modPow(bigE, bigN).toByteArray();}
c. Déchiffrement d’un message avec RSA
// decryption
byte[] plain = [Link](cipher);
[Link]("Decrypting Bytes: " + bToS(plain));
[Link]("Plain message is: " + new String(plain));
La procedure publique decrypt
public byte[] decryptMessage(byte[] message)
{ return (new BigInteger(message)).modPow(bigD, bigN).toByteArray();}
Sauvegarder le programme dans gen_pair_RSA.java avec les tailles 512 bits et 1024
bits et afficher les sorties textes générées ([Link], [Link]) avec le
programme wordpad pour voir les grands nombres entiers générés.
import [Link];
import [Link];
import [Link];
import [Link];
public class gen_pair_RSA {
private static void RSA_paire() throws Exception {
... code de generation de paire de clés
public static void main (String [] arguments) throws IOException
{
gen_pair_RSA rsa = new gen_pair_RSA ();
DataInputStream input = new DataInputStream([Link]);
String inputString;
[Link]("Enter message you wish to send.");
inputString = [Link]();
[Link]("Encrypting the message: " + inputString);
[Link]("The message in bytes is:: "
+ bToS([Link]()));
// encryption
byte[] cipher = [Link]([Link]());
[Link]("Encrypting Bytes: " + bToS(cipher));
[Link]("Cipher text is: " + new String(cipher));
// decryption
byte[] plain = [Link](cipher);
[Link]("Decrypting Bytes: " + bToS(plain));
[Link]("Plain message is: " + new String(plain));
}
[Link] Page 2
}
Ces opérations sont implémentées dans la classe crypto_RSA.java pour être
instanciée dans n’importe quel programme java (ex. : RSA_cs.java).
public class RSA_CS {
/**
* @param args the command line arguments
*/
private static String bToS(byte[] cipher)
{
String temp = "";
for (byte b : cipher)
{
temp += [Link](b);
}
return temp;
}
public static void main(String[] args)throws Exception{
RSA_crypto rsa = new RSA_crypto();
String message = "euschi";
[Link]("plain text message in bytes is:: "
+ bToS([Link]()));
byte[] cipher = [Link]([Link]());
[Link]("The encrypted message in bytes is: "
+ bToS(cipher));
[Link]("message chiffré : " + new String(cipher));
byte[] plain = [Link](cipher);
[Link]("The decrypted message in bytes is:: "
+ bToS(plain));
[Link]("message plaintext : " + new String(plain));
}}
[Link] Page 3